Skip to content

Volume

持久化

容器的存储是容器的运行时状态,容器退出后,存储数据会丢失,容器重新启动后,数据会丢失。

emptyDir

emptyDir是kubernetes提供的一种存储类型,用于在容器之间共享数据,容器退出后,数据不会丢失,容器重新启动后,数据不会丢失。

yaml
apiVersion: v1
kind: Pod
metadata:
  name: volume-emptydir-pod
spec:
  containers:
    - name: volume-emptydir-nginx
      image: nginx
      ports:
        - containerPort: 80
      volumeMounts:
        - name: volume-emptydir-volume
          mountPath: /usr/share/nginx/logs
    - name: volume-emptydir-touch
      image: apline
      command: [ "/bin/sh", "-c", "touch /logs/hello.log && tail -f /logs/hello.log" ]
      volumeMounts:
        - name: volume-emptydir-volume
          mountPath: /logs
  volumes:
    - name: volume-emptydir-volume
      emptyDir: { }
bash
/var/lib/kubelet/pods/{pods}/volumes/kubernetes.io~empty-dir/volume-emptydir-volume

hostPath

hostPath是kubernetes提供的一种存储类型,用于在容器之间共享数据,容器退出后,数据不会丢失,容器重新启动后,数据不会丢失。

yaml
apiVersion: v1
kind: Pod
metadata:
  name: volume-hostpath-pod
spec:
  containers:
    - name: volume-hostpath-nginx
      image: nginx
      ports:
        - containerPort: 80
      volumeMounts:
        - name: volume-hostpath-volume
          mountPath: /test-pod
  volumes:
    - name: volume-hostpath-volume
      hostPath:
        path: /test
        type: Directory