Kubernetes--Volume存储卷实践

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

你可能感兴趣的:(kubernetes)