volume

存储卷(volume)是pod的一部分, 和pod共享相同的生命周期。 在容器重新启动期间, 卷的内容将保持不变。

如果一个 pod包含多个容器,这个卷可以同时被所有的容器使用。

emptyDir

用于存储临时数据的简单空目录。卷从一个空目录开始,同一个 pod 中运行的容器之间共享文件特别有用。 多个容器共享相同的目录, 通常其中一个容器写入,另外一个容器读取。

当删除 pod 时, 卷的内容就会丢失。

pod示例

apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
        image: redis
        volumeMounts:
        - name: redis-storage
          mountPath: /data/redis
  volumes:
  - name: redis-storage
        emptyDir: {}

hostpath

hostPath 卷能将主机节点文件系统上的文件或目录挂载到您的 Pod 中。

hostpath的内容持久存储。在pod删除时,其存储的内容不会发生变化。

有path指定目录或文件名,还可以选择指定hostPath卷的(type)类型。type 支持的值为:

  • DirectoryOrCreate,如果给定路径上不存在任何内容,则将根据需要在该目录中创建一个空目录, 并将权限设置为0755,该目录与Kubelet具有相同的组和所有权。

  • Directory,该目录必须已经存在。

  • File 文件必须位于给定路径

myconfig默认不存在,因此需要设置type为: DirectoryOrCreate

name: myconfig
    hostPath:
          path: /etc/myconfig/
          type: DirectoryOrCreate

系统pod有使用hostpath的情况,例如etcd, 查看其详细信息

[root@zhang1 k8s]# kubectl describe po etcd-zhang1 --namespace kube-system
Name:                 etcd-zhang1
Namespace:            kube-system
Priority:             2000001000
Priority Class Name:  system-node-critical
Node:                 zhang1/192.168.101.180
Start Time:           Wed, 04 Nov 2020 20:34:53 -0500
Labels:               component=etcd
                                          tier=control-plane
Annotations:          kubeadm.kubernetes.io/etcd.advertise-client-urls: https://192.168.101.180:2379
                                          kubernetes.io/config.hash: ba7b8ddccb06cde3ca0adea85cebb6c5
                                          kubernetes.io/config.mirror: ba7b8ddccb06cde3ca0adea85cebb6c5
                                          kubernetes.io/config.seen: 2020-10-30T04:07:08.972112527-04:00
                                          kubernetes.io/config.source: file
Status:               Running
IP:                   192.168.101.180
IPs:
  IP:           192.168.101.180
Controlled By:  Node/zhang1
Containers:
  etcd:
        Container ID:  docker://a90c1abaaa07eb03cae476aa1ef5183ad0f187a3f601b21735a9107cbb5faf66
        Image:         registry.aliyuncs.com/google_containers/etcd:3.4.13-0
        Image ID:      docker-pullable://registry.aliyuncs.com/google_containers/etcd@sha256:bd4d2c9a19be8a492bc79df53eee199fd04b415e9993eb69f7718052602a147a
        Port:          <none>
        Host Port:     <none>
        Command:
          etcd
          --advertise-client-urls=https://192.168.101.180:2379
          --cert-file=/etc/kubernetes/pki/etcd/server.crt
          --client-cert-auth=true
          --data-dir=/var/lib/etcd
          --initial-advertise-peer-urls=https://192.168.101.180:2380
          --initial-cluster=zhang1=https://192.168.101.180:2380
          --key-file=/etc/kubernetes/pki/etcd/server.key
          --listen-client-urls=https://127.0.0.1:2379,https://192.168.101.180:2379
          --listen-metrics-urls=http://127.0.0.1:2381
          --listen-peer-urls=https://192.168.101.180:2380
          --name=zhang1
          --peer-cert-file=/etc/kubernetes/pki/etcd/peer.crt
          --peer-client-cert-auth=true
          --peer-key-file=/etc/kubernetes/pki/etcd/peer.key
          --peer-trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
          --snapshot-count=10000
          --trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
        State:          Running
          Started:      Wed, 04 Nov 2020 20:34:54 -0500
        Last State:     Terminated
          Reason:       Completed
          Exit Code:    0
          Started:      Tue, 03 Nov 2020 20:53:58 -0500
          Finished:     Wed, 04 Nov 2020 05:43:53 -0500
        Ready:          True
        Restart Count:  10
        Liveness:       http-get http://127.0.0.1:2381/health delay=10s timeout=15s period=10s #success=1 #failure=8
        Startup:        http-get http://127.0.0.1:2381/health delay=10s timeout=15s period=10s #success=1 #failure=24
        Environment:    <none>
        Mounts:
          /etc/kubernetes/pki/etcd from etcd-certs (rw)
          /var/lib/etcd from etcd-data (rw)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  etcd-certs:
        Type:          HostPath (bare host directory volume)
        Path:          /etc/kubernetes/pki/etcd
        HostPathType:  DirectoryOrCreate
  etcd-data:
        Type:          HostPath (bare host directory volume)
        Path:          /var/lib/etcd
        HostPathType:  DirectoryOrCreate
QoS Class:         BestEffort
Node-Selectors:    <none>
Tolerations:       :NoExecuteop=Exists
Events:            <none>

pod使用两个hostPath卷来访问节点的 /etc/kubernetes/pki/etcd 和 /var/lib/etcd 目 录。

hostpath也可以存放节点的日志文件。

参考资料

https://kubernetes.io/docs/tasks/configure-pod-container/configure-volume-storage/