K8s中的volumes-容器数据存放类型及位置


学习对象:kubectl explain pod.spec.volumespod.spec.containers.image.volumeMounts

emptyDir
  • gitRepo
  • hostPath
  • nfs
  • iscsi
  • EmptyDir

    emptyDir卷对于在同一个pod中运行的容器之间共享文件特别有用。但是它用于将数据临时写入磁盘,当删除pod时卷的数据就会丢失。

    使用emptyDir

    spec:
      containers:
      - image: luksa/fortune
        name: html-generator
        volumeMounts:
        - name: html
          mountPath: /var/htdocs
      volumes:
      - name: html
        emptyDir: {}
    

    使用emptyDir创建的volume实际是在节点磁盘上创建的,我们可以将这个暂时存在的volume(tmfs)创建在内存上:

      volumes:
      - name: html
        emptyDir: {}
          medium: Memory
    

    HostPath:访问节点主机上的文件

    HostPath卷需要和节点绑定,hostPath中的路径是Pod创建节点的绝对路径;

    Pod删除后该路径下的数据不会被删除;

    apiVersion: v1
    kind: Pod
    metadata:
      name: hostpath-pod
    spec:
      nodeSelector: 
        kubernetes.io/hostname: nodename
      containers:
      - image: hub-mirror.c.163.com/library/busybox
        name: busybox
        args:
        - /bin/sh
        - -c
        - sleep 10; touch /tmp/healthy; sleep 30000
        volumeMounts:
        - name: test
          mountPath: /data/test
      volumes:
      - name: test
        hostPath:
          path: /home/rancher/test
    

    NFS

    这个和NFS的StorageClass不同,这种会将nfs的整个资源池挂载在pod里,而不是在里面创建一个文件夹给pod使用的这种形式。

    spec:
        containers:
        - name: php
          image: php:7.0-apache
          volumeMounts:
          - mountPath: /var/www/html/index.php
            name: index
            subPath: index.php
    

    ISCSI

    建议先使用:kubectl explain pod.spec.volumes.iscsi

    spec:
      volumes:
      - name: iscsipd-rw
        iscsi:
          targetPortal: 10.0.2.15:3260
          portals: ['10.0.2.16:3260', '10.0.2.17:3260']			 # 没有多个可以省略
          iqn: iqn.2001-04.com.example:storage.kube.sys1.xyz # iscsi target端的iqn
          lun: 0
          fsType: ext4
          readOnly: true