swarm
Swarm是Docker公司在2014年12月初发布的一套较为简单的工具,用来管理Docker集群,它将一群Docker宿主机变成一个单一的,虚拟的主机。Swarm使用标准的Docker API接口作为其前端访问入口,换言之,各种形式的Docker Client(docker client in go, docker_py, docker等)均可以直接与Swarm通信。
Swarm deamon只是一个调度器(Scheduler)加路由器(router),Swarm自己不运行容器,它只是接受docker客户端发送过来的请求,调度适合的节点来运行容器,这意味着,即使Swarm由于某些原因挂掉了,集群中的节点也会照常运行,当Swarm重新恢复运行之后,它会收集重建集群信息
swarm集群
使用的是docker自带的swarm mode,也就是docker集群的管理和编排。所谓的编排就是指多台集群的管理, 主机的配置,容器的调度等。
swarm mode是docker engine中自带的一种模式,很容易使用,并且无须安装其他的软件。 在使用swarm mode的时候,几台主机上都要先安装好docker。
搭建swarm集群
1.初始化
[root@server1 ~]# docker swarm init
Swarm initialized: current node (3w9leghq2cy8eu01wufxs12ft) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1vfrxr6d0323fcoxpdb5keqywajangxu8i8lq0up2mo09boy36-ausloqbfrkfkbb5vlkad4ya18 172.25.68.1:2377 ##token作为通讯的密钥
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
2.docker2和docker3加入swarm集群中
[root@server2 docker]# docker swarm join --token SWMTKN-1-1vfrxr6d0323fcoxpdb5keqywajangxu8i8lq0up2mo09boy36-ausloqbfrkfkbb5vlkad4ya18 172.25.68.1:2377
This node joined a swarm as a worker.
[root@server3 docker]# docker swarm join --token SWMTKN-1-1vfrxr6d0323fcoxpdb5keqywajangxu8i8lq0up2mo09boy36-ausloqbfrkfkbb5vlkad4ya18 172.25.68.1:2377
This node joined a swarm as a worker.
[root@server1 ~]# docker node ls ##查看节点状态
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
3w9leghq2cy8eu01wufxs12ft * server1 Ready Active Leader 18.06.1-ce
7xtxjg5e9byvmke00fbfpswxa server2 Ready Active 18.06.1-ce
v3d9r7sksrvte5iifb2fs4qnn server3 Ready Active 18.06.1-ce
3.都导入nginx镜像
[root@server1 ~]# docker load -i nginx.tar
[root@server2 ~]# docker load -i nginx.tar
[root@server3 ~]# docker load -i nginx.tar
4.创建swarm的网络
[root@server1 ~]# docker network create -d overlay webnet
w7kdnfsbi1w2gqyc8ocufyhd6
[root@server1 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
cfcf10b23f5d bridge bridge local
5a713ee1094b docker_gwbridge bridge local
af113c71e06b host host local
o0uztujanmpj ingress overlay swarm
cf56e94132cb none null local
w7kdnfsbi1w2 webnet overlay swarm
5.部署一个服务群集
#在manager节点部署nginx服务,服务数量为3个 ,使用nginx镜像
[root@server1 ~]# docker service create --name web --network webnet --publish 80:80 --replicas 3 nginx
[root@server1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
nbziwdj808nd web replicated 3/3 nginx:latest *:80->80/tcp
[root@server1 ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
g8rri3b9y6hk web.1 nginx:latest server2 Running Running about a minute ago
rnmx4cyafvdv web.2 nginx:latest server3 Running Running about a minute ago
y9pha70e4f4i web.3 nginx:latest server1 Running Running about a minute ago
6.添加 6 个nginx 服务
[root@server1 ~]# docker service scale web=6
[root@server1 ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
g8rri3b9y6hk web.1 nginx:latest server2 Running Running 2 minutes ago
rnmx4cyafvdv web.2 nginx:latest server3 Running Running 2 minutes ago
y9pha70e4f4i web.3 nginx:latest server1 Running Running 2 minutes ago
z0trqfutelz3 web.4 nginx:latest server2 Running Running 22 seconds ago
d2hky35dog9k web.5 nginx:latest server3 Running Running 21 seconds ago
xhzch8cz6xn7 web.6 nginx:latest server1 Running Running 20 seconds ago
[root@server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fccad5a3b596 nginx:latest "nginx -g 'daemon of…" 45 seconds ago Up 41 seconds 80/tcp web.6.xhzch8cz6xn7636zy5o7qic8u
bb6f1cddfa90 nginx:latest "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 80/tcp web.3.y9pha70e4f4if2r9ckwsn6lea
7.编写测试页面
[root@server1 ~]# vim index.html
server1
[root@server1 ~]# docker cp index.html bb6f1cddfa90:/usr/share/nginx/html
[root@server1 ~]# docker cp index.html fccad5a3b596:/usr/share/nginx/html
server2 server3同样操作,index.html内容分别为server2 server3
8.测试集群负载均衡
添加监控
1.导入镜像并创建监控
[root@server1 ~]# docker load -i visualizer.tar
[root@server1 ~]# docker service create \
> --name=viz \
> --publish=8080:8080/tcp \
> --constraint=node.role==manager \
> --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
> dockersamples/visualizer
2.查看监控和节点是否在线
[root@server1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
aejx2ocibogm viz replicated 1/1 dockersamples/visualizer:latest *:8080->8080/tcp
nbziwdj808nd web replicated 6/6 nginx:latest *:80->80/tcp
3.浏览器:输入172.25.68.1:8080,可以看到6个nginx的进程被平均分在了三台机子上
滚动更新
#添加新的镜像
[root@server1 ~]# docker load -i httpd.tar
[root@server2 ~]# docker load -i httpd.tar
[root@server3 ~]# docker load -i httpd.tar
[root@server1 ~]# docker service update --image httpd --update-delay 5s --update-parallelism 5 web
##每隔五秒更新,每次更新五个机子
2.测试
浏览器的监控镜像由nginx变成了httpd
高可用
1.停掉一个node
[root@server2 ~]# systemctl stop docker
2.测试
浏览器的监控显示server2故障,并且6个httpd进程被平均分到docker1和docker3上