乐趣区

关于kubernetes:KubernetesVolume存储卷实践

Volumne

  • emptyDir
  • hostPath
长期存储卷 emptyDir

emptyDir 随着 pod 生命周期完结而完结


$ cat vol-emptydir.yaml
apiVersion: v1
kind: Pod
metadata:
  name: vol-emptydir-pod
spec:
  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.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  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.txt
Sun Apr 19 13:49:03 UTC 2020

# 查看 k8s 节点
k8s-node$ cat /data/test.txt
Sun Apr 19 13:49:03 UTC 2020
退出移动版