当前位置:   article > 正文

K8S中hostpath的使用_k8s配置hostpath

k8s配置hostpath

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/214361
推荐阅读
相关标签
  

闽ICP备14008679号