Docker Swarm 安装使用教程


1. 初始化管理节点

#这里的IP为机器的ip, 此主机为master
docker swarm init --advertise-addr 192.168.88.129

一定要复制下来生成的token:
​​​docker swarm join --token SWMTKN-1-1t1rwgqk1fe92tk5jtboturo4otziqbxgxjy5pd8410g8yd9f3-9qniaz6f9ruvty6i5sdqeupcq 192.168.88.129:2377​

Docker Swarm 安装使用教程_linux

2. 创建工作节点

请参考Docker Machine安装及使用
安装完成后:

# 1.进入机器
docker-machine ssh m1

# 2. 添加token 第一步生成的token
docker swarm join --token SWMTKN-1-1t1rwgqk1fe92tk5jtboturo4otziqbxgxjy5pd8410g8yd9f3-9qniaz6f9ruvty6i5sdqeupcq 192.168.88.129:2377

如果报错:
Error response from daemon: rpc error: code = Unavailable desc = all SubConns are in TransientFailure, latest connection error: connection error: desc = "transport: Error while dialing dial tcp 192.168.88.129:2377: ​​​connect: no route to host​

Docker Swarm 安装使用教程_redis_02

# 退出远程机器
exit;

# 此时退出远程机器, 在master机器执行如下
# 1:查看防火状态
systemctl status firewalld

# 2:暂时关闭防火墙
systemctl stop firewalld

# 3:永久关闭防火墙
systemctl disable firewalld

# 4:重启防火墙
systemctl enable firewalld

Docker Swarm 安装使用教程_linux_03
然后重新加入, 如果有多个结点.
进入对应的结点然后加入即可:

docker-machine ssh m1
docker swarm join --token SWMTKN-1-1t1rwgqk1fe92tk5jtboturo4otziqbxgxjy5pd8410g8yd9f3-9qniaz6f9ruvty6i5sdqeupcq 192.168.88.129:2377

Docker Swarm 安装使用教程_docker_04

3. 查看集群信息

需要退出远程机器, 在master主机上查看

docker info

Docker Swarm 安装使用教程_docker_05

4.使用

4.1 部署服务到集群中

跟集群管理有关的任何操作,都是在​​管理节点​​ master主机上操作的。

# 查看所有结点
docker node ls

# 随机在一个工作节点上创建一个名为 helloworld 的服务
docker service create --replicas 1 --name helloworld alpine ping docker.com

# 查看服务部署情况
docker service ps helloworld

# 查看具体信息
docker service inspect --pretty helloworld

# 扩展集群服务 扩展到俩个节点
docker service scale helloworld=2

# 删除
docker service rm helloworld

# 滚动升级
docker service update --image redis:3.0.7 redis

Docker Swarm 安装使用教程_linux_06
Docker Swarm 安装使用教程_linux_07