Swarm是一套极为简单管理docker集群工具,使得docker集群暴露给用户的是一个虚拟整体。搭建环境:关闭防火墙和安全linux[root@localhost ~]# systemctl stop firewalld[root@localhost ~]# setenforce 0分别修改主机名:[root@localhost ~]# vi /etc/hostnameswarm01swarm02重启并分别添加一张桥接网卡联网[root@swarm01 ~]# systemctl restart network[root@swarm02 ~]# systemctl restart network[root@swarm01 ~]# vi /etc/yum.repos.d/CentOS-Base.repo //分别配置yum源[docker-repo]name=Docker Repositorybaseurl=https://yum.dockerproject.org/repo/main/centos/7/enabled=1gpgcheck=1gpgkey=https://yum.dockerproject.org/gpg[base]name=centos-$releasever - Basebaseurl=file:///mntenabled=1gpgcheck=0[root@swarm02 ~]# vi /etc/yum.repos.d/CentOS-Base.repo[docker-repo]name=Docker Repositorybaseurl=https://yum.dockerproject.org/repo/main/centos/7/enabled=1gpgcheck=1gpgkey=https://yum.dockerproject.org/gpg[base]name=centos-$releasever - Basebaseurl=file:///mntenabled=1gpgcheck=0 分别下载安装docker:yum install -y docker-enginesystemctl enable dockersystemctl start docker修改docker的监听端口[root@swarm01 ~]# vi /lib/systemd/system/docker.service 在ExecStart后加入:-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock[root@swarm01 ~]# systemctl daemon-reload //重新加载配置文件[root@swarm01 ~]# systemctl restart docker //重新启动docker[root@swarm01 ~]# docker pull swarm[root@swarm01 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEswarm latest ff454b4a0e84 7 months ago 12.7MB[root@swarm02 ~]# docker pull swarm[root@swarm02 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEswarm latest ff454b4a0e84 7 months ago 12.7MB //下载swarm成功初始化Swarm[root@swarm01 ~]# docker swarm init --advertise-addr 192.168.100.30 上面命令执行后,该机器自动加入到swarm集群。这个会创建一个集群token,获取全球唯一的 token,作为集群唯一标识。后续将其他节点加入集群都会用到这个token值。查看swarm节点[root@swarm02 ~]# docker swarm join --token SWMTKN-1-45wi0d159hormsyptmtnve18a1sas6zfo1alxzsk60ow5ii36t-bi167goeagg6tb0yf9dnzgmcv 192.168.100.30:2377Swarm02加入swarm群集之后有两个节点了,后面有几台服务器都可以用这个token值加入swarm群集创建集群网络overlay[root@swarm01 ~]# docker network create --driver overlay skynetdocker network ls 查看![root@swarm01 ~]# docker pull httpd
部署测试[root@swarm01 ~]# docker service create -p 80:80 --name webserver --replicas 4 httpd //如果要继续加服务如nginx,前面80端口要改,服务名也要改[root@swarm01 ~]# docker service ls //查看服务列表 [root@swarm01 ~]# docker service ps webserver | grep Running //查看具体服务开启情况访问:http://192.168.100.30/http://192.168.100.40/全程swarm02没有下载过httpd服务如果访问不了就重启docker ,systemctl restart docker