容器基础(八): 使用docker swarm部署程序

时间:2023-03-09 22:39:25
容器基础(八): 使用docker swarm部署程序

环境

基于上一节的env/server:v0.1, env/worker:v0.1镜像, 在基于debian8.2的两台机器上测试部署docker swarm.

docker service部署

➜  ~  docker service create --replicas  --name server -e APP_PORT= --network docker-net --publish : env/server:v0.
image env/server:v0. could not be accessed on a registry to record
its digest. Each node will access env/server:v0. independently,
possibly leading to different nodes running different
versions of the image. pzyx887jb112isgkyorarvt7t
overall progress: out of tasks
/: running [==================================================>]
verify: Service converged
➜ ~
➜ ~ docker service create --replicas --name worker -e APP_DOMAIN=server -e APP_PORT= --network docker-net env/worker:v0.
image env/worker:v0. could not be accessed on a registry to record
its digest. Each node will access env/worker:v0. independently,
possibly leading to different nodes running different
versions of the image. wavv2ie7ad9je6s33q82f3skv
overall progress: out of tasks
/: running [==================================================>]
/: running [==================================================>]
verify: Service converged
➜ ~ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
pzyx887jb112 server replicated / env/server:v0. *:->/tcp
wavv2ie7ad9j worker replicated / env/worker:v0.
➜ ~ docker service ps worker
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
hyjcg25ymfvx worker. env/worker:v0. debian-vm Running Running seconds ago
zpx2oq4q0dhu worker. env/worker:v0. debian Running Running seconds ago  
 ➜  ~  docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9b1931acf12 env/worker:v0. "python worker.py" minutes ago Up minutes worker..zpx2oq4q0dhuqgvtkt0mm8dyt
994e4779c252 env/server:v0. "python server.py" minutes ago Up minutes server..v5mu7wa8tfswayzwjh3u1qtpk
➜ ~ docker exec 994e4779c252 cat /env/server/log/server.log
-- :: [INFO] Waiting for connection...
-- :: [INFO] ('10.0.0.5', ) connected
-- :: [INFO] ('10.0.0.4', ) connected
➜ ~ docker exec a9b1931acf12 cat /env/worker/log/worker.log
-- :: [INFO] [+] ('10.0.0.2', ) connected
-- :: [INFO] [recv] hello, docker!
➜ ~ docker exec a9b1931acf12 ping -c server
PING server (10.0.0.2) () bytes of data.
bytes from 10.0.0.2: icmp_seq= ttl= time=0.117 ms
bytes from 10.0.0.2: icmp_seq= ttl= time=0.205 ms --- server ping statistics ---
packets transmitted, received, % packet loss, time 1000ms
rtt min/avg/max/mdev = 0.117/0.161/0.205/0.044 ms

manager node上测试服务可用和可发现

 root@debian-vm:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94b6e94a4f4c env/worker:v0. "python worker.py" About a minute ago Up seconds worker..hyjcg25ymfvx2u6ghy3ng7g1y
root@debian-vm:~# docker exec 94b6e94a4f4c cat /env/worker/log/worker.log
-- :: [INFO] [+] ('10.0.0.2', ) connected
-- :: [INFO] [recv] hello, docker!
root@debian-vm:~# docker exec 94b6e94a4f4c ping -c server
PING server (10.0.0.2) () bytes of data.
bytes from 10.0.0.2: icmp_seq= ttl= time=0.058 ms
bytes from 10.0.0.2: icmp_seq= ttl= time=0.057 ms --- server ping statistics ---
packets transmitted, received, % packet loss, time 1002ms
rtt min/avg/max/mdev = 0.057/0.057/0.058/0.007 ms
root@debian-vm:~#

worker node上测试服务可用和可发现

# 从worker node上验证publish端口
root@debian-vm:/env/worker# export APP_PORT=
root@debian-vm:/env/worker# export APP_DOMAIN=192.168.2.118
root@debian-vm:/env/worker# python worker.py
[+] ('192.168.2.118', ) connected
[+][recv] hello, docker!
^C
[-] signal() received, exit!
root@debian-vm:/env/worker# # 从manager node上验证publish端口
➜ worker export APP_PORT=
➜ worker export APP_DOMAIN=192.168.2.118
➜ worker python worker.py
[+] ('192.168.2.118', ) connected
[+][recv] hello, docker!
^C
[-] signal() received, exit!
➜ worker docker exec 994e4779c252 cat /env/server/log/server.log
-- :: [INFO] Waiting for connection...
-- :: [INFO] ('10.0.0.5', ) connected
-- :: [INFO] ('10.0.0.4', ) connected
-- :: [INFO] ('10.255.0.2', ) connected
-- :: [INFO] ('10.255.0.2', ) closed
-- :: [INFO] ('10.255.0.2', ) connected
-- :: [INFO] ('10.255.0.2', ) closed
➜ worker

验证服务的publish端口

docker stack部署

#docker-compose.yml

version: ""
services:
server:
image: env/server:v0.
build:
context: /app/env/server
dockerfile: Dockerfile
labels:
description: "tcp server test script"
ports:
- :${APP_PORT-}
- ${APP_PORT-}:${APP_PORT-}
restart: always
environment:
- APP_PORT=${APP_PORT-}
networks:
- overlay
deploy:
replicas: worker:
image: env/worker:v0.
build: /app/env/worker
labels:
description: "tcp client test script"
links:
- server
environment:
- APP_PORT=${APP_PORT-}
- APP_DOMAIN=server
networks:
- overlay
depends_on:
- server
deploy:
replicas:
restart_policy:
condition: on-failure worker_test:
image: env/worker_test:v0.
networks:
- overlay
deploy:
replicas: networks:
overlay:
➜  /env  echo $APP_PORT

➜  /env  docker stack deploy -c docker-compose.yml  env
Ignoring unsupported options: build, links, restart Creating network env_overlay
Creating service env_worker_test
Creating service env_server
Creating service env_worker
➜ /env docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
lwl5gpaseyz5 env_server replicated / env/server:v0. *:->/tcp, *:->/tcp
0ywtuy79e5af env_worker replicated / env/worker:v0.
a2malzyjs8a8 env_worker_test replicated / env/worker_test:v0.
➜ /env docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6534e1ab7a0f env/worker:v0. "python worker.py" About a minute ago Up About a minute env_worker..o8oo0c2do3xi17bb2y1jyxyvt
a6f05a1180fa env/worker_test:v0. "python worker.py" About a minute ago Up About a minute env_worker_test..xyriek6uv0jt5u8grfh2r2nip
dd178c531cab env/worker_test:v0. "python worker.py" About a minute ago Up About a minute env_worker_test..e5akyik75zru3a3vm7t0uow42
➜ /env docker exec 6534e1ab7a0f cat /env/worker/log/worker.log
-- :: [INFO] [+] ('10.0.3.8', ) connected
-- :: [INFO] [recv] hello, docker!
 root@debian-vm:/# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54816f125b5d env/worker:v0. "python worker.py" minutes ago Up minutes env_worker..583qka4f0we1tr8gzfbfwd8xq
b148b68c60ac env/worker:v0. "python worker.py" minutes ago Up minutes env_worker..pajhjlgfcfpkcvtgbl4pd9iec
4501dddabc12 env/server:v0. "python server.py" minutes ago Up minutes env_server..r8rffhrzds0p5qreiz3zskglt
423f9f7fe5bf env/worker_test:v0. "python worker.py" minutes ago Up minutes env_worker_test..yafiu0eg5bmmv47do4dwtbcdm
root@debian-vm:/# docker exec 4501dddabc12 cat /env/server/log/server.log
-- :: [INFO] Waiting for connection...
-- :: [INFO] ('10.0.3.3', ) connected
-- :: [INFO] ('10.0.3.3', ) connected
-- :: [INFO] ('10.0.3.2', ) connected
root@debian-vm:/#

manager node验证连接建立成功

# worker node启动客户端
➜ worker export APP_PORT=
➜ worker export APP_DOMAIN=192.168.2.118
➜ worker python worker.py
[+] ('192.168.2.118', ) connected
[+][recv] hello, docker!
^C
[-] signal() received, exit!
➜ worker # manager node查看日志, 验证连接成功
root@debian-vm:/# docker exec 4501dddabc12 cat /env/server/log/server.log
-- :: [INFO] Waiting for connection...
-- :: [INFO] ('10.0.3.3', ) connected
-- :: [INFO] ('10.0.3.3', ) connected
-- :: [INFO] ('10.0.3.2', ) connected
-- :: [INFO] ('10.255.0.2', ) connected
-- :: [INFO] ('10.255.0.2', ) closed
root@debian-vm:/#

验证服务的publish端口