使用Kubernetes进行多个Volume挂载:一种可行,一种不可行 [英] Multiple Volume mounts with Kubernetes: one works, one doesn't

查看:597
本文介绍了使用Kubernetes进行多个Volume挂载:一种可行,一种不可行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用单个容器创建一个Kubernetes容器,该容器上装有两个外部卷.我的.yml Pod文件是:

I am trying to create a Kubernetes pod with a single container which has two external volumes mounted on it. My .yml pod file is:

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

I am using Persistent Volumes and Persisten Volume Claims, as such: 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时,其中一条路径已正确安装(因此是永久性的),另一条路径正在擦除...

I have initially created my disks using the command: $ gcloud compute disks create --size 250GB pd-disk Same goes for the second disk and second PV and PVC. Everything seems to work ok when I create the pod, no errors are thrown. Now comes the weird part: one of the paths is being mounted correctly (and is therefor persistent) and the other one is being erased every time I restart the pod...

我尝试重新创建所有内容,但没有任何变化.另外,从pod描述中,两个卷似乎都已正确安装:

I have tried re-creating everything from scratch, but nothing changes. Also, from the pod description, both volumes seem to be correctly mounted:

$ 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

感谢您的帮助.谢谢.

推荐答案

我没有看到发生上述行为的任何直接问题!但是,我想请您尝试的是使用部署"而不是许多部署会照顾很多事情,以保持所需"状态".我在下面附上了我的代码,以供您参考,并且即使在删除/终止/重新启动之后,这两个卷也是永久的,因为这是由Deployment的所需状态管理的.

I do not see any direct problem for which such behavior as explained above has occurred! But what I can rather ask you to try is to use a "Deployment" instead of a "Pod" as suggested by many here, especially when using PVs and PVCs. Deployment takes care of many things to maintain the "Desired State". I have attached my code below for your reference which works and both the volumes are persistent even after deleting/terminating/restarting as this is managed by the Deployment's desired state.

您会在我的代码中发现与您的代码不同的两个地方:

Two difference which you would find in my code from yours are:

  1. 我有一个部署对象而不是pod
  2. 我正在使用GlusterFs作为音量.

部署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

这篇关于使用Kubernetes进行多个Volume挂载:一种可行,一种不可行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆