Kubernetes Certification Exam Self-Study Series | hostPath

Book source: "CKA/CKAD Test Guide: From Docker to Kubernetes Complete Raiders"

Organize the reading notes while studying, and share them with everyone. If the copyright is violated, it will be deleted. Thank you for your support!

Attach a summary post: Kubernetes certification exam self-study series | Summary_COCOgsta's Blog-CSDN Blog


Using the storage method of hostPath is similar to the command docker run -v /data:/xx when creating a docker container, which means that the directory /data in the physical machine is mapped to the /xx directory of the container. If the pod is deleted , the data is still retained.

Step 1: Create a pod yaml file host.yaml with the following content.

[root@vms10 volume]# cat host.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: demo
  labels:
    purpose: demonstrate-envars 
spec:
  volumes:
  - name: volume1
    hostPath:
      path: /data 
  containers:
  - name: demo1
    image: busybox 
    imagePullPolicy: IfNotPresent 
    command: ['sh', '-c', 'sleep 5000']
    volumeMounts: 
    - mountPath: /xx 
      name: volume1
 
![image.png](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/bfb4165679264e149a846eeab8cb63b8~tplv-k3u1fbpfcp-watermark.image?)
  - name: demo2
    image: busybox 
    imagePullPolicy: IfNotPresent 
    command: ['sh', '-c', 'sleep 5000']
    volumeMounts:
    - mountPath: /xx 
      name: volume1
[root@vms10 volume]#

In this yaml file, a volume named volume1 is created under the volumes field, and the type is hostPath, which corresponds to the directory /data of the physical machine (specified by path).

Two containers are defined, and volumeMounts is used in each container to mount the volume volume1 to the /xx directory of the container (specified by mount Path), so the directory /xx in each container is mounted to the /data directory of the physical machine .

Step 2: Create and view pods.

[root@vms10 volume]# kubectl apply -f host.yaml 
pod/demo created
[root@vms10 volume]# kubectl get pods
NAME   READY   STATUS  RESTARTS  AGE 
demo   2/2    Running   0        11s 
[root@vms10 volume]#

Step 3: Check the properties of the pod to confirm that HostPath is being used now.

[root@vms10 volume]# kubectl describe pod demo | grep -A3 Volumes 
Volumes:
  volume1:
    Type:     HostPath (bare host directory volume)
    Path:     /data 
[root@vms10 volume]#

As can be seen from the above, /xx in the pod mounts the /data directory of the physical machine.

Step 4: Check the machine where the pod is located.

[root@vms10 volume]# kubectl get pods -o wide 
NAME   READY   STATUS    RESTARTS     AGE     IP             NODE 
demo   2/2     Running   0            3m14s   10.244.3.31    vms12.rhce.cc 
[root@vms10 volume]#

You can see that the pod is running on vms12.

Step 5: Copy a file on the master to the /xx directory of the demo1 container in this pod.

[root@vms10 volume]# kubectl cp /etc/hosts demo:/xx -c demo1
[root@vms10 volume]#

Step 6: Switch to vms12 and check whether the file is placed in the /data directory of vms12.

[root@vms12 ~]#
[root@vms12 ~]# ls /data/
hosts
[root@vms12 ~]#

Step 7: Delete this pod.

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/130722743