In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how to arrange services in docker CE on Linux. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
An overview of the deployment of distributed applications requires dealing with each logical layer and the relationships between them, such as front-end agents, web applications, message queues, caches, databases, etc. Containerized deployment introduces the concept of service orchestration (orchestration) to centrally control the lifecycle and running parameters of containers, including, but not limited to, the following:
Container deployment
Resource control
Load balancing
Health examination
Application configuration
Scale and scale
Location migration
Docker-ce natively provides two ways: compose and stack. The service is orchestrated by the container running parameters defined in the configuration file, which can be in the format of yaml or json. This paper takes the front-end agent (nginx) + web program (tomcat) as an example to briefly describe the application of these two methods.
Sample environment
2 hosts: dock_host_0 (192.168.9.168), dock_host_1 (192.168.9.169), the system is consistent with the software environment, all are new and minimized installation, single physical network card, operating system version CentOS Linux release 7.6.1810 (Core), kernel version 3.10.0-957.12.2.el7.x86_64, turn off selinux and firewall.
Docker is installed by default, version 18.09.6, with no other additional settings.
The basic image is the latest version of CentOS 7 official image.
Tomcat and jdk environments, as well as configuration files and logs of nginx, are mounted to the container in the form of directories.
The source code packages jdk-8u212-linux-x64.tar.gz and apache-tomcat-8.5.40.tar.gz are located in the / opt/ directory of the host.
Nginx is tengine and is compiled and installed by source code.
Compose mode
Install docker-compose
All versions of docker-compose are included in https://github.com/docker/compose/releases/. This article takes 1.24.0 as an example.
Download the source file:
[root@docker_host_0 ~] # ip addr show eth0 | sed-n'/ inet / p' | awk'{print $2} '192.168.9.168 / 24 [root @ docker_host_0 ~] # [root@docker_host_0 ~] # uname-r3.10.0-957.12.2.el7.x86_64 [root@docker_host_0 ~] # [root@docker_host_0 ~] # docker-vDocker version 18.09.6 Build 481bc77156 [root @ docker_host_0] # [root@docker_host_0 ~] # curl-L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname-s)-$(uname-m)"-o / usr/local/bin/docker-compose% Total% Received% Xferd Average Speed Time Current Dload Upload Total Spent Left Speed100 617 0617 00 5400-0:00:01-541100 15.4M 100 15.4M 00 261k 00:01: 0:01:00--:-836k [root @ docker_host_0 ~] # [root@docker_host_0 ~] # ll / usr/local/bin/docker-compose-rw-r- -root root-1 root root 16154160 May 30 23:23 / usr/local/bin/docker- compose [root @ root ~] # [root@docker_host_0 ~] # chmod uplix / usr/local/bin/docker- compose [root @ docker_host_0 ~] # [root@docker_host_0 ~] # which docker-compose/usr/local/bin/docker- compose [root @ docker_host_0 ~] # [root@docker_host_0 ~] # docker-compose versiondocker-compose version 1.24.0 Build 0aa59064docker-py version: 3.7.2CPython version: 3.6.8OpenSSL version: OpenSSL 1.1.0j 20 Nov 2018 [root@docker_host_0 ~] #
Add the command line completion feature of docker-compose:
[root@docker_host_0 ~] # curl-L https://raw.githubusercontent.com/docker/compose/1.24.0/contrib/completion/bash/docker-compose-o / etc/bash_completion.d/docker-compose% Total% Received% Xferd Average Speed Time Current Dload Upload Total Spent Left Speed100 13258 13258 00 14985 0 -: -:- -14980 [root@docker_host_0 ~] # [root@docker_host_0 ~] # source / etc/bash_completion.d/docker- composition [root @ docker_host_0 ~] # [root@docker_host_0 ~] # docker-composebuild create exec kill port push run stop upbundle down help logs ps restart scale top Versionconfig events images pause pull rm start unpausee [root @ docker_host_0 ~] #
Deployment servic
Create the source path of the directory mount:
In this example, the source mount paths for tomcat and jdk environment are / opt/apps/app_0/source and / opt/jdks, respectively.
The pattern field in server.xml is used to set the default access log format, which is changed to% local% {local} p% av% {remote} p, indicating local IP: Port-to-peer IP: Port, which is used to distinguish the access source.
[root@docker_host_0 ~] # cd / opt/ [root @ docker_host_0 opt] # [root@docker_host_0 opt] # lsapache-tomcat-8.5.40.tar.gz containerd jdk-8u212-linux- x64.tar.gz [root @ docker_host_0 opt] # [root@docker_host_0 opt] # mkdir-p / opt/ {apps/app_0/source Jdks} [root@docker_host_0 opt] # [root@docker_host_0 opt] # tar axf apache-tomcat-8.5.40.tar.gz-- strip-components=1-C apps/app_0/source/ [root @ docker_host_0 opt] # [root@docker_host_0 opt] # sed-I's apps/app_0 = "% h% l% u% t apps/app_0 ="% Alav% {local} p% av% {remote} p% t apps/app_0 / source/conf/ server.xml [root @ docker_host_0 opt] # [root@docker_host_0 opt] # tar axf jdk-8u212-linux-x64.tar.gz-C jdks/ [root @ docker_host_0 opt] #
Edit dockerfile:
[root@docker_host_0 opt] # vi dockerfile-for-nginxFROM centos:latestARG tmp_dir='/tmp'ARG repo_key=' http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7'ARG repo_src=' http://mirrors.163.com/.help/CentOS7-Base-163.repo'ARG repo_dst='/etc/yum.repos.d/CentOS-Base.repo'ARG tengine_ver='2.3.0'ARG tengine_src= "http:// Tengine.taobao.org/download/tengine-$ {tengine_ver} .tar.gz "ARG tengine_dst=" tengine-$ {tengine_ver} .tar.gz "ARG tengine_cfg_opts='--prefix=/usr/local/nginx\-with-http_gzip_static_module\-- with-http_stub_status_module\-- with-http_ssl_module\-- With-http_slice_module\-with-pcre'ARG depend_rpms='gcc make openssl-devel pcre-devel'RUN cd ${tmp_dir}\ & & cp-a ${repo_dst} ${repo_dst} .ori\ & & curl-L ${repo_src}-o ${repo_dst}\ & & curl-L ${tengine_src}-o ${tengine_dst}\ & & rpm -import ${repo_key}\ & & yum-y update-- downloadonly-- downloaddir=. \ & & yum-y install ${depend_rpms}-downloadonly-downloaddir=. \ & & yum-y install. / * .rpm\ & & useradd www-s / sbin/nologin\ & & tar axf ${tengine_dst}\ & & cd tengine-$ {tengine_ver}\ & make\ & & make install\ & & cd\ & & yum-y remove gcc make cpp\ & & yum clean all\ & & rm- Rf ${tmp_dir} / * EXPOSE 80/tcp 443/tcpENV PATH ${PATH}: / usr/local/nginx/sbinCMD nginx-g "daemon off "
Edit the service orchestration profile:
Yaml format configuration files in docker are usually suffixed with yml or yaml (customary, non-mandatory).
This example defines two services named webapp and proxy:
The webapp service runs the centos:latest image (image), mounts the data volume / directory (volumes), and specifies the environment variable (environment), the working directory (working_dir), the command running in the container (command), and the restart policy (restart) when the command fails (on-failure).
The proxy service runs the tengine_nginx:2.3.0 image, depending on the container depends_on within the webapp service. The dockerfile file used when compiling (build) the image is called dockerfile-for-nginx, and port 80 of the container is open to external port 80 (ports).
The network-related parameters can be set through the top-level networks directive in the configuration file. If not specified, it will be set by default. For all containers connected to the same network driver, all ports are open to each other. In this example, the default network setting is used. Tomcat's default port 8080 is open to nginx, and access to web services is forwarded through nginx, so port opening and mapping (expose/ports) is optional.
[root@docker_host_0 opt] # vi tomcat-with-nginx-compose.ymlversion: '3.7'services: webapp: image: centos:latest volumes:-/ opt/jdks/jdk1.8.0_212:/opt/jdks/jdk1.8.0_212:ro-/ opt/apps/app_0/source:/opt/apps/app_0 environment: JAVA_HOME: / opt/jdks/jdk1.8.0 _ 212 working_dir: / opt/apps/app_0 command: bin/catalina.sh run restart: on-failure proxy: build: context:. Dockerfile: dockerfile-for-nginx depends_on:-webapp image: tengine_nginx:2.3.0 volumes:-/ opt/apps/app_0/nginx/conf:/usr/local/nginx/conf:ro-/ opt/apps/app_0/nginx/logs:/usr/local/nginx/logs restart: on-failure ports: -'80 webapp image 80max tcp'
Check the orchestration profile:
The docker-compose config command is used to check the syntax and instructions of the configuration file, and to output all the contents of the configuration file. If the-q/--quiet parameter is specified, only the check is performed and no output is output.
The default profile name for docker-compose is docker-compose.yml or the docker-compose.yaml,-f parameter is used to specify a custom profile.
[root@docker_host_0 opt] # docker-compose-f tomcat-with-nginx-compose.yml config-Q [root @ docker_host_0 opt] #
Compile the image:
According to services. Docker-compose build command in the configuration file. The parameter build definition of the service name compiles the image. If the build directive is not specified in the configuration file, this step is not performed.
[root@docker_host_0 opt] # docker-compose-f tomcat-with-nginx-compose.yml build. [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEtengine_nginx 2.3.0 9404e1b71b70 32 seconds ago 340MBcentos latest 9f38484d220f 2 months ago 202 MB [root @ docker_host_0 opt] #
Run nginx in non-daemon mode (nginx-g "daemon off;") for 60 seconds and copy the files required by nginx:
[root@docker_host_0 opt] # docker run-dit-rm-name t_nginx tengine_nginx:2.3.0 bash-c 'timeout 60 nginx-g "daemon off "'3cc8de88de3fe295657fde08552165e69514c368689e2078ec89771e23cb16e8 [root@docker_host_0 opt] # docker container ls-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3cc8de88de3f tengine_nginx:2.3.0" bash-c' timeout 60... " 7 seconds ago Up 6 seconds 80/tcp 443/tcp ttnginx [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker exec-it t_nginx ls-l / usr/local/nginxtotal 0drwx-2 nobody root 6 May 30 23:39 client_body_tempdrwxr-xr-x 2 root root 333 May 30 23:37 confdrwx- 2 nobody root 6 May 30 23:39 fastcgi_tempdrwxr-xr-x 2 root root 40 May 30 23:37 htmldrwxr-xr -x 1 root root 58 May 30 23:39 logsdrwx- 2 nobody root 6 May 30 23:39 proxy_tempdrwxr-xr-x 2 root root 19 May 30 23:37 sbindrwx- 2 nobody root 6 May 30 23:39 scgi_tempdrwx- 2 nobody root 6 May 30 23:39 uwsgi_ temp [docker_host_0 opt] # [root@docker_host_0 opt] # docker cp t_nginx:/usr/local/nginx / / opt/apps/app_0/ [root @ docker_host_0 opt] # [root@docker_host_0 opt] # ll / opt/apps/app_0/nginx/total 0drwx-2 root root 6 May 30 23:39 client_body_tempdrwxr-xr-x 2 root root 333 May 30 23:37 confdrwx- 2 root root 6 May 30 23:39 fastcgi_tempdrwxr-xr-x 2 root root 40 May 30 23:37 htmldrwxr-xr-x 2 root root 58 May 30 23:39 logsdrwx- 2 root root 6 May 30 23:39 proxy_tempdrwxr-xr-x 2 root root 19 May 30 23:37 sbindrwx- 2 root root 6 May 30 23:39 scgi_tempdrwx- 2 root root 6 May 30 23:39 uwsgi_ temp [root @ docker_host_0 opt] #
Edit the nginx configuration file:
Docker internally implements the service discovery (service discovery) function, which automatically provides name resolution for containers connected to the same network driver. In this example, after the webapp service starts successfully, it can be recognized by the nginx of the proxy service, so the service name or alias can be used as a parameter to proxy_pass or upstream in the nginx.
User www www;worker_processes auto;pid logs/nginx.pid;error_log logs/error.log warn;worker_rlimit_nofile 51200 use epoll; worker_connections events {include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 4096;} http {include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 16k; large_client_header_buffers 4 32k; client_max_body_size 8m Access_log off; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 30; proxy_cache_methods POST GET HEAD; open_file_cache max=655350 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; gzip on; gzip_min_length 1k; gzip_buffers 8 8k Gzip_http_version 1.0; gzip_comp_level 4; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php; gzip_vary on; server_tokens off Log_format main'$remote_addr\ t$upstream_addr\ t [$time_local]\ t$request\ t'$status\ t$body_bytes_sent\ t$http_user_agent\ t$http_referer\ t'$http_x_forwarded_for\ t$request_time\ t$upstream_response_time\ t$remote_user\ t'$request_body' Map $http_upgrade $connection_upgrade {default upgrade;''close;} upstream tomcat-app-0 {server webapp:8080;} server {listen 80; server_name 127.0.1; charset utf-8 Client_max_body_size 75M; location / {proxy_pass http://tomcat-app-0;} access_log logs/webapp-access.log main;}}
Test the nginx profile:
[root@docker_host_0 opt] # docker run-it-- rm-- mount type=bind,src=/opt/apps/app_0/nginx/conf,dst=/usr/local/nginx/conf,ro-- add-host webapp:127.0.0.1 tengine_nginx:2.3.0 bash-c 'nginx-t'nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is fulfilling [root @ docker_host_0 opt] #
Start the service:
The docker-compose up command is used to start the service:
All services defined in the default startup configuration file can be explicitly specified to start a specific service.
If the image name specified in the configuration file does not exist, compilation (build) is performed first by default.
-d/--detach is used to specify that the container runs in the background, which is equivalent to the-d/--detach option of the docker run command.
-- scale is used to specify the number of containers for the corresponding service in the format of service name = quantity.
[root@docker_host_0 opt] # docker-compose-f tomcat-with-nginx-compose.yml up-d-scale webapp=3Creating network "opt_default" with the default driverCreating opt_webapp_1. DoneCreating opt_webapp_2... DoneCreating opt_webapp_3... DoneCreating opt_proxy_1... Docker container ls [root @ docker_host_0 opt] # [root@docker_host_0 opt] # root-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES6b55fe98a99c tengine_nginx:2.3.0 "/ root-c 'nginx-…" 10 seconds ago Up 9 seconds 0.0.0.0 80/tcp 80-> 80/tcp 443/tcp opt_proxy_10617d640c60a centos:latest "bin/catalina.sh run" 11 seconds ago Up 9 seconds opt_webapp_2c85f2de181cd centos:latest "bin/catalina.sh run" 11 seconds ago Up 10 seconds opt_webapp_32517e03f11c9 centos:latest "bin/catalina.sh run" 11 seconds ago Up 10 seconds opt_webapp_1 [root@docker_host_0 opt] #
Docker-compose creates a network in bridge mode by default:
[root@docker_host_0 opt] # docker network lsNETWORK ID NAME DRIVER SCOPEcb90714e47b3 bridge bridge locala019d8b63640 host host localbb7095896ade none null local80ce8533b964 opt_default bridge local [root@docker_host_0 opt] #
Check the running status of the processes in the container:
Docker-compose top can specify a service name to see how processes are running within a particular service.
[root@docker_host_0 opt] # docker-compose-f tomcat-with-nginx-compose.yml topopt_proxy_1UID PID PPID C STIME TTY TIME CMD Root 13674 13657 0 00:28? 00:00:00 nginx: master process nginx-g daemon off 1000 13738 13674 000: 28? 00:00:00 nginx: worker process1000 13739 13674 000: 28? 00:00:00 nginx: worker processopt_webapp_1UID PID PPID C STIME TTY TIME CMD-- -root 13367 13342 1 00:28? 00:00:02 / opt/jdks/jdk1.8.0_212/bin/java-Djava.util.l Ogging.config.file=/opt/apps/app_0/conf/loggi ng.properties-Djava.util.logging.manager=org .apache.juli.ClassLoaderLogManager-Djdk.tls EphemeralDHKeySize = 2048-Djava.prot ocol.handler.pkgs=org.apache.catalina.webreso urces-Dorg.apache.catalina.security.Security Listener.UMASK=0027-Dignore.endorsed.dirs= -classpath / opt/apps/app_0/bin/bootstrap.jar: / opt/apps/app_0/bin/tomcat-juli.jar-Dcatalina.base=/opt/apps/app_0 -Dcatalina.home=/opt/apps/app_0-Djava.io.tmpdir=/opt/apps/app_0/temp org.apache.catalina.startup.Bootstrap startopt_webapp_2UID PID PPID C STIME TTY TIME CMD-root 13436 13388 1 00:28? 00:00:02 / opt/jdks/jdk1.8.0_212/bin/java-Djava.util.l ogging.config.file=/opt/apps/app_0/conf/loggi ng.properties-Djava.util.logging.manager=org .apache.juli.ClassLoaderLogManager-Djdk.tls.ephemeralDHKeySize=2048-Djava.prot ocol.handler.pkgs=org.apache.catalina.webreso Urces-Dorg.apache.catalina.security.Security Listener.UMASK=0027-Dignore.endorsed.dirs=-classpath / opt/apps/app_0/bin/bootstrap.jar: / opt/apps/app_0/bin/tomcat-juli.jar-Dcatalina.base=/opt/apps/app_0-Dcatalina.home=/opt/apps/app_0 -Djava.io.tmpdir=/opt/apps/app_0/temp org.apache.catalina.startup.Bootstrap startopt_webapp_3UID PID PPID C STIME TTY TIME CMD-- -root 13425 13397 1 00:28? 00:00:02 / opt/jdks/jdk1.8.0_212/bin/java-Djava.util.l Ogging.config.file=/opt/apps/app_0/conf/loggi ng.properties-Djava.util.logging.manager=org .apache.juli.ClassLoaderLogManager-Djdk .tls.ephemeralDHKeySize = 2048-Djava.prot ocol.handler.pkgs=org.apache.catalina.webreso urces-Dorg.apache.catalina.security.Security Listener.UMASK=0027-Dignore.endorsed.dirs= -classpath / opt/apps/app_0/bin/bootstrap.jar: / opt/apps/app_0/bin/tomcat-juli.jar-Dcatalina.base=/opt/apps/app_0 -Dcatalina.home=/opt/apps/app_0-Djava.io.tmpdir=/opt/apps/app_0/temp org.apache.catalina.startup.Bootstrap start [root @ docker_host_0 opt] #
Access the web service, and the request is dispatched to each container within the service:
[root@docker_host_0 opt] # ss-atn | grep 80LISTEN 0 128: 80: * [root@docker_host_0 opt] # [root@docker_host_0 opt] # for i in $(seq 6); do curl-s 127.0.0.1-o / dev/null [root @ docker_host_0 opt] # [root@docker_host_0 opt] # cat / opt/apps/app_0/source/logs/localhost_access_log.$ (date +% F). Txt172.20.0.3:80 172.20.0.5 date 42430 [31/May/2019:00:32:16 + 0000] "GET / HTTP/1.0" 200 11184172.20.0.3 cat 80 172.20.0.5 cat 42436 [31/May/2019:00 : 32:16 + 0000] "GET / HTTP/1.0" 200 11184172.20.0.4 purl 80 172.20.0.5 GET / HTTP/1.0 [31/May/2019:00:32:16 + 0000] "GET / HTTP/1.0" 20011184172.20.0.4 purl 80 172.20.0.5 [31/May/2019:00:32:16 + 0000] "GET / HTTP/1.0" 20011184172.20.0.2 purl 80 172.20.0.5: 59294 [31/May/2019:00:32:16 + 0000] "GET / HTTP/1.0" 200 11184172.20.0.2 HTTP/1.0 80 172.20.0.5 31/May/2019:00:32:16 + 0000] "GET / HTTP/1.0" 11184 [root@docker_host_0 opt] #
Expand the number of containers in the service:
When the docker-compose up command is executed again on a running service,-- scale is used to dynamically increase or decrease the number of containers in the service on an existing basis.
[root@docker_host_0 opt] # docker-compose-f tomcat-with-nginx-compose.yml up-d-- scale webapp=6Starting opt_webapp_1. DoneStarting opt_webapp_2... DoneStarting opt_webapp_3... DoneCreating opt_webapp_4... DoneCreating opt_webapp_5... DoneCreating opt_webapp_6... Doneopt_proxy_1 is up-to- date [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker container ls-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb9fc74985a13 centos:latest "bin/catalina.sh run" 9 seconds ago Up 7 seconds Opt_webapp_429e9837c7b4d centos:latest "bin/catalina.sh run" 9 seconds ago Up 7 seconds opt_webapp_55e0a0611bb2f centos:latest "bin/catalina.sh run" 9 seconds ago Up 8 seconds opt _ webapp_66b55fe98a99c tengine_nginx:2.3.0 "/ bin/sh-c 'nginx-…" 3 minutes ago Up 3 minutes 0.0.0.080/tcp 80-> 80/tcp 443/tcp opt_proxy_10617d640c60a centos:latest "bin/catalina.sh run" 3 minutes ago Up 3 minutes opt_webapp_2c85f2de181cd centos:latest "bin/catalina.sh run" 3 minutes ago Up 3 minutes opt_webapp_32517e03f11c9 centos:latest "bin/catalina.sh run" 3 minutes ago Up 3 minutes opt_webapp_1 [root@docker_host_0 opt] #
Remove the service:
The docker-compose down command is used to remove the service, including stopping the containers and networks associated with the service, and specifying the-- rmi and-v/--volumes options to remove the associated data volumes and mirrors.
[root@docker_host_0 opt] # docker-compose-f tomcat-with-nginx-compose.yml downStopping opt_webapp_4... DoneStopping opt_webapp_5... DoneStopping opt_webapp_6... DoneStopping opt_proxy_1... DoneStopping opt_webapp_2... DoneStopping opt_webapp_3... DoneStopping opt_webapp_1... DoneRemoving opt_webapp_4... DoneRemoving opt_webapp_5... DoneRemoving opt_webapp_6... DoneRemoving opt_proxy_1... DoneRemoving opt_webapp_2... DoneRemoving opt_webapp_3... DoneRemoving opt_webapp_1... DoneRemoving network opt_ default [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker container ls-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root @ docker_host_0 opt] # [root@docker_host_0 opt] # [root@docker_host_0 opt] # ss-atn | grep 80 [root@docker_host_ 0 opt] # [root@docker_host_0 opt] # docker network lsNETWORK ID NAME DRIVER SCOPEcb90714e47b3 bridge bridge locala019d8b63640 host host localbb7095896ade none null local [root @ docker_host_0 opt] #
Stack mode
Host docker_host_0 creates a cluster, and docker_host_1 joins the cluster with an administrative role:
[root@docker_host_0 opt] # docker swarm initSwarm initialized: current node (u9siv3gxc4px3xa85t5tybv68) is now a manager.To add a worker to this swarm, run the following command: docker swarm join--token SWMTKN-1-5icsimlouv1ppt09fxovvlvn9pp3prevlu2vus6wvtdilv6w86-3y28uwlmc5hcb61hw42oxe4j2 192.168.9.168:2377To add a manager to this swarm, run 'docker swarm join-token manager' and follow the policies. [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker swarm join-token managerTo add a manager to this swarm Run the following command: docker swarm join-- token SWMTKN-1-5icsimlouv1ppt09fxovvlvn9pp3prevlu2vus6wvtdilv6w86-elvhukieu148f22dmimq914ki 192.168.9.168: 2377 [root @ docker_host_0 opt] # [root@docker_host_1 ~] # ip addr show eth0 | sed-n'/ inet / p' | awk'{print $2} '192.168.9.169 / 24 [root @ docker_host_1] # [root@docker_host_1 ~] # uname-r3.10.0-957.12.2.el7. X86'64 [root@docker_host_1 ~] # [root@docker_host_1 ~] # docker-vDocker version 18.09.6 Build 481bc77156 [root @ docker_host_1 ~] # [root@docker_host_1 ~] # docker swarm join-- token SWMTKN-1-5icsimlouv1ppt09fxovvlvn9pp3prevlu2vus6wvtdilv6w86-elvhukieu148f22dmimq914ki 192.168.9.168:2377This node joined a swarm as a manager. [root @ docker_host_1 ~] # [root@docker_host_1 ~] # docker node lsID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSIONu9siv3gxc4px3xa85t5tybv68 docker_host _ 0 Ready Active Leader 18.09.6qhgpqw9n5wwow1zfzji69eac0 * docker_host_1 Ready Active Reachable 18.09.6 [root @ docker_host_1 ~] #
Export the tengine_nginx:2.3.0 image on the docker_host_0 node and transfer it to the docker_host_1 node:
[root@docker_host_0 opt] # docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEtengine_nginx 2.3.0 9404e1b71b70 About an hour ago 340MBcentos latest 9f38484d220f 2 months ago 202MB [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker image save tengine_ Nginx:2.3.0-o nginx.tar [root @ docker_host_0 opt] # [root@docker_host_0 opt] # ll-h nginx.tar-rw- 1 root root 338M May 31 00:56 nginx.tar [root @ docker_host_0 opt] # [root@docker_host_0 opt] # scp nginx.tar root@192.168.9.169:/optnginx.tar 100% 337MB 83.1MB/s 00:04 [root@docker_host_0 opt] #
Import the tengine_nginx:2.3.0 image on the docker_host_1 node and set the same mount source path as the docker_host_0 node:
[root@docker_host_1 ~] # cd / opt/ [root @ docker_host_1 opt] # [root@docker_host_1 opt] # lsapache-tomcat-8.5.40.tar.gz containerd jdk-8u212-linux-x64.tar.gz nginx.tar [root @ docker_host_1 opt] # [root@docker_host_1 opt] # docker image load-I nginx.tard69483a6face: Loading layer 209.5MB/209.5MB717661697400: Loading layer 144.3MB/144.3MBLoaded image: tengine_nginx : 2.3.0 [root @ docker_host_1 opt] # [root@docker_host_1 opt] # docker image ls-aREPOSITORY TAG IMAGE ID CREATED SIZEtengine_nginx 2.3.0 9404e1b71b70 About an hour ago 340MB [root @ docker_host_1 opt] # [root@docker_host_1 opt] # mkdir-p / opt/ {apps/app_0/source Jdks} [root@docker_host_1 opt] # [root@docker_host_1 opt] # tar axf apache-tomcat-8.5.40.tar.gz-- strip-components=1-C apps/app_0/source/ [root @ docker_host_1 opt] # [root@docker_host_1 opt] # sed-I's apps/app_0 = "% h% l% u% t apps/app_0 ="% Alav% {local} p% av% {remote} p% t apps/app_0 / source/conf/ server.xml [root @ docker_host_1 opt] # [root@docker_host_1 opt] # tar axf jdk-8u212-linux-x64.tar.gz-C jdks/ [root @ docker_host_1 opt] #
Edit the service orchestration profile on the docker_host_0 node:
Volumes and port use the long syntax (long syntax) format to specify the mount point and port.
Services. The service name .deploy specifies the mode of operation of the service (mode), the number of replicas (replicas), the restart policy (restart_policy), and the node where the service resides (placement).
[root@docker_host_0 opt] # vi tomcat-with-nginx-stack.ymlversion: "3.7services: webapp: image: centos:latest volumes:-type: bind source: / opt/jdks/jdk1.8.0_212 target: / opt/jdks/jdk1.8.0_212 read_only: true-type: bind source: / opt/apps/app_0/source Target: / opt/apps/app_0 environment: JAVA_HOME: / opt/jdks/jdk1.8.0_212 working_dir: / opt/apps/app_0 command: bin/catalina.sh run deploy: mode: replicated replicas: 3 restart_policy: condition: on-failure proxy: image: tengine_nginx:2.3.0 volumes:-type: bind source: / opt/ Apps/app_0/nginx/conf target: / usr/local/nginx/conf read_only: true-type: bind source: / opt/apps/app_0/nginx/logs target: / usr/local/nginx/logs deploy: placement: constraints:-node.hostname = = docker_host_0 mode: global restart_policy: condition: on-failure Ports:-target: 80 published: 80 protocol: tcp mode: ingress
Deployment service, named web-cluster:
[root@docker_host_0 opt] # docker stack deploy-c tomcat-with-nginx-stack.yml web-clusterCreating network web-cluster_defaultCreating service web-cluster_webappCreating service web-cluster_ proxy [root @ docker_host_0 opt] #
The replica service webapp is assigned to 2 nodes, and the global service proxy is assigned to the docker_host_0 node according to the constraint (constraints):
[root@docker_host_0 opt] # docker stack lsNAME SERVICES ORCHESTRATORweb-cluster 2 servers [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker service lsID NAME MODE REPLICAS IMAGE PORTSnjg5ngjjp9gi web-cluster_proxy Global 1 docker_host_0 opt 1 tengine_nginx:2.3.0 *: 80-> 80/tcpwc2uv0zllneo web-cluster_webapp replicated 3 docker_host_0 opt 3 centos: latest [root @ root] # [root@docker_host_0 opt] # docker service ps web-cluster_webappID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTSnh0pei38ikf7 web-cluster_webapp.1 centos:latest docker_host_1 Running Running about a minute agootzusftmorjr web-cluster_webapp.2 centos:latest docker_host_1 Running Running about a minute agotmjkmrmtbx9g web-cluster_webapp.3 centos:latest Docker_host_0 Running Running about a minute ago [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker service ps web-cluster_proxyID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTStal0jywnmukv Web-cluster_proxy.u9siv3gxc4px3xa85t5tybv68 tengine_nginx:2.3.0 docker_host_0 Running Running 53 seconds ago [root @ docker_host_0 opt] #
Stack defaults to overlay networks created in swarm mode:
[root@docker_host_0 opt] # docker network lsNETWORK ID NAME DRIVER SCOPEb728df2e4b85 bridge bridge localdfe3ba6e0df5 docker_gwbridge bridge locala019d8b63640 host host localmxcmpb9uzjy2 ingress overlay swarmbb7095896ade none Null localxxl3uk5r7s7v web-cluster_default overlay slots [root @ docker_host_0 opt] #
The proxied web service can be accessed through both docker_host_0 and docker_host_1 nodes:
[root@docker_host_1 ~] # ss-atn | grep: 80LISTEN 0 128: 80: * [root@docker_host_1 ~] # [root@docker_host_1 ~] # curl-I-o / dev/null-s-w% {http_code} 192.168.9.168200 [root @ docker_host_1 ~] # curl-I-o / dev/null-s-w% {http_code} 192.168.9.169200 [root @ docker_host_1 ~] # [root@docker_host_0 opt] # ss-atn | grep: 80LISTEN 0 128: 80: * [root@docker_host_0 opt] # [root@docker_host_0 opt] # curl-I-o / dev/null-s-w% {http_code} 192.168.9.168200 [root @ docker_host_0 opt] # curl-I-o / dev/null-s-w% {http_code} 192.168.9.169200 [root @ docker_host_0 opt] #
Remove the service:
For services choreographed through swarm stack, the deployment operation is the docker stack deploy service name and the removal operation is the docker stack rm service name. After the service is removed, the network driver associated with it is also removed.
[root@docker_host_0 opt] # docker stack lsNAME SERVICES ORCHESTRATORweb-cluster 2 servers [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker stack rm web-clusterRemoving service web-cluster_proxyRemoving service web-cluster_webappRemoving network web-cluster_ default [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker stack lsNAME SERVICES orchestrator [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker service lsID NAME MODE REPLICAS IMAGE ports [root @ docker_host_0 opt] # [root@docker_host_0 opt] # docker network lsNETWORK ID NAME DRIVER SCOPEb728df2e4b85 Bridge bridge localdfe3ba6e0df5 docker_gwbridge bridge locala019d8b63640 host host localmxcmpb9uzjy2 ingress overlay swarmbb7095896ade none null local [root @ docker_host_0 opt] #
The depends_on directive can only be used to specify the construction order of the image and the start and stop order of the container in docker-compose mode. If you need to resolve the dependencies between applications within the container, you need to manually implement the slow start of the commands in the container (such as performing the target port survival or url availability test in the container's script to determine whether to turn on the local service), or with the help of third-party tools such as dockerize.
Difference and relation
Both stack and compose orchestrate services through configuration files in yaml or json format. The main differences include:
Some instructions are not compatible in two ways, such as build,deploy,depends_on,restart_policy.
Stack is built into the docker engine, and swarm mode must be enabled. Multiple containers that make up the same service may span multiple hosts, so the corresponding images must exist in the local or accessible warehouse of the host. Compose needs to be installed additional, and there is no need to enable swarm mode. All containers are located in the current single host.
Stack can only deploy services based on pre-compiled images; compose supports image compilation and service deployment, both of which can be executed at the same time or separately.
The above is how to arrange the services in docker CE on Linux. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.