Continuous integration deployment-k8s-configuration and storage-configuration management: use of HostPath

Continuous integration deployment-k8s-configuration and storage-configuration management: use of HostPath

1 Introduction

In Kubernetes,HostPath is a volume type used to mount files or directories on the host into a container. Using the HostPath volume type allows you to access files or directories on the host from inside the container.

Specifically, when you define the volume type in Pod, will Create the specified directory on the host and mount it into the container. The location of this directory is specified by you, usually an existing directory or file on the host machine. Inside the container, you can access this mounted directory or file just like a normal file system. HostPathKubernetes

Mount the files or directories on the node to the Pod. At this time, the directory will become a persistent storage directory. Even if the Pod is deleted and restarted, it can be reloaded to this directory and the files in this directory will not be lost.

2. Create a Pod and mount the directory using HostPath

Create a new file: volume-test-pd.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-volume-pd
spec:
  containers:
  - image: nginx
    name: nginx-volume
    volumeMounts:
    - mountPath: /test-pd # 挂载到容器的哪个目录
      name: test-volume # 挂载哪个 volume
  volumes:
  - name: test-volume
    hostPath: # 与主机共享目录,加载主机中的指定目录到容器中
      path: /data # 节点中的目录
      type: DirectoryOrCreate # 检查类型,在挂载前对挂载目录做什么检查操作,有多种选项,默认为空字符串,不做任何检查

A new Nginx Pod and a Volume named test-volume are created here, and the configuration is hostPath, which corresponds to < a i=3> is the directory on the host machine. path

Among them type types support the following types:

  • Empty string: Default type, no checking
  • DirectoryOrCreate: If the given path does not exist, create an empty directory 755
  • Directory: This directory must exist
  • FileOrCreate: If the given file does not exist, create an empty file with permissions 644
  • File: This file must exist
  • Socket: UNIX socket, must exist
  • CharDevice: character device, must exist
  • BlockDevice: block device, must exist

When the type verification is not satisfied, an error will be reported when creating a Pod.

Then create Pod:kubectl create -f volume-test-pd.yaml

[root@docker-54 volumes]# kubectl get po
NAME                           READY   STATUS              RESTARTS          AGE
dns-test                       1/1     Running             1 (41d ago)       41d
fluentd-p2znm                  1/1     Running             0                 39d
fluentd-qx22q                  1/1     Running             0                 39d
nginx-deploy-bcc5c945c-988wb   1/1     Running             44 (4m9s ago)     44h
nginx-deploy-bcc5c945c-ptqc6   1/1     Running             148 (4m39s ago)   6d4h
private-image-pull-pod         0/1     Completed           0                 5d21h
test-configfile-po             0/1     Completed           0                 20d
test-volume-pd                 0/1     ContainerCreating   0                 7s
[root@docker-54 volumes]# kubectl get po -o wide
NAME                           READY   STATUS      RESTARTS          AGE     IP             NODE        NOMINATED NODE   READINESS GATES
dns-test                       1/1     Running     1 (41d ago)       41d     10.244.1.35    docker-55   <none>           <none>
fluentd-p2znm                  1/1     Running     0                 39d     10.244.1.44    docker-55   <none>           <none>
fluentd-qx22q                  1/1     Running     0                 39d     10.244.2.62    docker-56   <none>           <none>
nginx-deploy-bcc5c945c-988wb   1/1     Running     44 (4m53s ago)    44h     10.244.1.60    docker-55   <none>           <none>
nginx-deploy-bcc5c945c-ptqc6   1/1     Running     148 (5m23s ago)   6d4h    10.244.2.96    docker-56   <none>           <none>
private-image-pull-pod         0/1     Completed   0                 5d21h   10.244.2.98    docker-56   <none>           <none>
test-configfile-po             0/1     Completed   0                 20d     10.244.2.71    docker-56   <none>           <none>
test-volume-pd                 1/1     Running     0                 51s     10.244.2.113   docker-56   <none>           <none>
[root@docker-54 volumes]# 
[root@docker-54 volumes]# 

You can see that this test-volume-pd is running on my docker-56 node.

Connect to the docker-56 node, go to the /data directory, and create a new file: index.html

Then enter the Pod container to see if this file exists:

[root@docker-54 volumes]# kubectl exec -it test-volume-pd -- sh
# ls
bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  test-pd  tmp  usr  var
# cd /data
sh: 2: cd: can't cd to /data
# cd /test-pd
# ls
index.html
# 

As you can see, after we enter the container, we go directly to the/data directory and find that the directory does not exist. This is because this directory is the directory of the host machine and is mapped to ours. The path inside the container is /test-pd, and this index.html file does exist.

Then we add some content to this index.html in the container:

# echo 'Hello Volume, HostPath....' > index.html
# ls
index.html
# cat index.html  
Hello Volume

Then return to the host docker-56 node and look at this file:

[root@docker-56 data]# touch index.html
[root@docker-56 data]# 
[root@docker-56 data]# cat index.html 
Hello Volume, HostPath....
[root@docker-56 data]# 

It can be seen that the latest content of the file does exist on the host machine.

Then we modify the content of this fileindex.html on the host, and then go back to the container to take a look. We find that after the modification on the host, it can also be seen immediately in the container.

With this feature, we can share files on the host with our container.

Guess you like

Origin blog.csdn.net/linmengmeng_1314/article/details/134614796