- Docker Swarm 采取集群管理,统一部署
- 弹性伸缩:可以通过策略的方式随意增加、删减容器数量
- 多主机网络:Swarm内置多主机网络,实现多主机中的容器间互通。(overlay 网络)
- 服务发现:可以通过 Swarm 内置的 DNS 服务器查询集群中每个运行的容器。
- 负载均衡:实现服务副本负载均衡,提供入口访问。也可以将服务入口暴露给外部负载
实验环境
192.168.43.124 node1
192.168.43.60 node2
192.168.43.160 node3
关闭防火墙,添加主机名称 (三台相同操作)
[root@localhost ~]# setenforce 0
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables-save
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# vim /etc/hosts
192.168.43.124 node1
192.168.43.60 node2
192.168.43.160 node3
修改主机名(三台)
[root@localhost ~]# hostname node1
[root@localhost ~]# bash
[root@localhost ~]# hostname node2
[root@localhost ~]# bash
[root@localhost ~]# hostname node3
[root@localhost ~]# bash
开启路由转发(三台)
[root@node3 ~]# echo "net.ipv4.ip_forward = 1 " >> /etc/sysctl.conf
[root@node3 ~]# sysctl -p
net.ipv4.ip_forward = 1
ssh配置免密登陆模块(三台相同操作)
生成密钥对
[root@node1 ~]# ssh-keygen
上传公钥
[root@node1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
[root@node1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
[root@node1 ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
swarm集群模块
初始化集群,选择一台主机为manager,
下面的意思是添加一台普通节点服务器请在节点服务器上面执行(1)命令
要想添加manager节点请在当前主机执行(2)命令,然后再manager服务器执行(3)命令
[root@node1 ~]# docker swarm init --advertise-addr 192.168.43.124
[root@node2 ~]# docker swarm join --token SWMTKN-1-2fk3s2kbyfoia8mfsk5u8xzq6fioe28fm0pcx0gbmyg1zr4p2z-14co3304n4rfbmgzpa39g1b0m 192.168.43.124:2377
This node joined a swarm as a worker.
把node3添加到swarm中身份为:manager
[root@node3 ~]# docker swarm join --token SWMTKN-1-2fk3s2kbyfoia8mfsk5u8xzq6fioe28fm0pcx0gbmyg1zr4p2z-4fx4hltvhxtci01ygwwlf73mi 192.168.43.124:2377
This node joined a swarm as a manager.
docker swarm内部命令
查看当前主机的swarm集群信息 [root@node2 ~]# docker info 升级:把节点升级到manager
[root@node1 ~]# docker node promote node2 Node node2 promoted to a
manager in the swarm. 降级:把manager降低到节点服务器 [root@node1 ~]# docker node
demote node2 Manager node2 demoted in the swarm. [root@node1 ~]#
docker node demote node3 Manager node3 demoted in the swarm.
退出集群状态的意思 [root@node2 ~]# docker swarm leave -f
搭建私有仓库模块
开启路由转(三台)
[root@node1 ~]# sysctl -p
net.ipv4.ip_forward = 1
下载私有库镜像,创建私有库的镜像家目录并且生成容器
[root@node1 ~]# docker pull registry:2
[root@node1 ~]# mkdir -p /opt/data/registry
[root@node1 ~]# docker run -itd -p 5000:5000 --restart always -v /opt/data/registry/:/var/lib/registry --name registry registry:2
6c3f7e3690033ac2fb775c041190adeb10bed3e3b793776a336ffdcf6f66e6ed
验证是否成功
[root@node1 ~]# curl 192.168.43.124:5000/v2/_catalog
{"repositories":[]}
修改docker的配置文件,指定私有库服务器
[root@node1 ~]# vim /usr/lib/systemd/system/docker.service(添加蓝字部分)
14 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd /containerd.sock --insecure-registry 192.168.43.124:5000
scp复制给其它两台服务器
[root@node1 ~]# scp /usr/lib/systemd/system/docker.service node2:/usr/lib/systemd/system/docker.service
[root@node1 ~]# scp /usr/lib/systemd/system/docker.service node3:/usr/lib/systemd/system/docker.service
三台重新加载docker配置文件和重启docker服务
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl restart docker
修改镜像标签并且上传到镜像私有库中
[root@node1 ~]# docker tag httpd:latest 192.168.43.124:5000/httpd
[root@node1 ~]# docker tag busybox:latest 192.168.43.124:5000/busybox
[root@node1 ~]# docker tag centos:latest 192.168.43.124:5000/centos
[root@node1 ~]# docker push 192.168.43.124:5000/centos
[root@node1 ~]# docker push 192.168.43.124:5000/httpd
[root@node1 ~]# docker push 192.168.43.124:5000/busybox
创建网卡,并且直接同步给node2,node2
[root@node1 ~]# docker network create --driver overlay docker
在node2和node3上查看(网卡同步,同步的比较慢但是不影响以下操作)
[root@node1 ~]# docker pull dockersamples/visualizer
[root@node1 ~]# docker tag dockersamples/visualizer 192.168.43.124:5000/visualizer
node2和node3操作 下载镜像
[root@node2 ~]# docker pull 192.168.43.124:5000/visualizer
[root@node3 ~]# docker pull 192.168.43.124:5000/visualizer
生成swarm集群图形化
[root@node1 ~]# docker run -itd -p 8888:8080 -e HOST=192.168.43.124 -e POST=8080 -v /var/run/docker.sock:/var/run/docker.sock --name visualizer 192.168.43.124:5000/visualizer
访问192.168.43.124:8888
部署 service 的命令形式与运行容器的 docker run 很相似,–name 为 service 命名,httpd 为镜像的名字。
[root@node1 ~]# docker service create --replicas 1 --network docker --name web1 -p 8000:80 -p 10122:22 192.168.43.124:5000/httpd
[root@node1 ~]# docker service create --replicas 2 --network docker --name web2 -p 8001:80 -p 10123:22 192.168.43.124:5000/httpd
[root@node1 ~]# docker service create --replicas 3 --network docker --name web3 -p 8002:80 -p 10124:22 192.168.43.124:5000/httpd
[root@node1 ~]# docker service create --mode global --network docker --name web4 -p 8003:80 -p 10125:22 192.168.43.124:5000/httpd
[root@node1 ~]# docker service scale web_server1=5
驱离:让manager的容器都去worer服务器上面
[root@node1 ~]# docker node update --availability drain node1
[root@node1 ~]# docker service scale web_server1=3