Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use NFS to mount memory into a high-speed hard disk in a Docker container based on CentOS7.6.

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Use a background to mount memory into a high-speed hard disk through NFS in an CentOS7.6-based Docker container.

This is a problem encountered in the recent project. In an application that is known to be deployed on the docker container cloud, reads and writes are very frequent and require high disk performance, but they cannot be read and written with high intensity in the same container. In addition, the host memory resources are redundant, allowing the container to run in privileged mode without requiring persistent storage of this part of the data.

Through the analysis of the problem, I have adopted the following solutions:

By mounting the memory into a hard disk, the performance of the disk can be greatly improved.

Since you cannot read and write in the same container, you can use NFS to solve the problem

Privileged mode is allowed, and disks can be mounted inside the container

Data persistence is not required, and memory can be used as a message to disk

On the same host, the cross-host interconnection of containers can be ignored.

In this article, the content related to the interests of the company has been dealt with, for example: the image involved in this article has removed the relevant applications and is directly based on centos7.6.1810.

Environment 2.1Host OSCentOS Linux release 7.6.1810 (Core) 2.2.hardware information memory: initialization configuration of 256GB2.3 host #! / bin/bashUserName='gysl'PassWord='drh223'# Install the Docker engine. This needs to be executed on every machine.curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo-o / etc/yum.repos.d/docker-ce.repo > & / dev/nullif [$?-eq 0] Then yum remove docker\ docker-client\ docker-client-latest\ docker-common\ docker-latest\ docker-latest-logrotate\ docker-logrotate\ docker-selinux\ Docker-engine-selinux\ docker-engine > & / dev/null yum list docker-ce--showduplicates | grep "^ doc" | sort-r yum-y install docker-ce-18.09.3-3.el7 rm-f / etc/yum.repos.d/docker-ce.repo systemctl enable docker--now & & systemctl status docker else echo "Install failed! Please try again! "; exit 110fi# Modify related kernel parameters. Cat > / etc/sysctl.d/docker.conf > / etc/exportsmount-t tmpfs-o size=$SIZE tmpfs $SSD/usr/sbin/exportfs-r/usr/sbin/rpcbind/usr/sbin/rpc.nfsd/usr/sbin/rpc.mountd/usr/sbin/rpc.rquotadwhile true; do sleep 6000; done3.1.2 construction phase

Create a new directory and put the Dokcerfile and start.sh above in that directory.

[gysl@gysl-dev ~] $mkdir nfs [gysl@gysl-dev ~] $cd nfs [gysl@gysl-dev nfs] $docker build-t nfs:v1.0 .3.1.3 start the NFS service container

Start the NFS service in the container with the following command:

[gysl@gysl-dev nfs] $docker run-itd-- privileged-- rm nfs:v1.0953dd0cf03e024447ba3a7f1be6dce6217226b25c13ffa2b9967941c96b73f4e3.1.4 write down the IP of the NFS service container [Gysl @ gysl-dev nfs] $docker inspect 953 | grep-w 'IPAddress' "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", 3.2 modify the application image 3.2.1 add the NFS service to the image where the application resides

Modify the Dockerfile as follows:

FROM centos:7.6.1810ENV SSD='/high-speed-storage' DATA='/data'COPY. / RUN yum-y install nfs-utils & &\ mkdir $DATACMD ["/ bin/bash", "/ start-client.sh"]

Add a start-client.sh script with the following content:

#! / bin/bashmount-t nfs 172.17.0.2:$SSD $DATAwhile true; do sleep 6000; done3.2.2 rebuilds the application image

Create a new directory and put the modified Dockerfile and start-client.sh in the same directory. Execute the command as follows:

[gysl@gysl-dev ~] $mkdir nfs-client [gysl@gysl-dev ~] $cd nfs-client/ [gysl@gysl-dev nfs-client] $vi Dockerfile [gysl@gysl-dev nfs-client] $vi start-client.sh [gysl@gysl-dev nfs-client] $docker run-- privileged-itd-- rm nfs-client:v1.07e01276f49815b76dd4dc3ae3ff9a80b8d4f32814f46c4e58f7cfab0d945cebf3.3.3 verify whether the mount is successful

Enter the application container to check whether the mount is successful:

[root@7e01276f4981 /] # df-hFilesystem Size Used Avail Use% Mounted onoverlay 8.0G 2.6G 5.5G 32% / tmpfs 64M 064m 0% / devtmpfs 455M 0455m 0% / sys/fs/cgroup/dev/mapper/centos-root 8. 0G 2.6G 5.5G 32% / etc/hostsshm 64M 0 64m 0% / dev/shm172.17.0.2:/high-speed-storage 10m 0 10m 0% / data [root@7e01276f4981 /] # touch / data/test

Success! Problem solved!

4 summarize and expand 4.1 the shortcomings of this case

It does not meet the mainstream standards of container application of one container, one process.

Data cannot be persisted, restart container data will be erased

The container has a dependency. You must first start the container that provides the NFS service.

The scope of application is narrow

Services cannot be managed through systemd

This scenario is not recommended in a production environment. 4.2 expand knowledge 4.2.1 there are tmpfs and ramdisk solutions for mounting memory into a high-speed hard disk.

Ramdisk under linux is provided by the kernel and can be used by mounting the mount command. It is treated as a block device and the file system needs to be formatted when it is used. Once created, ramdisk takes up a fixed amount of physical memory, while tmpfs is allocated dynamically.

4.2.2 Interconnection of Docker containers

In the case that the network scheme is not specified for the same host, Docker is bridged by bridge. If interconnection across hosts is involved, other scenarios may need to be used.

4.2.3 other NFS solutions in the container

Nfs-ganesha is also a popular solution for NFS in containers. For more information, please see:

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report