一.环境准备
1.3台服务器,配置静态ip,主机名,绑定hosts
hostnamectl set-hostname <主机名>
1.1.1.3 node1
1.1.1.4 node2
1.1.1.5 node3
2.关闭selinux,iptables,firewalld
# iptables -F
# systemctl disable firewalld
# systemctl stop firewalld
# vim /etc/sysconfig/selinux
SELINUX=disabled
二.安装Docker
安装必要的工具
yum install -y yum-utils device-mapper-persistent-data lvm2
添加Docker的存储库
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装
yum install -y docker-ce
配置国内镜像源
在/etc/docker/
目录下新建文件daemon.json
配置内容如下
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
重新载入配置并启动Docker
systemctl daemon-reload
systemctl start docker
三.开始搭建Swarm集群
1.初始化集群,执行初始化的服务器默认为管理节点
docker swarm init --advertise-addr 1.1.1.3
Swarm initialized: current node (l9mveonpubd7dugdpbrraq1q3) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-6co0gt73819kggxcsnqfr7j32ner9tdl7wi3mf0dnvu0o2bknz-ablwzg4iahibpq6r9dquwppvr \
1.1.1.3:2377 --执行该条命令,以worker的身份加入集群
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
查看Swarm集群激活状态
docker info | grep -i swarm
Swarm: active
如果出现以下提示
WARNING: IPv4 forwarding is disabled
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Swarm: active
解决方法如下
修改/etc/sysctl.conf
文件,内容如下
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 1
执行sysctl -p
,生效
2.将其他服务器加入到集群
node2以管理者身份加入集群
docker swarm join \
--token SWMTKN-1-6co0gt73819kggxcsnqfr7j32ner9tdl7wi3mf0dnvu0o2bknz-cy4uh1dfa4l4s3cng0yg5lujn \
1.1.1.3:2377
node3以工作者身份加入集群
docker swarm join \
--token SWMTKN-1-6co0gt73819kggxcsnqfr7j32ner9tdl7wi3mf0dnvu0o2bknz-ablwzg4iahibpq6r9dquwppvr \
1.1.1.3:2377
查看集群成员
# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
5bq2l28mb6kdl88t4lzjdz6xy node2 Ready Active Reachable
l0j2psjlfs3hoywwjzlk785c2 node3 Ready Active
l9mveonpubd7dugdpbrraq1q3 * node1 Ready Active Leader
新建一个overlay网络
Swarm集群默认的ingress网络不支持服务发现,所以重新创建一个网络。
# docker network create --attachable --driver overlay microservice
rjxxb5gz6gbx2bc50077k9uby
查看集群所有网络
# docker network ls
NETWORK ID NAME DRIVER SCOPE
0aa88eeb09b2 bridge bridge local
38c8f825c2a8 docker_gwbridge bridge local
2df53c0f2885 host host local
sce8abmnyz7j ingress overlay swarm
rjxxb5gz6gbx microservice overlay swarm
2e8597069347 none null local
Docker Swarm集群搭建完成
四.搭建consul集群和server高可用
官方建议在运行Docker容器时,用host网络模式
1.在node1上执行,安装consul server
docker run -d -p 8300:8300 -p 8301:8301/tcp -p 8302:8302/tcp -p 8301:8301/udp \
-p 8302:8302/udp -p 8500:8500 -p 8600:8600/udp -p 8600:8600/tcp \
--restart=always \
-h node1 \
--name consul \
--network microservice \
-v /data/consul:/consul/data \
-v /etc/localtime:/etc/localtime:ro \
consul agent \
-server \
-bootstrap-expect=2 \
-node=node1 \
-rejoin \
-client 0.0.0.0 \
-advertise 1.1.1.3 \
-ui
2.在node2上执行,安装consul server,与上面的命令有区别,没有-ui
选项
docker run -d -p 8300:8300 -p 8301:8301/tcp -p 8302:8302/tcp -p 8301:8301/udp \
-p 8302:8302/udp -p 8500:8500 -p 8600:8600/udp -p 8600:8600/tcp \
--restart=always \
-h node2 \
--name consul \
--network microservice \
-v /etc/localtime:/etc/localtime:ro \
consul agent \
-server \
-node=node2 \
-rejoin \
-client 0.0.0.0 \
-join 1.1.1.3 \
-advertise 1.1.1.4
3.在node3上执行,安装consul client
docker run -d -p 8300:8300 -p 8301:8301/tcp -p 8302:8302/tcp -p 8301:8301/udp \
-p 8302:8302/udp -p 8500:8500 -p 8600:8600/udp -p 8600:8600/tcp \
--restart=always \
-h node3 \
--name consul \
--network microservice \
-v /etc/localtime:/etc/localtime:ro \
consul agent \
-node=node3 \
-rejoin \
-client 0.0.0.0 \
-join 1.1.1.3 \
-advertise 1.1.1.5
4.在node1上查看consul集群节点信息
# docker exec consul consul members
Node Address Status Type Build Protocol DC Segment
node1 1.1.1.3:8301 alive server 1.4.4 2 dc1 <all>
node2 1.1.1.4:8301 alive server 1.4.4 2 dc1 <all>
node3 1.1.1.5:8301 alive client 1.4.4 2 dc1 <default>
五.搭建服务注册,采用registrator实现
在node1,node2,node3按顺序执行以下命令
docker run -d \
--restart=always \
--name=registrator \
--net=host \
--volume=/var/run/docker.sock:/tmp/docker.sock \
-v /etc/localtime:/etc/localtime:ro \
doubleshit/registrator:v7 \
-resync 60 \
-cleanup \
-internal \
-ip 1.1.1.3 \
-useIpFromNetworkName=microservice \
consul://1.1.1.3:8500
可以通过访问:http://1.1.1.3:8500 入到consul的ui界面查看运行的服务或者通过curl http://1.1.1.3:8500/v1/catalog/services
在终端查看
查看详细的服务信息
# curl http://1.1.1.3:8500/v1/catalog/service/<服务名>
集群搭建完成