Kubernetes

version: 1.0.0

Built with Teadocs

7-3.Kubernetes - volume

# 3.Kubernetes - volume

# 1.emptyDir

创建volumes-emptyDir.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: hub.gerrywen.com/library/myapp:v2
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  - image: hub.gerrywen.com/library/myapp:v1
    name: liveness-exec-container
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","sleep 6000s"]
    volumeMounts:
    - mountPath: /test
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}

查看pod运行情况

kubectl get pod -o wide
kubectl describe pod test-pd

执行命令进入pod

kubectl exec test-pd -c test-container -it -- /bin/sh
/ # cd /cache/
/cache # ls
/cache # touch index.html
/cache # date > index.html 
/cache # cat index.html 
Wed May 27 23:43:24 UTC 2020
Wed May 27 23:43:32 UTC 2020
kubectl exec test-pd -c liveness-exec-container -it -- /bin/sh
/ # cd /test/
/test # ls
index.html
/test # date >> index.html 
/test # cat index.html 
Wed May 27 23:43:24 UTC 2020
Wed May 27 23:43:32 UTC 2020

# 2.hostPath

hostPath 卷将主机节点的文件系统中的文件或目录挂载到集群中

创建volumes-hostPath.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: hub.gerrywen.com/library/myapp:v1
    name: liveness-exec-container
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","sleep 6000s"]
    volumeMounts:
    - mountPath: /test-pd
      name: cache-volume
  volumes:
  - name: cache-volume
    hostPath:
      path: /data
      type: DirectoryOrCreate
kubectl exec -it test-pd -it -- /bin/sh