您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何進行docker CE on Linux中的服務編排,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
容器部署
資源控制
健康檢查
應用配置
規模伸縮
位置遷移
docker-ce原生提供了compose與stack兩種方式,通過定義在配置文件中的容器運行參數對服務進行編排,配置文件的格式可以為yaml或json。本文以前端代理(nginx) + web程序(tomcat)為例,簡述這兩種方式的應用。
宿主機2臺:dock_host_0(192.168.9.168/24),dock_host_1(192.168.9.169/24),系統與軟件環境一致,均為全新最小化安裝,單物理網卡,操作系統版本CentOS Linux release 7.6.1810 (Core),內核版本3.10.0-957.12.2.el7.x86_64,關閉selinux與防火墻。
docker為默認安裝,版本18.09.6,無其他額外設置。
基礎鏡像為最新版CentOS 7官方鏡像。
tomcat與jdk環境,以及nginx的配置文件與日志,均以目錄的方式掛載至容器。
源碼包jdk-8u212-linux-x64.tar.gz與apache-tomcat-8.5.40.tar.gz,位于宿主機的/opt/目錄。
nginx為tengine,通過源碼編譯安裝。
安裝docker-compose
https://github.com/docker/compose/releases/ 中包含docker-compose的所有版本,本文以1.24.0為例。
下載源文件:
[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 -r 3.10.0-957.12.2.el7.x86_64 [root@docker_host_0 ~]# [root@docker_host_0 ~]# docker -v Docker 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 Time Time Current Dload Upload Total Spent Left Speed 100 617 0 617 0 0 540 0 --:--:-- 0:00:01 --:--:-- 541 100 15.4M 100 15.4M 0 0 261k 0 0:01:00 0:01:00 --:--:-- 836k [root@docker_host_0 ~]# [root@docker_host_0 ~]# ll /usr/local/bin/docker-compose -rw-r--r-- 1 root root 16154160 May 30 23:23 /usr/local/bin/docker-compose [root@docker_host_0 ~]# [root@docker_host_0 ~]# chmod u+x /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 version docker-compose version 1.24.0, build 0aa59064 docker-py version: 3.7.2 CPython version: 3.6.8 OpenSSL version: OpenSSL 1.1.0j 20 Nov 2018 [root@docker_host_0 ~]#
添加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 Time Time Current Dload Upload Total Spent Left Speed 100 13258 100 13258 0 0 14985 0 --:--:-- --:--:-- --:--:-- 14980 [root@docker_host_0 ~]# [root@docker_host_0 ~]# source /etc/bash_completion.d/docker-compose [root@docker_host_0 ~]# [root@docker_host_0 ~]# docker-compose build create exec kill port push run stop up bundle down help logs ps restart scale top version config events images pause pull rm start unpause [root@docker_host_0 ~]#
部署服務
創建目錄掛載的源路徑:
本例中,tomcat與jdk環境對應的源掛載路徑分別為/opt/apps/app_0/source與/opt/jdks。
server.xml中的pattern字段用于設置默認的訪問日志格式,更改為%A:%{local}p %a:%{remote}p,表示本端IP:端口 對端IP:端口,用于區分訪問來源。
[root@docker_host_0 ~]# cd /opt/ [root@docker_host_0 opt]# [root@docker_host_0 opt]# ls apache-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/pattern="%h %l %u %t/pattern="%A:%{local}p %a:%{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]#
編輯dockerfile:
[root@docker_host_0 opt]# vi dockerfile-for-nginx FROM centos:latest ARG 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} \ && ./configure ${tengine_cfg_opts} \ && make \ && make install \ && cd \ && yum -y remove gcc make cpp \ && yum clean all \ && rm -rf ${tmp_dir}/* EXPOSE 80/tcp 443/tcp ENV PATH ${PATH}:/usr/local/nginx/sbin CMD nginx -g "daemon off;"
編輯服務編排配置文件:
docker中的yaml格式配置文件通常以yml或yaml作為后綴(慣例,非強制)。
本例定義了2個服務,名稱分別為webapp與proxy:
webapp服務運行centos:latest鏡像(image),掛載數據卷/目錄(volumes),并指定環境變量(environment),工作目錄(working_dir),容器內運行的命令(command),以及重啟策略(restart)為命令運行失敗時(on-failure)。
proxy服務運行tengine_nginx:2.3.0鏡像,依賴于webapp服務內的容器啟動(depends_on)。編譯(build)鏡像時使用的dockerfile文件名為dockerfile-for-nginx,容器的80端口開放為外部的80端口(ports)。
配置文件內可以通過頂級的networks指令設置網絡相關的參數,未指定則按默認設置。對于連接至同一網絡驅動下的所有容器,相互之間開放所有端口,本例使用默認網絡設置,tomcat默認的8080端口對nginx開放,web服務的訪問通過nginx進行轉發,因此端口的開放與映射(expose/ports)可選。
[root@docker_host_0 opt]# vi tomcat-with-nginx-compose.yml version: '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:80/tcp'
檢查編排配置文件:
docker-compose config命令用于檢查配置文件的語法與指令,并輸出配置文件的所有內容,若指定-q/--quiet參數則僅執行檢查而不輸出。
docker-compose默認的配置文件名稱為docker-compose.yml或docker-compose.yaml,-f參數用于指定自定義的配置文件。
[root@docker_host_0 opt]# docker-compose -f tomcat-with-nginx-compose.yml config -q [root@docker_host_0 opt]#
編譯鏡像:
docker-compose build命令根據配置文件中services.服務名.build定義的參數編譯鏡像,若配置文件中未指定build指令,則不執行該步驟。
[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 ls REPOSITORY TAG IMAGE ID CREATED SIZE tengine_nginx 2.3.0 9404e1b71b70 32 seconds ago 340MB centos latest 9f38484d220f 2 months ago 202MB [root@docker_host_0 opt]#
以非守護進程模式(nginx -g "daemon off;")運行nginx 60秒,復制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]# [root@docker_host_0 opt]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3cc8de88de3f tengine_nginx:2.3.0 "bash -c 'timeout 60…" 7 seconds ago Up 6 seconds 80/tcp, 443/tcp t_nginx [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker exec -it t_nginx ls -l /usr/local/nginx total 0 drwx------ 2 nobody root 6 May 30 23:39 client_body_temp drwxr-xr-x 2 root root 333 May 30 23:37 conf drwx------ 2 nobody root 6 May 30 23:39 fastcgi_temp drwxr-xr-x 2 root root 40 May 30 23:37 html drwxr-xr-x 1 root root 58 May 30 23:39 logs drwx------ 2 nobody root 6 May 30 23:39 proxy_temp drwxr-xr-x 2 root root 19 May 30 23:37 sbin drwx------ 2 nobody root 6 May 30 23:39 scgi_temp drwx------ 2 nobody root 6 May 30 23:39 uwsgi_temp [root@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 0 drwx------ 2 root root 6 May 30 23:39 client_body_temp drwxr-xr-x 2 root root 333 May 30 23:37 conf drwx------ 2 root root 6 May 30 23:39 fastcgi_temp drwxr-xr-x 2 root root 40 May 30 23:37 html drwxr-xr-x 2 root root 58 May 30 23:39 logs drwx------ 2 root root 6 May 30 23:39 proxy_temp drwxr-xr-x 2 root root 19 May 30 23:37 sbin drwx------ 2 root root 6 May 30 23:39 scgi_temp drwx------ 2 root root 6 May 30 23:39 uwsgi_temp [root@docker_host_0 opt]#
編輯nginx配置文件:
docker內部實現了服務發現(service discovery)功能,對連接至同一網絡驅動下的容器自動提供名稱解析。本例中,webapp服務成功啟動后,可以被proxy服務的nginx識別,因此服務名稱或別名均可以作為nginx中proxy_pass或upstream的參數。
user www www; worker_processes auto; pid logs/nginx.pid; error_log logs/error.log warn; worker_rlimit_nofile 51200; events { use epoll; worker_connections 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.0.1; charset utf-8; client_max_body_size 75M; location / { proxy_pass http://tomcat-app-0; } access_log logs/webapp-access.log main; } }
測試nginx配置文件:
[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 ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@docker_host_0 opt]#
啟動服務:
docker-compose up命令用于啟動服務:
默認啟動配置文件內定義的所有服務,可以顯式指定服務名,以啟動特定的服務。若配置文件中指定的鏡像名稱不存在,則默認首先執行編譯(build)。
-d/--detach用于指定容器后臺運行,等同于docker run命令的-d/--detach選項。
--scale用于指定相應服務的容器數量,格式為服務名=數量。
[root@docker_host_0 opt]# docker-compose -f tomcat-with-nginx-compose.yml up -d --scale webapp=3 Creating network "opt_default" with the default driver Creating opt_webapp_1 ... done Creating opt_webapp_2 ... done Creating opt_webapp_3 ... done Creating opt_proxy_1 ... done [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6b55fe98a99c tengine_nginx:2.3.0 "/bin/sh -c 'nginx -…" 10 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp, 443/tcp opt_proxy_1 0617d640c60a centos:latest "bin/catalina.sh run" 11 seconds ago Up 9 seconds opt_webapp_2 c85f2de181cd centos:latest "bin/catalina.sh run" 11 seconds ago Up 10 seconds opt_webapp_3 2517e03f11c9 centos:latest "bin/catalina.sh run" 11 seconds ago Up 10 seconds opt_webapp_1 [root@docker_host_0 opt]#
docker-compose默認創建bridge模式的網絡:
[root@docker_host_0 opt]# docker network ls NETWORK ID NAME DRIVER SCOPE cb90714e47b3 bridge bridge local a019d8b63640 host host local bb7095896ade none null local 80ce8533b964 opt_default bridge local [root@docker_host_0 opt]#
查看容器內的進程運行情況:
docker-compose top可以指定服務名稱,以查看特定服務內的進程運行情況。
[root@docker_host_0 opt]# docker-compose -f tomcat-with-nginx-compose.yml top opt_proxy_1 UID 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 0 00:28 ? 00:00:00 nginx: worker process 1000 13739 13674 0 00:28 ? 00:00:00 nginx: worker process opt_webapp_1 UID 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 start opt_webapp_2 UID 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 start opt_webapp_3 UID 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]#
訪問web服務,請求被調度至服務內的每個容器:
[root@docker_host_0 opt]# ss -atn | grep 80 LISTEN 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; done [root@docker_host_0 opt]# [root@docker_host_0 opt]# cat /opt/apps/app_0/source/logs/localhost_access_log.$(date +%F).txt 172.20.0.3:80 172.20.0.5:42430 [31/May/2019:00:32:16 +0000] "GET / HTTP/1.0" 200 11184 172.20.0.3:80 172.20.0.5:42436 [31/May/2019:00:32:16 +0000] "GET / HTTP/1.0" 200 11184 172.20.0.4:80 172.20.0.5:45098 [31/May/2019:00:32:16 +0000] "GET / HTTP/1.0" 200 11184 172.20.0.4:80 172.20.0.5:45122 [31/May/2019:00:32:16 +0000] "GET / HTTP/1.0" 200 11184 172.20.0.2:80 172.20.0.5:59294 [31/May/2019:00:32:16 +0000] "GET / HTTP/1.0" 200 11184 172.20.0.2:80 172.20.0.5:59306 [31/May/2019:00:32:16 +0000] "GET / HTTP/1.0" 200 11184 [root@docker_host_0 opt]#
擴充服務內的容器數量:
對運行中的服務再次執行docker-compose up命令時,--scale用于在現有基礎上動態增加或減小服務內的容器數量。
[root@docker_host_0 opt]# docker-compose -f tomcat-with-nginx-compose.yml up -d --scale webapp=6 Starting opt_webapp_1 ... done Starting opt_webapp_2 ... done Starting opt_webapp_3 ... done Creating opt_webapp_4 ... done Creating opt_webapp_5 ... done Creating opt_webapp_6 ... done opt_proxy_1 is up-to-date [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b9fc74985a13 centos:latest "bin/catalina.sh run" 9 seconds ago Up 7 seconds opt_webapp_4 29e9837c7b4d centos:latest "bin/catalina.sh run" 9 seconds ago Up 7 seconds opt_webapp_5 5e0a0611bb2f centos:latest "bin/catalina.sh run" 9 seconds ago Up 8 seconds opt_webapp_6 6b55fe98a99c tengine_nginx:2.3.0 "/bin/sh -c 'nginx -…" 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp, 443/tcp opt_proxy_1 0617d640c60a centos:latest "bin/catalina.sh run" 3 minutes ago Up 3 minutes opt_webapp_2 c85f2de181cd centos:latest "bin/catalina.sh run" 3 minutes ago Up 3 minutes opt_webapp_3 2517e03f11c9 centos:latest "bin/catalina.sh run" 3 minutes ago Up 3 minutes opt_webapp_1 [root@docker_host_0 opt]#
移除服務:
docker-compose down命令用于移除服務,包括停止與移除與服務相關聯的容器與網絡,另可指定--rmi與-v/--volumes選項移除相關聯的數據卷與鏡像。
[root@docker_host_0 opt]# docker-compose -f tomcat-with-nginx-compose.yml down Stopping opt_webapp_4 ... done Stopping opt_webapp_5 ... done Stopping opt_webapp_6 ... done Stopping opt_proxy_1 ... done Stopping opt_webapp_2 ... done Stopping opt_webapp_3 ... done Stopping opt_webapp_1 ... done Removing opt_webapp_4 ... done Removing opt_webapp_5 ... done Removing opt_webapp_6 ... done Removing opt_proxy_1 ... done Removing opt_webapp_2 ... done Removing opt_webapp_3 ... done Removing opt_webapp_1 ... done Removing network opt_default [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker container ls -a CONTAINER 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 ls NETWORK ID NAME DRIVER SCOPE cb90714e47b3 bridge bridge local a019d8b63640 host host local bb7095896ade none null local [root@docker_host_0 opt]#
宿主機docker_host_0創建群集,docker_host_1以管理角色加入群集:
[root@docker_host_0 opt]# docker swarm init Swarm 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:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker swarm join-token manager To 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 -r 3.10.0-957.12.2.el7.x86_64 [root@docker_host_1 ~]# [root@docker_host_1 ~]# docker -v Docker 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:2377 This node joined a swarm as a manager. [root@docker_host_1 ~]# [root@docker_host_1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION u9siv3gxc4px3xa85t5tybv68 docker_host_0 Ready Active Leader 18.09.6 qhgpqw9n5wwow1zfzji69eac0 * docker_host_1 Ready Active Reachable 18.09.6 [root@docker_host_1 ~]#
在docker_host_0節點上導出tengine_nginx:2.3.0鏡像,并傳輸至docker_host_1節點:
[root@docker_host_0 opt]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE tengine_nginx 2.3.0 9404e1b71b70 About an hour ago 340MB centos 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:/opt nginx.tar 100% 337MB 83.1MB/s 00:04 [root@docker_host_0 opt]#
在docker_host_1節點上導入tengine_nginx:2.3.0鏡像,并設置與docker_host_0節點相同的掛載源路徑:
[root@docker_host_1 ~]# cd /opt/ [root@docker_host_1 opt]# [root@docker_host_1 opt]# ls apache-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.tar d69483a6face: Loading layer 209.5MB/209.5MB 717661697400: Loading layer 144.3MB/144.3MB Loaded image: tengine_nginx:2.3.0 [root@docker_host_1 opt]# [root@docker_host_1 opt]# docker image ls -a REPOSITORY TAG IMAGE ID CREATED SIZE tengine_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/pattern="%h %l %u %t/pattern="%A:%{local}p %a:%{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]#
在docker_host_0節點上編輯服務編排配置文件:
volumes與port使用長語法(long syntax)格式指定掛載點與端口。
services.服務名.deploy指定服務的運行模式(mode),副本數量(replicas),重啟策略(restart_policy),服務所在節點(placement)。
[root@docker_host_0 opt]# vi tomcat-with-nginx-stack.yml version: "3.7" services: 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
部署服務,名稱為web-cluster:
[root@docker_host_0 opt]# docker stack deploy -c tomcat-with-nginx-stack.yml web-cluster Creating network web-cluster_default Creating service web-cluster_webapp Creating service web-cluster_proxy [root@docker_host_0 opt]#
副本服務webapp被分配至2個節點,全局服務proxy按約束條件(constraints)被分配至docker_host_0節點:
[root@docker_host_0 opt]# docker stack ls NAME SERVICES ORCHESTRATOR web-cluster 2 Swarm [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS njg5ngjjp9gi web-cluster_proxy global 1/1 tengine_nginx:2.3.0 *:80->80/tcp wc2uv0zllneo web-cluster_webapp replicated 3/3 centos:latest [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker service ps web-cluster_webapp ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS nh0pei38ikf7 web-cluster_webapp.1 centos:latest docker_host_1 Running Running about a minute ago otzusftmorjr web-cluster_webapp.2 centos:latest docker_host_1 Running Running about a minute ago tmjkmrmtbx9g 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_proxy ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS tal0jywnmukv web-cluster_proxy.u9siv3gxc4px3xa85t5tybv68 tengine_nginx:2.3.0 docker_host_0 Running Running 53 seconds ago [root@docker_host_0 opt]#
stack默認使用swarm模式創建的overlay網絡:
[root@docker_host_0 opt]# docker network ls NETWORK ID NAME DRIVER SCOPE b728df2e4b85 bridge bridge local dfe3ba6e0df5 docker_gwbridge bridge local a019d8b63640 host host local mxcmpb9uzjy2 ingress overlay swarm bb7095896ade none null local xxl3uk5r7s7v web-cluster_default overlay swarm [root@docker_host_0 opt]#
通過docker_host_0與docker_host_1節點均可訪問被代理的web服務:
[root@docker_host_1 ~]# ss -atn | grep :80 LISTEN 0 128 :::80 :::* [root@docker_host_1 ~]# [root@docker_host_1 ~]# curl -I -o /dev/null -s -w %{http_code} 192.168.9.168 200 [root@docker_host_1 ~]# curl -I -o /dev/null -s -w %{http_code} 192.168.9.169 200 [root@docker_host_1 ~]#
[root@docker_host_0 opt]# ss -atn | grep :80 LISTEN 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.168 200 [root@docker_host_0 opt]# curl -I -o /dev/null -s -w %{http_code} 192.168.9.169 200 [root@docker_host_0 opt]#
移除服務:
對于通過swarm stack方式編排的服務,部署操作為docker stack deploy 服務名,移除操作為docker stack rm 服務名。移除服務后,與之相關聯的網絡驅動也隨之被移除。
[root@docker_host_0 opt]# docker stack ls NAME SERVICES ORCHESTRATOR web-cluster 2 Swarm [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker stack rm web-cluster Removing service web-cluster_proxy Removing service web-cluster_webapp Removing network web-cluster_default [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker stack ls NAME SERVICES ORCHESTRATOR [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS [root@docker_host_0 opt]# [root@docker_host_0 opt]# docker network ls NETWORK ID NAME DRIVER SCOPE b728df2e4b85 bridge bridge local dfe3ba6e0df5 docker_gwbridge bridge local a019d8b63640 host host local mxcmpb9uzjy2 ingress overlay swarm bb7095896ade none null local [root@docker_host_0 opt]#
depends_on指令僅可用于docker-compose方式指定鏡像的構建順序,以及容器的啟動與停止順序。若需要解決容器內應用程序間的依賴關系,則需手動實現容器內命令的慢啟動(如在容器的腳本內執行目標端口存活或url的可用性檢測,以決定是否開啟本端服務),或借助于諸如dockerize之類的第三方工具。
stack與compose均通過yaml或json格式的配置文件進行服務的編排,區別主要包括:
某些指令在兩種方式下不兼容,如build,deploy,depends_on,restart_policy等。
stack由docker引擎內置,須開啟swarm模式,組成同一服務的多個容器可能跨越多個宿主機,因此要求相應的鏡像必須存在于宿主機本地或可訪問的倉庫中;compose需要額外安裝,無須開啟swarm模式,所有容器均位于當前單個宿主機。
stack僅可根據預先已編譯的鏡像部署服務;compose支持鏡像的編譯與服務的部署,二者可同時執行,或單獨執行。
以上就是如何進行docker CE on Linux中的服務編排,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。