Volumne
长期存储卷emptyDir
emptyDir随着pod生命周期完结而完结
$ cat vol-emptydir.yamlapiVersion: v1kind: Podmetadata: name: vol-emptydir-podspec: volumes: - name: html emptyDir: {} containers: - name: nginx image: nginx:1.12-alpine volumeMounts: - name: html mountPath: /usr/share/nginx/html - name: pagegen image: alpine volumeMounts: - name: html mountPath: /html command: ["/bin/sh", "-c"] args: - while true; do echo $(hostname) $(date) >> /html/index.html; sleep 10; done
# emptyDir存储卷基于RAM举例,可用来做高速缓存 volumes: - name: cache emptyDir: medium: Memory
$ kubectl create -f vol-emptydir.yaml$ kubectl delete pod vol-emptydir-pod
节点存储卷hostPath
将数据存在node节点磁盘上
# 将工作节点的目录挂载至pod中$ cat vol-hostpath.yamlapiVersion: v1kind: Podmetadata: name: test-pdspec: containers: - image: myapp:v1 name: test-container volumeMounts: - mountPath: /test-pd name: test-volume volumes: - name: test-volume hostPath: path: /data type: Directory
$ kubectl apply -f vol-hostpath.yaml# 查看pod中的容器$ kubectl exec -ti test-pd -- sh# date >> test.txt# more test.txtSun Apr 19 13:49:03 UTC 2020# 查看k8s节点k8s-node$ cat /data/test.txtSun Apr 19 13:49:03 UTC 2020