Kubernetes中的多个卷挂载:一个可以工作,另一个不行。

9

我正在尝试创建一个Kubernetes pod,其中包含一个单独的容器,该容器上挂载了两个外部卷。我的.yml pod文件如下:

apiVersion: v1
kind: Pod
metadata:
  name: my-project
  labels:
    name: my-project
spec:
  containers:
    - image: my-username/my-project
      name: my-project
      ports:
        - containerPort: 80
          name: nginx-http
        - containerPort: 443
          name: nginx-ssl-https
      imagePullPolicy: Always
      volumeMounts:
        - mountPath: /home/projects/my-project/media/upload
          name: pd-data
        - mountPath: /home/projects/my-project/backups
          name: pd2-data
  imagePullSecrets:
    - name: vpregistrykey
  volumes:
    - name: pd-data
      persistentVolumeClaim:
        claimName: pd-claim
    - name: pd2-data
      persistentVolumeClaim:
        claimName: pd2-claim

我正在使用持久卷和持久卷声明,如下所示: PV
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pd-disk
  labels:
    name: pd-disk
spec:
  capacity:
    storage: 250Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: "pd-disk"
    fsType: "ext4"

PVC

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pd-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 250Gi

我最初使用以下命令创建了我的磁盘: $ gcloud compute disks create --size 250GB pd-disk 第二个磁盘、第二个PV和PVC也是如此。当我创建pod时,似乎一切正常,没有出现错误。现在出现了奇怪的情况:其中一个路径被正确挂载(因此是持久的),而另一个路径每次重新启动pod时都会被删除...
我已经尝试从头开始重新创建所有内容,但没有任何改变。此外,从pod的描述中,两个卷似乎都被正确挂载:
$ kubectl describe pod my-project
Name:       my-project
...
Volumes:
  pd-data:
    Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  pd-claim
    ReadOnly: false
  pd2-data:
    Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  pd2-claim
    ReadOnly: false

非常感谢您的帮助。谢谢。


你使用的 Kubernetes 版本是什么,你是如何重新启动 Pod 的? - Yu-Ju Hong
我正在使用 Kubernetes v1,为了重启 Pod,我使用 kubectl delete -f my-project.yml 然后跟着使用 kubectl create -f my-project.yml - AdelaN
kubectl version 的输出是什么? - Yu-Ju Hong
Client Version: version.Info{Major:"1", Minor:"1", GitVersion:"v1.1.4", GitCommit:"a5949fea3a91d6a50f40a5684e05879080a4c61d", GitTreeState:"clean"} Server Version: version.Info{Major:"1", Minor:"1", GitVersion:"v1.1.7", GitCommit:"e4e6878293a339e4087dae684647c9e53f1cf9f0", GitTreeState:"clean"} - AdelaN
如果您已经解决了这个问题,您可以在此发布自我答案,以供可能遇到相同问题的其他社区成员参考。 - Faizan
显示剩余2条评论
3个回答

5

Kubernetes 文档指出:

卷不能挂载到其他卷上,也不能与其他卷有硬链接。

我遇到了同样的问题,在我这种情况下,问题在于两个卷挂载具有重叠的 mountPath,即两个都以 /var/ 开始。

修复后它们在没有问题地挂载了。


4
我并没有看到任何直接的问题导致了上述行为。但是我建议您尝试使用“Deployment”而不是像许多人在这里建议的那样使用“Pod”,特别是在使用PV和PVC时。Deployment会处理许多事情以保持“期望状态”。 我已经附上了我的代码供您参考,它可以工作,并且即使在删除/终止/重新启动后,两个卷也仍然是持久的,因为这由Deployment的期望状态管理。
我的代码与您的代码有两个不同之处:
1. 我使用的是deployment对象而不是pod。 2. 我正在使用GlusterFs作为我的卷。
Deployment yml。
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
  namespace: platform
  labels:
    component: nginx
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        component: nginx
    spec:
      nodeSelector:
        role: app-1
      containers:
        - name: nginx
          image: vip-intOAM:5001/nginx:1.15.3
          imagePullPolicy: IfNotPresent
          volumeMounts:
          - mountPath: "/etc/nginx/conf.d/"
            name: nginx-confd
          - mountPath: "/var/www/"
            name: nginx-web-content
      volumes:
      - name: nginx-confd
        persistentVolumeClaim:
          claimName: glusterfsvol-nginx-confd-pvc
      - name: nginx-web-content
        persistentVolumeClaim:
          claimName: glusterfsvol-nginx-web-content-pvc

我的PV项目之一

apiVersion: v1
kind: PersistentVolume
metadata:
  name: glusterfsvol-nginx-confd-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  glusterfs:
    endpoints: gluster-cluster
    path: nginx-confd
    readOnly: false
  persistentVolumeReclaimPolicy: Retain
  claimRef:
    name: glusterfsvol-nginx-confd-pvc
    namespace: platform

以上的 PVC

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: glusterfsvol-nginx-confd-pvc
  namespace: platform
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

0
一个可能的原因是两个PV中的volumeHandle相同,这将导致无法将两个PV都挂载到同一个pod中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接