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