In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Due to the popularity of Docker technology, companies large and small are now using Docker. Friends who are familiar with Docker understand that DOcker containers run with a lifecycle, and if the docker container fails, it means that the data in the container will also be lost. So for enterprises, data is important! How to persist the data in the docker container is a problem that operators need to consider! This blog post will learn how to persist the data in the container!
Before you understand Docker data persistence, you need to have a simple understanding of the storage type of Docker, which can be seen by executing the following command:
[root@docker ~] # docker info / / View the details of Docker Containers: 2 / / there are several containers Running: 2 / / there are several containers that are running Paused: 0 / / suspend, Several containers Stopped: 0 / / several containers Images: 2 / / there are several mirrored Server Version: 18.09.0 / / version information of docker Storage Driver: overlay2 / / Storage driver type is overlay2 Backing Filesystem: xfs / / supported file system: xfs Supports d_type: true Native Overlay Diff: trueLogging Driver: json-fileCgroup Driver: cgroupfsPlugins: Volume: local / / Local storage Network: bridge host macvlan null overlay / / supported network type Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslogSwarm: inactiveRuntimes: runcDefault Runtime: runcInit Binary : docker-initcontainerd version: c4446665cb9c30056f4998ed953e6d4ff22c7c39runc version: 4fc53a81fb7c994640722ac585fa9ca548971871init version: fec3683Security Options: seccomp Profile: defaultKernel Version: 3.10.0-862.el7.x86_64 / / Kernel Information Operating System: CentOS Linux 7 (Core) / / operating system OSType: linux / / operating system Type Architecture: x86 kernel 64 CPUs: 2 / / number of CPU Total Memory: 1.779GiB / / memory capacity Name: dockerID: 3SU5:P433:UIFM:YK6O:FBGP:MJSN:MTSO:PKPA:3NMN:6VW4:XUOV:XL5HDocker Root Dir: / var/lib/dockerDebug Mode (client): falseDebug Mode (server): falseRegistry: https://index.docker.io/v1/Labels:Experimental: falseInsecure Registries: Accelerator information adopted by 127.0.0.0/8Registry Mirrors: / / http://f1361db2.m.daocloud.io/Live Restore Enabled: falseProduct License: Community EngineData Volume (volume management operation)
Database management operation is divided into two ways: bind mount and docker managervolume.
1) Bind mount (bind and mount)
Persistent storage: essentially a directory or file in the DockerHost file system that can be directly Mount to the container's file system. When running the container, it can be done with-v.
Characteristics of Bind mount:
Data Volume is a directory or file, not an unformatted disk (block device); the container can read and write data in volume; it varies with the source file; volume data can be saved permanently, even if the container that uses it has been destroyed
Example:
[root@docker ~] # mkdir / html [root@docker ~] # echo "hello world" > > / html/index.html / / create a test page [root@docker ~] # docker run-itd-- name test-p 80:80-v / html:/usr/share/nginx/html nginx:latest// to run the container and use the "- v" option to specify the mount directory, preceded by the docker host directory ":" followed by the directory in the container [root@docker ~] # [root@docker ~] # curl 127.0.0.1 / / you can see that the mount has taken effect hello world [root@docker ~] # echo "lzj" > / html/index.html [root@docker ~] # curl 127.0.0.1 / / when the source file changes The target file will also change with lzj [root@docker ~] # docker inspect test / / View the details of the container "Mounts": [/ / find the Mount field {"Type": "bind", / / its type is bind "Source": "/ html" / / Source directory "Destination": "/ usr/share/nginx/html", / / destination directory "Mode": "," RW ": true," Propagation ":" rprivate "}]
Note:
The source file or directory that needs to be suspended on the DockerHost must already exist, otherwise, it will be hung into the container as a directory; by default, the container has read and write permissions for files mounted in the container. You can add the ": ro" option after running the container "- v" option to restrict the container's write permission; you can mount a separate file inside the container and use the scenario: if you do not want to overwrite the entire directory but only want to add a file, you can mount a single file; 2) Docker Manager Volume
Example:
[root@docker] # docker run-itd-- name test1-v / usr/share/nginx/html nginx:latest// after the "- v" option Simply add the directory in the container [root@docker ~] # docker inspect test1 "Mounts": [{"Type": "volume", / / its type is volume "Name": "47545a64ef51aa1ea1065848b6cb5cc5f19e2da8b2c91966b256a910fca58c4d", "Source": "/ var/lib/docker/volumes/47545a64ef51aa1ea1065848b6cb5cc5f19e2da8b2c91966b256a910fca58c4d/_data" / / Source directory (automatically generated by docker) "Destination": "/ usr/share/nginx/html", / / destination directory "Driver": "local", "Mode": "", "RW": true, "Propagation": ""}] [root@docker ~] # ls / var/lib/docker/volumes/47545a64ef51aa1ea1065848b6cb5cc5f19e2da8b2c91966b256a910fca58c4d/_data50x.html index.html// can see that the directory on the host is the directory mounted in the container
The characteristics of this approach are:
Will change according to the source file, and the effect is the same as Bind mount! When deleting a container, the original file on the dockerhost host will not be deleted by default. If you want to delete the original file when deleting the container, you can add the "- v" option when deleting the container (generally, it is not recommended because the file may be used by other containers); 3) Volume container (data sharing between container and container)
Volume container: a container that provides volume storage volumes to other containers. And it can provide either bind mount or docker manager volume.
[root@docker ~] # docker create-- name vc_data-v / html:/usr/share/nginx/html busybox:latest// create a container (no need to run) [root@docker ~] # docker run-itd-- name test3-P-- volumes-from vc_data nginx:latest// uses "--volumes-from" to mount data from the vc_data container to the new container test3 [root@docker ~] # docker ps / / View the mapped port CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES021653708bb2 nginx:latest "nginx-g 'daemon of …" 7 seconds ago Up 5 seconds 0.0.0.0 80/tcp test3 32768-> 80/tcp test3 [root@docker ~] # curl 127.0.0.1 80/tcp test3 32768 / / Test effect lzj
Note: after the source directory is deleted, the data in the container will also be lost!
You can write a dockerfile file, write the directory or file to the image, and then generate the container according to the image to ensure that the original data is lost and the data in the container will not change!
Because of the poor randomness and flexibility of this method, we will not introduce it here so that we can share data between containers through the data volume container.
Through the above mechanism, users do not have to worry about data loss even if the container fails during operation. If there is an accident, just recreate the container quickly!
Note: the most important thing in the production environment is the reliability of storage and the dynamic expansibility of storage. You must take this into account when making data volumes. What is more outstanding in this respect is the GFS file system. I just made a simple configuration above. If you are in a production environment, you must consider it carefully. For example, if you do the mirror volume container above, you can mount the GFS file system locally on the host. Then, when creating a mirror volume container, map the directory where the GFS is mounted to the mirror volume in the container, so that it is a qualified mirror volume container.
-that's all for this article. Thank you for reading-
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.