什么是Docker Swarm?
-
对比Docker
前面我们介绍过Docker可以理解成是一个我们的服务的独立运行的容器,那么在实际工作中,我们的系统可能是一个微服务应用,系统中根据业务拆分成多个模块,可以理解一个模块对应一组运行着相同镜像的的Docker容器,这就是单个Docker,而Swarm就是这个系统中,能够管理所有的容器之间的路由、负载均衡、服务发现,以及容器的伸缩(可以理解为我们一个服务进行扩/缩容)、日志、配置文件的编排工具。
-
对比Docker-Compose
Docker-Compose类似与我们生活中的管家,它仅仅能够管理一个主机上的所有容器,他可以一次性启动多个容器。而Docker-Swarm可以跨主机去管理多个容器,并且当一个容器状态异常时候可以快速在其他机器上启动容器,保证服务的高可用!
-
对比Kubernetes
Kubernetes它本身的角色定位是和Docker Swarm 是一样的,也就是说他们负责的工作在容器领域来说是相同的部分,都是一个跨主机的容器管理平台,当然也有自己一些不一样的特点,k8s是谷歌公司根据自身的多年的运维经验研发的一款容器管理平台。而Docker Swarm则是由Docker 公司研发的。Kubernetes在Docker基础上封装了一层概念,所以他的功能相对于Swarm更强大,但是运维更复杂,Swarm则更容易上手。
综上所述大家应该知道Docker就是一个容器,Docker-Compose是一个单主机的编排工具,Docker-Swarm是一个多主机的编排工具!
-
Swarm架构组成 -
Docker Api 是基于Docker去管理容器 -
Swarm Manage CLi 是Swarm集群管理Api -
Leadership 这个是Swarm提供的HA -
Scheduler 则是Swarm的调度策略,以及过滤规则管理 -
Discovery Service 管理每个容器的服务的注册、发现
-
-
Swarm如何工作? Swarm节点分为管理节点(manager)跟 工作节点(worker),管理节点主要用于swarm集群的管理(也可以设置执行任务工作),Docker Swarm命令只能在管理节点上去操作,一个集群可以存在多个管理节点,但是只有一个管理节点可以成为Leader。工作节点是任务执行节点。 -
Manager节点: -
处理集群管理任务 -
维护集群状态 -
调度服务 -
Swarm 提供外部可调用的 API 接口
-
-
Worker节点: 实际执行我们服务的Docker容器
-
Docker Swarm命令
-
初始化集群
$ docker swarm init --advertise-addr 192.168.99.121
Swarm initialized: current node (bvz81updecsj6wjz393c09vti) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx \
172.17.0.2:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
-
加入集群
# 加入worker
$ docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx \
172.17.0.2:2377
# 加入manager
$ docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-7p73s1dx5in4tatdymyhg9hu2 \
172.17.0.2:2377
-
离开集群
# --force / -f 强制离开
$ docker swarm leave -f
Node left the default swarm.
-
查看集群节点信息
docker@manager:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
alz0od3rbkg552mkicfuezhkn * manager Ready Active Leader 19.03.12
jxf5u2hnurrgz90vnd9ay3y7p node-1 Ready Active 19.03.12
lldvm2slseyodo0t7ath5tzne node-2 Ready Active 19.03.12
-
swarm集群中创建服务
docker service create --name redis redis:3.0.6
dmu1ept4cxcfe8k8lhtux3ro3
$ docker service create --mode global --name redis2 redis:3.0.6
a8q9dasaafudfs8q8w32udass
$ docker service ls
ID NAME MODE REPLICAS IMAGE
dmu1ept4cxcf redis replicated 1/1 redis:3.0.6
a8q9dasaafud redis2 global 1/1 redis:3.0.6
-
查看swarm集群服务信息
$ docker service inspect redis
-
查看服务的日志
$ docker service logs -f redis
-
查看启动所有服务
$ docker service ls
ID NAME MODE REPLICAS IMAGE
c8wgl7q4ndfd frontend replicated 5/5 nginx:alpine
dmu1ept4cxcf redis replicated 3/3 redis:3.0.6
iwe3278osahj mongo global 7/7 mongo:3.3
hh08h9uu8uwr job replicated-job 1/1 (3/5 completed) nginx:latest
-
查看服务启动的具体容器
$ docker service ps redis
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
0qihejybwf1x redis.1 redis:3.0.5 manager1 Running Running 8 seconds
bk658fpbex0d redis.2 redis:3.0.5 worker2 Running Running 9 seconds
5ls5s5fldaqg redis.3 redis:3.0.5 worker1 Running Running 9 seconds
-
删除服务
$ docker service rm redis
redis
$ docker service ls
ID NAME MODE REPLICAS IMAGE
-
扩容/缩容服务(可以动态添加服务提供节点,在swarm节点集群中启动) docker service create --mode global ... : 启动服务🈯️定--mode global 则不能使用此命令,global创建一个服务会在每个节点都启动一个
# frontend服务扩容50个节点
$ docker service scale frontend=50
frontend scaled to 50
-
指定参数更新 具体参考 https://docs.docker.com/engine/reference/commandline/service_update/
Swarm实战演练
使用Dcoker-Machine创建3台虚拟机
1.创建三台主机
docker-machine create --driver=virtualbox manager
docker-machine create --driver=virtualbox node-1
docker-machine create --driver=virtualbox node-2
[root@docker ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
manager - virtualbox Running tcp://192.168.99.100:2376 v19.03.12
node-1 - virtualbox Running tcp://192.168.99.103:2376 v19.03.12
node-2 - virtualbox Running tcp://192.168.99.102:2376 v19.03.12
2.创建好主机后初始化Swarm manager节点
[root@docker ~]# docker-machine ssh manager
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
# --advertise-addr 改成 manager 节点ip
docker@manager:~$ docker swarm init --advertise-addr 192.168.99.100
Swarm initialized: current node (alz0od3rbkg552mkicfuezhkn) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-4mocuep5w9jhd05bww7h0rzwhd1ayd1o3i2lncwkk2cw1rv9d2-9ed4ny56zt7ygw69dok39id8w 192.168.99.100:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
这样就初始化好了manager是不是很简单?
3.切换node-1、node-2加入集群
# node-1加入swarm集群、
[root@docker ~]# docker-machine ssh node-1
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@node-1:~$ docker swarm join --token SWMTKN-1-4mocuep5w9jhd05bww7h0rzwhd1ayd1o3i2lncwkk2cw1rv9d2-9ed4ny56zt7ygw69dok39id8w 192.168.99.100:2377
This node joined a swarm as a worker.
#node-2加入swarm集群
[root@docker ~]# docker-machine ssh node-2
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@node-2:~$ docker swarm join --token SWMTKN-1-4mocuep5w9jhd05bww7h0rzwhd1ayd1o3i2lncwkk2cw1rv9d2-9ed4ny56zt7ygw69dok39id8w 192.168.99.100:2377
This node joined a swarm as a worker
-
在manager查看当前集群node状态
docker@manager:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
alz0od3rbkg552mkicfuezhkn * manager Ready Active Leader 19.03.12
jxf5u2hnurrgz90vnd9ay3y7p node-1 Ready Active 19.03.12
lldvm2slseyodo0t7ath5tzne node-2 Ready Active 19.03.12
-
创建一个nginx服务包含3个副本
# --replicas 指定容器个例数
docker@manager:~$ docker service create --name test-nginx --publish 8080:80 --replicas=3 nginx
ya3q60llnj62w141aaekggyl2
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
docker@manager:~$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
ya3q60llnj62 test-nginx replicated 3/3 nginx:latest *:8080->80/tcp
这时候创建好了3个nginx服务,这时候我们只要访问manager的ip:8080 则可以随机访问任意一个nginx服务,是不是类似nginx的负载均衡?
# 发送多次请求nginx服务
curl 192.168.99.100:8080
#查看日志
docker@manager:~$ docker service logs -f ya3q60llnj62
test-nginx.1.h50ridnlpsim@node-2 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
test-nginx.1.h50ridnlpsim@node-2 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
test-nginx.1.h50ridnlpsim@node-2 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
test-nginx.1.h50ridnlpsim@node-2 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
test-nginx.1.h50ridnlpsim@node-2 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
test-nginx.1.h50ridnlpsim@node-2 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
test-nginx.1.h50ridnlpsim@node-2 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
test-nginx.1.h50ridnlpsim@node-2 | /docker-entrypoint.sh: Configuration complete; ready for start up
test-nginx.1.h50ridnlpsim@node-2 | 10.0.0.2 - - [25/Mar/2021:14:59:30 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
test-nginx.1.h50ridnlpsim@node-2 | 10.0.0.2 - - [25/Mar/2021:14:59:33 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
test-nginx.3.mw75ab00bfs1@node-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
test-nginx.3.mw75ab00bfs1@node-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
test-nginx.3.mw75ab00bfs1@node-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
test-nginx.3.mw75ab00bfs1@node-1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
test-nginx.3.mw75ab00bfs1@node-1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
test-nginx.3.mw75ab00bfs1@node-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
test-nginx.3.mw75ab00bfs1@node-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
test-nginx.3.mw75ab00bfs1@node-1 | /docker-entrypoint.sh: Configuration complete; ready for start up
test-nginx.3.mw75ab00bfs1@node-1 | 10.0.0.2 - - [25/Mar/2021:14:59:28 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
test-nginx.3.mw75ab00bfs1@node-1 | 10.0.0.2 - - [25/Mar/2021:14:59:31 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
test-nginx.3.mw75ab00bfs1@node-1 | 10.0.0.2 - - [25/Mar/2021:14:59:34 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
test-nginx.2.drsv2s4cnf9y@manager | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
test-nginx.2.drsv2s4cnf9y@manager | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
test-nginx.2.drsv2s4cnf9y@manager | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
test-nginx.2.drsv2s4cnf9y@manager | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
test-nginx.2.drsv2s4cnf9y@manager | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
test-nginx.2.drsv2s4cnf9y@manager | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
test-nginx.2.drsv2s4cnf9y@manager | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
test-nginx.2.drsv2s4cnf9y@manager | /docker-entrypoint.sh: Configuration complete; ready for start up
test-nginx.2.drsv2s4cnf9y@manager | 10.0.0.2 - - [25/Mar/2021:14:59:29 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
test-nginx.2.drsv2s4cnf9y@manager | 10.0.0.2 - - [25/Mar/2021:14:59:32 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
至此所有请求负载到后端3个nginx服务上。
结束
下篇结合微服务说swarm如何可以代替微服务的功能!
欢迎关注公众号! 公众号回复:
入群
,扫码加入我们交流群!