Kubernetes Certification Exam Self-Study Series | Use of Data Volumes

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


After the container is created, the container will be mapped to a certain directory on the physical machine, so as long as the container is not deleted, the data written in the container will always exist. But once the container is deleted, the corresponding container layer will also be deleted.

If you want the data to be saved permanently, you need to configure the data volume and mount the specified directory in the container to a directory on the physical machine, as shown in Figure 1-16.

Here, the directory aa in the container is cut to the bb directory of the physical machine. When data is written to the container directory aa, it is actually written to the directory bb of the physical machine. In this way, even if the container is deleted, the data in the physical machine directory bb still exists, and the permanent retention of the data is realized (unless manually deleted).

When creating a container, use -v to specify the data volume, and the usage is as follows.

-v /dir1: The directory /var/lib/docker/volumes/ID/_data/ of the physical machine will be mounted to the /dir1 directory of the container, where the ID is randomly generated.

-v /dir2:/dir1: The specified directory /dir2 in the physical machine is mapped to the /dir1 directory of the container.

Remember, -v /dir2 is the directory of the physical machine, and dir1 is the directory in the container. If these two directories do not exist, they will be created automatically when the container is created.

Step 1: Create container c1, and mount a random directory on the physical machine to the /data directory of the container.

[root@vms100 ~]# docker run -dit --name=c1 --restart=always -v /data hub.c.163.com/library/centos
5e7b70be7dbbb106f7c4648a5aea8f61fa52e877d6f19669b8fad3ec9e9ed93f
[root@vms100 ~]#
复制代码

In this command, only one directory /data/ is specified after -v, which refers to creating /data in the container and mounting a random directory in the physical machine.

Step 2: Check which directory on the physical machine the directory /data in the container corresponds to.

[root@vms100 ~]# docker inspect c1 | grep -A5 Mounts
  "Mounts": [
    {
      "Type": "volume",
      "Name": "3b9d162e61790b76d3fb3353672ca760f6ea369881bf952bf48939ed76d0d531",
      "Source": "/var/lib/docker/volumes/3b9d162e61790b76d3fb3353672ca760f6ea369881bf952bf48939ed76dod531/_data",
      "Destination": "/data",
[root@vms100 ~]#
复制代码

There are two parameters above, where Destination refers to the directory in the container, and Source refers to the directory corresponding to the physical machine.

Copy a file into the container.

[root@vms100 ~]# docker exec c1 ls /data 
[root@vms100 ~]# ls /var/lib/docker/
volumes/3b9d162e61790b76d3fb3353672ca760f6ea369881bf952bf48939ed76d0d531/_data 
[root@vms100 ~]#
复制代码

You can see that the directory is empty.

[root@vms100 ~]# docker cp /etc/hosts c1:/data
[root@vms100 ~]# docker exec c1 ls/data 
hosts
[root@vms100 ~]# ls /var/lib/docker/
volumes/3b9d162e61790b76d3fb3353672ca760f6ea369881bf952bf48939ed76d0d531/_data 
[root@vms100 ~]# 
复制代码

Step 3: Delete this container.

[root@vms100 ~]# docker rm -f c1
c1
[root@vms100 ~]#
复制代码

If you want to specify a directory instead of a random directory in the physical machine, the usage is -v /xx:/data, where the directory before the colon is the directory of the physical machine, and the directory after the colon is the directory in the container.

Step 4: Create container c1, and map the directory /xx of the physical machine to the /data directory of the container.

[root@vms100 ~]# docker run -dit --name=c1 --restart=always -v /xx:/data hub.c.163.com/library/centos 
a02739b678d21b0994fb06d9d65c9a1417a145ba992db57606be07d28208334e 
[root@vms100 ~]#
复制代码

Check out this container properties.

[root@vms100 ~]# docker inspect c1 | grep -A5 Mounts
  "Mounts": [
    {
      "Type": "bind",
      "Source": "/xx",
      "Destination": "/data",
      "Mode": "",
[root@vms100 ~]#
复制代码

Step 5: Copy some test files to observe.

[root@vms100 ~]# docker exec c1 ls/data 
[root@vms100 ~]# ls /xx #两个都是空的
[root@vms100 ~]# docker cp /etc/hosts c1:/data #往容器的/data里拷贝一个文件
[root@vms100 ~]# docker exec c1 ls/data 
hosts
[root@vms100 ~]# ls /xx/ #物理机的目录/xx里也有了这些数据
hosts
[root@vms100 ~]#
复制代码

Step 6: Delete this container.

[root@vms100 ~]# docker rm -f c1
c1
[root@vms100 ~]#
复制代码

Just now when creating the specified volume of the container, it was written like this: -v /xx:/data, in fact, a default option rw is hidden here, that is, the complete writing method is -v /xx:data:rw, that is, in the container / data is to mount the /xx directory of the physical machine in rw mode, and the volume can be mounted in ro (read-only) mode.

Step 7: Set the volume as read-only when creating the container.

[root@vms100 ~]# docker run -dit --name=c1 --restart=always -v /xx:/data:ro hub.c.163.com/library/centos
a593c19d7cc47d6d7f1514c806cc056b1d6d5aa01956c06e4faa4baab0256139
[root@vms100 ~]#
复制代码

At this point, copy a piece of data to the container.

[root@vms100 ~]# docker cp /etc/hosts c1:/data
Error response from daemon: mounted volume is marked read-only
[root@vms100 ~]#
复制代码

It cannot be copied, because the /xx directory of the physical machine is now mounted in the form of ro.

Step 8: Delete this container.

[root@vms100 ~]# docker rm -f c1
c1
[root@vms100 ~]#

 

Guess you like

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