hostPath类型则是映射node文件系统中的文件或者目录到pod里。在使用hostPath类型的存储卷时,也可以设置type字段,支持的类型有文件、目录、File、Socket、CharDevice和BlockDevice。

hostPath类似于docker -v参数,将宿主主机中的文件挂载pod中,但是hostPath比docker -v参数更强大,(Pod调度到哪个节点,则直接挂载到当前节点上)

应用实例:

[root@k8s-master1 hostPath]# cat hostPath-2.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: hostpath
  namespace: app
spec:
  selector:
    matchLabels:
      app: hostpath
  template:
    metadata:
      labels:
        app: hostpath
    spec:
      containers:
        - name: nginx
          image: nginx
          volumeMounts:
            - mountPath: /root  # 本地必须存在的目录
              name: hostpath-name
      volumes:
        - name: hostpath-name
          hostPath:
            path: /root # 随机在node节点创建
            type:  DirectoryOrCreate # 类型:没有就创建,有就不创建

[root@k8s-master1 hostPath]# kubectl apply -f hostPath-2.yaml
deployment.apps/hostpath created
[root@k8s-master1 hostPath]# kubectl get pods -n app -o wide
NAME                          READY   STATUS              RESTARTS   AGE    IP              NODE          NOMINATED NODE   READINESS GATES
hostpath-6857454677-ldntx     0/1     ContainerCreating   0          7s     <none>          k8s-node1     <none>           <none>
nginx                         1/1     Running             0          124m   10.10.135.202   k8s-master3   <none>           <none>
with-affinity-anti-affinity   1/1     Running             0          38m    10.10.135.205   k8s-master3   <none>           <none>

#查看容器root目录下的文件和该node节点上宿主机的文件一致,完成文件的映射
[root@k8s-master1 hostPath]# kubectl exec -it hostpath-6857454677-ldntx -n app sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# cd /root
#
#
# ls -ltr
total 941012
-rw-------. 1 root root      1260 Oct 13 18:52 anaconda-ks.cfg
-rw-r--r--. 1 root root 383673856 Oct 17 17:27 calico_img.tar
-rw-r--r--  1 root root  49820672 Oct 18 12:44 coredns.tar
drwxr-xr-x  3 root root        53 Nov 18 14:47 python3
-rw-r--r--  1 root root   2116242 Nov 18 15:22 psutil-master.zip
drwxr-xr-x  3 root root        67 Nov 18 15:22 test
-rw-------  1 root root 527973888 Nov 24 07:32 zookeeper.tar
drwxr-xr-x  3 root root        40 Nov 28 13:22 charts
drwxr-xr-x  4 root root        93 Nov 28 13:25 mychart
drwxr-xr-x  4 root root        93 Nov 28 13:32 tomcat-mychart

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐