実行手順
1. テスト用ページの作成NFSサーバーのホストにテスト用ページを作成します。
sudo mkdir -p /shared_dir/html
echo '<html><head><title>sample</title></head><body>contents on NFS server.</body></html>' | sudo tee /shared_dir/html/index.html
2.永続ボリュームの作成
以下のコマンドで、NFSサーバ上の永続ストレージを作成します。
cat << EOF > nfs-httpd-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-httpd-pv
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadOnlyMany
  storageClassName: slow
  nfs:
    server: ※NFSサーバのIPアドレス
    path: "/shared_dir/html" ※NFSサーバのパス
  mountOptions:
    - nfsvers=4.2
EOF
microk8s kubectl apply -f ./nfs-httpd-pv.yml
3. 永続ボリューム要求の作成
以下のコマンドで、永続ストレージ要求を作成します。
cat << EOF > nfs-httpd-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-httpd-pvc
spec:
  accessModes:
    - ReadOnlyMany
  storageClassName: slow
  resources:
    requests:
      storage: 2Gi
  volumeName: nfs-httpd-pv
EOF
microk8s kubectl apply -f ./nfs-httpd-pvc.yml
4. Deploymentの作成
以下のコマンドでhttpd:2.4-alipineのイメージと作成したNFS上の永続ボリュームを使用するDeploymentを作成します。
cat << EOF > httpd-nfs-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment
  labels:
    app: myhttpd
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myhttpd
  template:
    metadata:
      labels:
        app: myhttpd
    spec:
      containers:
      - name: myhttpd
        image: httpd:2.4-alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - name: documentroot
          mountPath: /usr/local/apache2/htdocs
      volumes:
      - name: documentroot
        persistentVolumeClaim:
          claimName: nfs-httpd-pvc
EOF
microk8s kubectl apply -f ./httpd-nfs-deployment.yml
5. サービスの作成
以下のコマンドでサービスを作成します。
cat << EOF > httpd-service.yml
apiVersion: v1
kind: Service
metadata:
  name: httpd-service
spec:
  selector:
    app: myhttpd
  type: LoadBalancer
  externalIPs:
  - xxx.xxx.xxx.xxx # 外部からアクセスできるMicrok8sをインストールしたホストのIPを設定します。
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80
EOF
microk8s kubectl apply -f ./httpd-service.yml
ブラウザからhttp://<ホスト名:8080/ にアクセスします
 
0 件のコメント:
コメントを投稿