一、规划
1、swarm01作为manager节点,swarm02和swarm03作为worker节点。
$cat /etc/hosts
172.18.144.188 node1 node1
172.18.144.191 node2
172.18.144.190 node3
172.18.144.189 node4
2、配置SSH免密登陆
二、安装docker和ansible
1、安装配置ansible
$ yum -y install ansible
$ cat /etc/ansible/hosts | grep -v ^# | grep -v ^$
[node]
172.18.144.188
172.18.144.191
172.18.144.190
172.18.144.189
$ sed -i "s/SELINUX=enforcing/SELINUX=disabled" /etc/selinux/config
$ ansible node -m copy -a 'src=/etc/selinux/config dest=/etc/selinux/'
$ systemctl stop firewalld
$ systemctl disable firewalld
$ ansible node -a 'systemctl stop firewalld'
$ ansible node -a 'systemctl disable firewalld'</pre>
注:这里选择关闭防火墙,实际环境中可自行开放端口。
2、安装docker
- 在manager节点安装docker
$ yum install -y yum-utils device-mapper-persistent-data lvm2
$ yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
$ yum list docker-ce --showduplicates | sort -r
$ yum -y install docker-ce
$ docker --version
Docker version 18.05.0-ce, build f150324
$ systemctl start docker
$ systemctl status docker
$ systemctl enable docker</pre>
- 使用ansible在worker节点安装docker
$ ansible node -m copy -a 'src=/etc/yum.repos.d/docker-ce.repo dest=/etc/yum.repos.d/'
$ ansible node -m yum -a "state=present name=docker-ce"
$ ansible node -a 'docker --version'
172.18.144.191 | SUCCESS | rc=0 >>
Docker version 18.05.0-ce, build f150324
1172.18.144.190 | SUCCESS | rc=0 >>
Docker version 18.05.0-ce, build f150324
172.18.144.189 | SUCCESS | rc=0 >>
Docker version 18.05.0-ce, build f150324
$ ansible node -a 'systemctl start docker'
$ ansible node -a 'systemctl status docker'
$ ansible node -a 'systemctl enable docker'
三、配置docker swarm集群
1、创建docker swarm集群
$ docker swarm init --listen-addr 0.0.0.0
Swarm initialized: current node (a1tno675d14sm6bqlc512vf10) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3sp9uxzokgr252u1jauoowv74930s7f8f5tsmm5mlk5oim359e-dk52k5uul50w49gbq4j1y7zzb 172.18.144.188:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
2、查看节点
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
a1tno675d14sm6bqlc512vf10 * swarm01 Ready Active Leader</pre>
3、查看加入集群manager管理节点的命令
$ docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3sp9uxzokgr252u1jauoowv74930s7f8f5tsmm5mlk5oim359e-7tdlpdnkyfl1bnq34ftik9wxw 172.18.144.188:2377
4、查看加入集群worker节点的命令
$ docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3sp9uxzokgr252u1jauoowv74930s7f8f5tsmm5mlk5oim359e-dk52k5uul50w49gbq4j1y7zzb 172.18.144.188:2377
5、将前面规划的三个worker节点加入集群
$ docker swarm join --token SWMTKN-1-3sp9uxzokgr252u1jauoowv74930s7f8f5tsmm5mlk5oim359e-dk52k5uul50w49gbq4j1y7zzb 172.18.144.188:2377
This node joined a swarm as a worker.
6、查看worker节点是否已加入集群
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
mcc9hge72y8dhnxms6jwdqhrd * node1 Ready Active Leader 18.05.0-ce
rc8xoueu78vp2lmyv9afd5wkf node2 Ready Active 18.05.0-ce
lu8qsmf31ubk9s77bawmsxzw2 node3 Ready Active 18.05.0-ce
yl71cs8qs7s24yhvuwmdatfzp node4 Ready Active 18.05.0-ce
7、查看docker swarm的管理网络
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
91e24f0c4c6b bridge bridge local
6f6790a75aee docker_gwbridge bridge local
8add841bce4f host host local
jqzdbhspwicz ingress overlay swarm
6e8c1ff220be none null local
31e09aaeec48 redis-net bridge local
这里,一个docker swarm集群就搭建好了
四、搭建docker swarm的UI—Portainer
Portainer地址:https://portainer.io/
1、使用该命令部署Portainer
$ docker volume create portainer_data
$ docker service create \
--name portainer \
--publish 9000:9000 \
--replicas=1 \
--constraint 'node.role == manager' \
--mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock \
--mount type=volume,src=portainer_data,dst=/data \
portainer/portainer \
-H unix:///var/run/docker.sock
# docker images |grep portainer
portainer/portainer latest 07cde96d4789 2 weeks ago 10.4MB
# docker service ls ###查看集群列表
ID NAME MODE REPLICAS IMAGE PORTS
p5bo3n0fmqgz portainer replicated 1/1 portainer/portainer:latest *:9000->9000/tcp
这就部署好了
2、浏览器输入http://localhost:9000进入该UI界面,如下所示,第一次进入Portainer,配置8位数的admin密码
密码修改完成后点击“validate”验证
如下图所示,输入admin用户名和密码进入Portainer
首页如下
image.png查看swarm节点模块
image.png
网友评论