带有复制控制器的 Kubernetes 卷挂载

Kubernetes Volume Mount with Replication Controllers

为 Kubernetes EmptyDir 卷找到此示例

apiVersion: v1
kind: Pod
metadata:
  name: www
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /srv/www
      name: www-data
      readOnly: true
  - name: git-monitor
    image: kubernetes/git-monitor
    env:
    - name: GIT_REPO
      value: http://github.com/some/repo.git
    volumeMounts:
    - mountPath: /data
      name: www-data
  volumes:
  - name: www-data
    emptyDir: {}

我想在 2 pods 之间挂载卷。我正在使用 2 个不同的复制控制器创建这些 pods。复制控制器看起来像这样

复制控制器 1:

apiVersion: v1
kind: ReplicationController
metadata:
  name: node-worker
  labels:
    name: node-worker
spec:
  replicas: 1
  selector:
    name: node-worker
  template:
    metadata:
      labels:
        name: node-worker
    spec:
      containers:
      -
        name: node-worker
        image: image/node-worker
        volumeMounts:
          - mountPath: /mnt/test
            name: deployment-volume
      volumes:
        - name: deployment-volume
          emptyDir: {}

复制控制器 2:

apiVersion: v1
    kind: ReplicationController
    metadata:
      name: node-manager
      labels:
        name: node-manager
    spec:
      replicas: 1
      selector:
        name: node-manager
      template:
        metadata:
          labels:
            name: node-manager
        spec:
          containers:
          -
            name: node-manager
            image: image/node-manager
            volumeMounts:
              - mountPath: /mnt/test
                name: deployment-volume
          volumes:
            - name: deployment-volume
              emptyDir: {}

Kubernetes emptyDir volume 可以用于这种场景吗?

为什么要在 pods 之间共享卷安装?这将无法可靠地工作,因为您不能保证在集群中调度复制控制器 1 和复制控制器 2 的 pods 之间有一个 1:1 映射。

如果你想在容器之间共享本地存储,你应该把两个容器放在同一个容器中,并让每个容器挂载 emptyDir 卷。

EmptyDir 卷固有地绑定到单个 pod 的生命周期,不能在复制控制器或其他方式中的 pods 之间共享。如果您想在 pods 之间共享卷,目前最好的选择是 NFS 或 gluster,在持久卷中。请在此处查看示例:https://github.com/kubernetes/examples/blob/master/staging/volumes/nfs/README.md

您需要 三个 东西才能使它正常工作。可以找到更多信息 here and some documentation here,但起初有点混乱。

此示例安装一个 NFS 卷。

1.创建指向 NFS 服务器的 PersistentVolume

文件:mynfssharename-pv.yaml

(更新服务器以指向您的服务器)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mynfssharename
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: yourservernotmine.yourcompany.com
    path: "/yournfspath"

kubectl create -f mynfssharename-pv.yaml

2。创建一个 PersistentVolumeClaim 以指向 PersistentVolume mynfssharename

文件:mynfssharename-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mynfssharename
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

kubectl create -f mynfssharename-pvc.yaml

3。将声明添加到您的 ReplicationController 或 Deployment

spec:
  containers:
  - name: sample-pipeline
    image: yourimage
    imagePullPolicy: Always
    ports:
    - containerPort: 8080
      name: http
    volumeMounts:
      # name must match the volume name below
      - name: mynfssharename
        mountPath: "/mnt"
  volumes:
  - name: mynfssharename
    persistentVolumeClaim:
      claimName: mynfssharename