Kubernetes - volume

 

 

 

 emptyDir示例:

apiVersion: v1
kind: Pod
metadata:
  name: pod-emptydir
  namespace: default
spec:
  containers:
  - name: myapp-pod
    image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  - name: busybox-pod
    image: registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh", "-c", "sleep 3600"]
    volumeMounts:
    - mountPath: /test/cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}

kubectl apply -f pod.yaml

 

 

两个container都可以更新目录下的文件,新建一个文件,分别进入两个容器中,验证下

kubectl exec -it pod-emptydir -c myapp-pod sh

kubectl exec -it pod-emptydir -c busybox-pod sh

 

 

 

 重新进入myapp-pod,文件已经更新

 

 

hostpath

 

 

 

 

 

示例:

apiVersion: v1
kind: Pod
metadata:
  name: pod-hostpath
  namespace: default
spec:
  containers:
  - name: myapp-pod
    image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: hostpath-dir-volume
      mountPath: /test-k8s/hostpath-dir
    - name: hostpath-file-volume
      mountPath: /test/hostpath-file/test.conf
  volumes:
  - name: hostpath-dir-volume
    hostPath:
      # 宿主机目录
      path: /k8s/hostpath-dir
      # hostPath 卷指定 type,如果目录不存在则创建(可创建多层目录)
      type: DirectoryOrCreate
  - name: hostpath-file-volume
    hostPath:
      path: /k8s2/hostpath-file/test.conf
      # 如果文件不存在则创建。 前提:文件所在目录必须存在  目录不存在则不能创建文件
      type: FileOrCreate

kubectl exec -it pod-hostpath sh

新增了一个index.html,并且在宿主机上也可以查看

 

posted @ 2021-07-19 17:25  大川哥  阅读(38)  评论(0编辑  收藏  举报