微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

k8s 数据卷hostPath卷

k8s-数据卷hostPath卷

1. 数据卷hostPath卷

  • hostPath卷:挂载Node节点本地文件系统(Pod所在节点)上文件或者目录到Pod中的容器。

  • 应用场景:Pod中容器需要访问宿主机文件

  • 示例:

    apiVersion: v1
    kind: Pod
    Metadata:
      name: my-hostpath
    spec:
      containers: 
      - name: busyBox
        image: busyBox
        args:
        - /bin/sh
        - -c
        - sleep 36000
        volumeMounts:
        - name: data
          mountPath: /data
      volumes:
      - name: data
        hostPath:
          path: /tmp
          type: Directory
    

    ​ 示例:将宿主机/tmp目录挂载到容器/data目录

2. 案例

2.1 编写配置文件

  • 创建编写配置文件目录

    [root@k8s-master yaml]# mkdir -p hostPath/
    [root@k8s-master yaml]# cd hostPath/
    [root@k8s-master hostPath]# ll
    总用量 0
    
  • 编写配置文件

    [root@k8s-master hostPath]# vim hostpath.yaml
    [root@k8s-master hostPath]# cat hostpath.yaml
    apiVersion: v1
    kind: Pod
    Metadata:
      name: my-hostpath
    spec:
      containers: 
      - name: busyBox
        image: busyBox
        args:
        - /bin/sh
        - -c
        - sleep 36000
        volumeMounts:
        - name: data
          mountPath: /data
      volumes:
      - name: data
        hostPath:
          path: /tmp
          type: Directory
    

2.2 启动服务

[root@k8s-master hostPath]# kubectl apply -f hostpath.yaml 
pod/my-hostpath created

2.3 查看服务是否启动

[root@k8s-master hostPath]# kubectl get pods -o wide
NAME                 READY   STATUS    RESTARTS   AGE     IP               NODE        NOMINATED NODE   READInesS GATES
configmap-demo-pod   1/1     Running   0          2d      10.244.107.209   k8s-node3   <none>           <none>
my-hostpath          1/1     Running   0          4m28s   10.244.107.211   k8s-node3   <none>           <none>
secret-demo-pod      1/1     Running   0          41h     10.244.107.210   k8s-node3   <none>           <none>

2.4 验证数据

我们可以看到节点在node3上面,我们去node3节点/tmp/目录下载创建一个文件做测试”test.txt“

  • 在node3上/tmp/目录下创建”test.txt“文件

    [root@k8s-node3 ~]# cd /tmp/
    [root@k8s-node3 tmp]# ls
    [root@k8s-node3 tmp]# touch test.txt
    [root@k8s-node3 tmp]# ls
    test.txt
    
  • 进入docker容器,验证是否被引入

    [root@k8s-master hostPath]# kubectl exec -it my-hostpath -- /bin/sh 
    / # ls  /data/
    test.txt
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐