docker swarm
系统:CentOS 7
docker版本:docker-ce-18.03.1
2台主机
主机名 | ip |
---|---|
manager | 192.168.10.131 |
woker | 192.168.10.130 |
环境搭建
配置安装yum管理工具,添加dokcer阿里源
配置阿里的docker yum源,快速下载docker-ce
yum –y install yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装docker-ce
[root@manager ~]# yum list docker-ce --showduplicates | sort -r
* updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
Installed Packages
* extras: mirrors.aliyun.com
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
# 我这里选择docker-ce-18.03.1.ce版本
yum -y install docker-ce-18.03.1.ce
配置docker
vim /lib/systemd/system/docker.service
在ExecStart后添加 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
–bip是docker默认初始化网络网段,可不加。
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --bip=10.18.18.1/24
禁用selinux
vim /etc/selinux/config
SELINUX=disable
reboot
重启后生效
上面2个主机都这样配置
docker swarm
启动docker服务
systemctl start docker
初始化管理节点
在manager主机
[root@manager ~]# docker swarm init --advertise-addr 192.168.10.131
Swarm initialized: current node (7jfig6733t8gf6afzrzr5ro2g) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0jdml8q0tnvxn6jet7qgyj88xn5dls0sp9q1xwf8egcx5pxss4-bm0cf6fytdolbvcidod73ispw 192.168.10.131:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
其它节点加入docker swarm集群
在woker主机
有2个关键信息,令牌与管理节点通信地址
[root@worker ~]# docker swarm join --token SWMTKN-1-0jdml8q0tnvxn6jet7qgyj88xn5dls0sp9q1xwf8egcx5pxss4-bm0cf6fytdolbvcidod73ispw 192.168.10.131:2377
This node joined a swarm as a worker.
查看节点列表
在管理节点上
[root@manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
7jfig6733t8gf6afzrzr5ro2g * manager Ready Active Leader 18.03.1-ce
3q7tggdv8kd4czlywgshz88q1 worker Ready Active 18.03.1-ce
创建docker service
[root@manager ~]# docker pull nginx
[root@manager ~]# docker service create --replicas 1 --name nginx -p 8080:80 nginx
58onndkgy9c18oxxlovyz7aj0
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
可以从swarm集群任意1节点访问到nginx
https://docs.docker.com/engine/swarm/images/ingress-routing-mesh.png