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
    
posted @   七月流星雨  阅读(2102)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
点击右上角即可分享
微信分享提示