host_path.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- name: busybox1
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]
volumeMounts:
- mountPath: /test-pd
name: test-volume
- name: busybox2
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
hostPath:
# directory location on host
path: /data
# this field is optional
type: Directory
首选在ubutu-129和130创建目录,/data,用于挂载
kubectl create -f host_path.yaml
root@ubuntu-128:/home/itcast/working/volume# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-5896fbb489-2vp6n 1/1 Running 1 24h
nginx-deployment-5896fbb489-snj49 1/1 Running 1 24h
test-pd 2/2 Running 0 6m15s
成功运行pod,且pod运行在129node节点上
root@ubuntu-128:~# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-5896fbb489-2vp6n 1/1 Running 1 24h 10.244.1.7 ubuntu-130 <none> <none>
nginx-deployment-5896fbb489-snj49 1/1 Running 1 24h 10.244.2.18 ubuntu-129 <none> <none>
test-pd 2/2 Running 0 114s 10.244.2.20 ubuntu-129 <none> <none>
登录container,发现容器的目录成功挂载到了本地磁盘/data目录
root@ubuntu-128:/home/itcast/working/volume# kubectl exec test-pd -it sh
Defaulting container name to busybox1.
Use 'kubectl describe pod/test-pd -n default' to see all of the containers in this pod.
/ # ls
bin dev etc home proc root sys test-pd tmp usr var
/ # cd test-pd/
/test-pd # ll
sh: ll: not found
/test-pd # ls
test
/test-pd # more test
aaaaa
/test-pd # ifconfig
eth0 Link encap:Ethernet HWaddr 0A:58:0A:F4:02:14
inet addr:10.244.2.20 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1
RX packets:30 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3489 (3.4 KiB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/test-pd # ls
test
/test-pd # more test
aaaaa
666666666
root@ubuntu-129:/data# more test
aaaaa
666666666