在 Kubernetes 集群中的所有 pods 共享 express.js 应用程序的 public 目录

Sharing the public directory of an express.js app across all pods in a Kubernetes cluster

我想使用部署在 Azure AKS 上的 Kubernetes 集群中的卷或持久卷来共享 public 应用程序的 public 目录中的文件。集群有3个副本。

我试过以下操作:https://docs.microsoft.com/en-us/azure/aks/azure-files-volume。我联系了 Kubernetes Slack 频道。有人建议我尝试持久卷和持久卷声明。我这样做了,但我仍然没有在任何 pods.

中看到共享目录
kind: Deployment
metadata:
  name: node-ffmpeg-video-cms-deployment 
  labels:
    app: node-ffmpeg-video-cms
spec:
  replicas: 3
  template:
    metadata:
      name: node-ffmpeg-video-cms
      labels:
        app: node-ffmpeg-video-cms
    spec:
      containers:
      - name: node-ffmpeg-video-cms
        image: nodeffmpegvideocmscr.azurecr.io/node-ffmpeg-video-cms:v1
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: mystorageaccount17924
          mountPath: /www/var/public
      restartPolicy: Always
      volumes:
      - name: mystorageaccount17924
        azureFile:
          secretName: azure-secret
          shareName: node-ffmpeg-video-cms
          readOnly: false
  selector:
    matchLabels:
      app: node-ffmpeg-video-cms

---

apiVersion: v1
kind: Service
metadata:
  name: node-ffmpeg-video-cms-service
spec:
  selector:
    app: node-ffmpeg-video-cms
  ports:
    - port: 3000
  type: LoadBalancer

---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: sample-storage
  # The label is used for matching the exact claim
  labels:
    usage: sample-storage
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  azureFile:
    secretName: azure-secret
    shareName: node-ffmpeg-video-cms
    readOnly: false
  mountOptions:
    - dir_mode=0777
    - file_mode=0777
    - uid=1000
    - gid=1000

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sample-storage-claim
  annotations:
    volume.beta.kubernetes.io/storage-class: ""
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  selector:
    matchLabels:
      usage: sample-storage

当我 运行 kubectl describe pod on my pods:

Namespace:      default
Priority:       0
Node:           aks-nodepool1-22998726-0/10.240.0.4
Start Time:     Fri, 26 Jul 2019 14:55:29 -0500
Labels:         app=node-ffmpeg-video-cms
                pod-template-hash=8547d97c69
Annotations:    <none>
Status:         Running
IP:             10.244.0.24
Controlled By:  ReplicaSet/node-ffmpeg-video-cms-deployment-8547d97c69
Containers:
  node-ffmpeg-video-cms:
    Container ID:   docker://4c4f89dfc0058fcaa6fcba0b3dd66e89493715fe4373ffe625eacc0296a45ae1
    Image:          nodeffmpegvideocmscr.azurecr.io/node-ffmpeg-video-cms:v1
    Image ID:       docker-pullable://nodeffmpegvideocmscr.azurecr.io/node-ffmpeg-video-cms@sha256:2b949efa8535b59a927efbb4d7c6d24739691fa90fad86c91086dc4cfbadbe23
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 26 Jul 2019 14:55:31 -0500
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-7pb5v (ro)
      /www/var/public from mystorageaccount17924 (rw)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  mystorageaccount17924:
    Type:        AzureFile (an Azure File Service mount on the host and bind mount to the pod)
    SecretName:  azure-secret
    ShareName:   node-ffmpeg-video-cms
    ReadOnly:    false
  default-token-7pb5v:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-7pb5v
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

要将 public 目录共享到 Azure 中的多个 pods 部署,您可以使用 Azure 文件共享,如您提供的 link 中所示。并且在部署中设置 volumesvolumeMounts 时不需要创建 PV/PVC。

它在我的测试中运行良好,我在下面展示了部署和结果的屏幕截图:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: nginx
        image: nginx:1.15.5
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        volumeMounts:
          - name: azure
            mountPath: /mnt/azure
      volumes:
        - name: azure
          azureFile:
            shareName: aksshare
            secretName: azure-secret
            readOnly: false