创建一个swarm
1、登录管理节点manager1
执行如下命令创建一个新的swarm:
$ docker swarm init --advertise-addr 192.168.99.100
Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
192.168.99.100:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
2、执行docker state
查看swarm的当前状态:
$ docker info
Containers: 2
Running: 0
Paused: 0
Stopped: 2
...snip...
Swarm: active
NodeID: dxn1zf6l61qsb1josjja83ngz
Is Manager: true
Managers: 1
Nodes: 1
...snip...
3、执行docker node ls
查看节点信息:
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
dxn1zf6l61qsb1josjja83ngz * manager1 Ready Active Leader
节点ID旁边的*表示当前连接在此节点上。
向swarm添加节点
1、登录想要运行工作节点的机器worker1
,执行如下命令:
$ docker swarm join \
--token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
192.168.99.100:2377
This node joined a swarm as a worker.
此命令出现在创建swarm时执行的docker swarm init...
的输出中,如果忘记此命令,可以在管理节点上执行一下命令:
$ docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
192.168.99.100:2377
2、依次添加worker2
主机,在管理节点查看添加的节点:
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
03g1y59jwfg7cf99w4lt0f662 worker2 Ready Active
9j68exjopxe7wfl6yuxml7a7j worker1 Ready Active
dxn1zf6l61qsb1josjja83ngz * manager1 Ready Active Leader
Swarm的管理类命令像docker node ls
只能在管理节点上执行。
向swarm部署一个服务
1、登录管理节点manager1
,执行以下命令:
$ docker service create --replicas 1 --name helloworld alpine ping docker.com
9uk4639qpg7npwf3fn2aasksr
docker service create
命令创建一个服务--name
标识服务名为helloworld
--replicas
指定运行服务的数量- 参数
alpine ping docker.com
将服务定义Alpine Linux
容器执行命令ping docker.com
2、运行docker service ls
查看运行的服务列表:
$ docker service ls
ID NAME SCALE IMAGE COMMAND
9uk4639qpg7n helloworld 1/1 alpine ping docker.com
检查swarm的服务
1、登录管理节点manager1
,执行docker service inspect --pretty <SERVICE-ID>
以简单易读的格式显示服务的详情:
$ docker service inspect --pretty helloworld
ID: 9uk4639qpg7npwf3fn2aasksr
Name: helloworld
Mode: REPLICATED
Replicas: 1
Placement:
UpdateConfig:
Parallelism: 1
ContainerSpec:
Image: alpine
Args: ping docker.com
执行不带--pretty
的命令返回json格式的服务的详情:
$ docker service inspect helloworld
[
{
"ID": "9uk4639qpg7npwf3fn2aasksr",
"Version": {
"Index": 418
},
"CreatedAt": "2016-06-16T21:57:11.622222327Z",
"UpdatedAt": "2016-06-16T21:57:11.622222327Z",
"Spec": {
"Name": "helloworld",
"TaskTemplate": {
"ContainerSpec": {
"Image": "alpine",
"Args": [
"ping",
"docker.com"
]
},
"Resources": {
"Limits": {},
"Reservations": {}
},
"RestartPolicy": {
"Condition": "any",
"MaxAttempts": 0
},
"Placement": {}
},
"Mode": {
"Replicated": {
"Replicas": 1
}
},
"UpdateConfig": {
"Parallelism": 1
},
"EndpointSpec": {
"Mode": "vip"
}
},
"Endpoint": {
"Spec": {}
}
}
]
2、执行docker service ps <SERVICE-ID>
查看哪个节点在运行服务:
$ docker service ps helloworld
ID NAME SERVICE IMAGE LAST STATE DESIRED STATE NODE
8p1vev3fq5zm0mi8g0as41w35 helloworld.1 helloworld alpine Running 3 minutes Running worker2
默认情况下,swarm中的节点可以像工作节点一样执行任务。
3、在运行任务的节点上执行docker ps
命令查看有关该任务的容器的详细信息:
$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e609dde94e47 alpine:latest "ping docker.com" 3 minutes ago Up 3 minutes helloworld.1.8p1vev3fq5zm0mi8g0as41w35
在swarm中缩放服务
1、登录管理节点,执行docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>
改变在swarm中期望服务的运行的状态:
$ docker service scale helloworld=5
helloworld scaled to 5
2、执行docker service ps <SERVICE-ID>
命令查看更新的任务列表:
$ docker service ps helloworld
ID NAME SERVICE IMAGE LAST STATE DESIRED STATE NODE
8p1vev3fq5zm0mi8g0as41w35 helloworld.1 helloworld alpine Running 7 minutes Running worker2
c7a7tcdq5s0uk3qr88mf8xco6 helloworld.2 helloworld alpine Running 24 seconds Running worker1
6crl09vdcalvtfehfh69ogfb1 helloworld.3 helloworld alpine Running 24 seconds Running worker1
auky6trawmdlcne8ad8phb0f1 helloworld.4 helloworld alpine Running 24 seconds Accepted manager1
ba19kca06l18zujfwxyc5lkyn helloworld.5 helloworld alpine Running 24 seconds Running worker2
删除在swarm中运行的服务
1、登录管理节点,执行以下命令删除服务:
$ docker service rm helloworld
helloworld
2、执行docker service inpect <SERVICE-ID>
命令验证swarm管理员删除了服务。终端返回服务没有找到的消息:
$ docker service inspect helloworld
[]
Error: no such service: helloworld
将滚动更新应用于服务
1、在swarm中部署Redis 3.0.6,并配置swarm延迟10秒更新(即UpdateConfig):
$ docker service create \
--replicas 3 \
--name redis \
--update-delay 10s \
redis:3.0.6
0u6a4s31ybk7yw2wyvtikmu50
- –update-delay 更新一个服务任务或者任务集合的延迟时间,
10m30s
意味着十分钟三十秒的延迟 - 默认调度器一次更新一个任务,可以通过
--update-parallelism
标识配置并行更新的最大数量 - 默人,当对单个任务的更新返回
RUNNING
状态时,调度程序会调度另外一个任务进行更行,直到所有任务都更新。如果在更新期间的任何事件返回FAILED,则调度程序停止更新。可以通过使用--update-failure-action
标识控制docker创建服务或者docker更新服务的行为。
2、检测redis
服务:
$ docker service inspect --pretty redis
ID: 0u6a4s31ybk7yw2wyvtikmu50
Name: redis
Mode: Replicated
Replicas: 3
Placement:
Strategy: Spread
UpdateConfig:
Parallelism: 1
Delay: 10s
ContainerSpec:
Image: redis:3.0.6
Resources:
3、更新redis
的容器镜像,swarm管理器会通过UpdateConfig
策略实施更新:
$ docker service update --image redis:3.0.7 redis
redis
调度程序默认按照如下过程应用滚动更新:
- 停止第一个任务
- 为停止的任务调度更新
- 为更新后的任务启动容器
- 如果任务的更新返回RUNNING,等待指定的延迟时间间隔,然后停止下一个任务
- 如果在更新过程中的任何时间,一个任务返回FAILED,暂停更新
4、运行docker service inspect --pretty redis
查看期望状态下的新镜像:
$ docker service inspect --pretty redis
ID: 0u6a4s31ybk7yw2wyvtikmu50
Name: redis
Mode: Replicated
Replicas: 3
Placement:
Strategy: Spread
UpdateConfig:
Parallelism: 1
Delay: 10s
ContainerSpec:
Image: redis:3.0.7
Resources:
如果因为失败导致更新暂停,service inspect
显示如下输出:
$ docker service inspect --pretty redis
ID: 0u6a4s31ybk7yw2wyvtikmu50
Name: redis
...snip...
Update status:
State: paused
Started: 11 seconds ago
Message: update paused due to failure or early termination of task 9p7ith557h8ndf0ui9s0q951b
...snip...
要重启暂停的更新,运行docker service update <SERVICE-ID>
,例如:
docker service update redis
为了避免重复某些更新失败,可能需要向docker service update
传递标识重新配置服务
6、运行docker service ps <SERVICE-ID>
查看滚动更新:
$ docker service ps redis
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
dos1zffgeofhagnve8w864fco redis.1 redis:3.0.7 worker1 Running Running 37 seconds
88rdo6pa52ki8oqx6dogf04fh \_ redis.1 redis:3.0.6 worker2 Shutdown Shutdown 56 seconds ago
9l3i4j85517skba5o7tn5m8g0 redis.2 redis:3.0.7 worker2 Running Running About a minute
66k185wilg8ele7ntu8f6nj6i \_ redis.2 redis:3.0.6 worker1 Shutdown Shutdown 2 minutes ago
egiuiqpzrdbxks3wxgn8qib1g redis.3 redis:3.0.7 worker1 Running Running 48 seconds
ctzktfddb2tepkr45qcmqln04 \_ redis.3 redis:3.0.6 mmanager1 Shutdown Shutdown 2 minutes ago
在swarm上架空(DRAIN)一个节点
将节点设置为DRAIN
可以防止节点从swarm管理器接收新的任务,也意味着管理器停止运行在这个节点的任务,并在一个ACTIVE
的节点上复制一个任务。
1、登录管理服务器manager1
,查看所有的节点为激活的状态:
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
1bcef6utixb0l0ca7gxuivsj0 worker2 Ready Active
38ciaotwjuritcdtn9npbnkuz worker1 Ready Active
e216jshn25ckzbvmwlnh5jr3g * manager1 Ready Active Leader
2、运行docker node update --availability drain <NODE-ID>
以排空已分配任务节点的任务:
docker node update --availability drain worker1
worker1
3、检查节点的可用性:
$ docker node inspect --pretty worker1
ID: 38ciaotwjuritcdtn9npbnkuz
Hostname: worker1
Status:
State: Ready
Availability: Drain
...snip...
4、运行docker service ps redis
查看swarm管理器如何更新redis服务的任务分配:
$ docker service ps redis
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
7q92v0nr1hcgts2amcjyqg3pq redis.1 redis:3.0.6 manager1 Running Running 4 minutes
b4hovzed7id8irg1to42egue8 redis.2 redis:3.0.6 worker2 Running Running About a minute
7h2l8h3q3wqy5f66hlv9ddmi6 \_ redis.2 redis:3.0.6 worker1 Shutdown Shutdown 2 minutes ago
9bg7cezvedmkgg6c8yzvbhwsd redis.3 redis:3.0.6 worker2 Running Running 4 minutes
5、运行docker node update --availability active <NODE-ID>
将架空的节点返回到激活状态:
$ docker node update --availability active worker1
worker1
6、检查节点,查看更新后的状态:
$ docker node inspect --pretty worker1
ID: 38ciaotwjuritcdtn9npbnkuz
Hostname: worker1
Status:
State: Ready
Availability: Active
...snip...
当节点回到激活状态,它可以接收新的任务:
- 在一个服务扩展期间
- 滚动更新期间
- 将另外一个节点设置成
Drain
可用性 - 当一个任务在另外的节点失败