Kubernetes卷挂载与复制控制器

时间:2016-02-25 12:56:04

标签: docker kubernetes mount

为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个pod之间进行音量安装。我正在使用2个不同的复制控制器创建这些pod。复制控制器看起来像这样

复制控制器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卷用于此场景吗?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

为什么要共享pod之间的卷装?这将无法可靠地工作,因为您无法保证在群集中的复制控制器1和复制控制器2中的容器之间具有1:1映射。

如果要在容器之间共享本地存储,则应将两个容器放在同一个容器中,并让每个容器装入emptyDir卷。

答案 2 :(得分:0)

您需要 三个 才能使其正常运行。可以找到更多信息here和一些文档here,但最初有点令人困惑。

此示例安装NFS卷。

<强> 1。创建指向NFS服务器的PersistentVolume

file:mynfssharename-pv.yaml

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

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

kubectl create -f mynfssharename-pv.yaml

<强> 2。创建PersistentVolumeClaim以指向PersistentVolume mynfssharename

file:mynfssharename-pvc.yaml

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

kubectl create -f mynfssharename-pvc.yaml

第3。将声明添加到ReplicationController或部署

{{1}}