配置和体验Docker Swarm
2017-02-25
本当记录按照Docker官方swarm-tutoria文档,配置和体验Docker Swarm Mode的过程。
准备 #
主机:
- manager1 - 192.168.61.10, CentOS7
- worker1 - 192.168.61.11, CentOS7
- worker2 - 192.168.61.12, CentOS7
Docker Engine > 1.12
开放端口2377 tcp端口, 7946 4789 tcp udp 端口
初始化集群(manager node) #
在manager1上执行docker swarm init --advertise-addr 192.168.61.10
命令初始化集群:
1docker swarm init --advertise-addr 192.168.61.10
2
3Swarm initialized: current node (jqcx4dbr1y00espou3mtmp6m3) is now a manager.
4
5To add a worker to this swarm, run the following command:
6
7 docker swarm join \
8 --token SWMTKN-1-55deh9541886sn6qsnwa2xq62f0s8k5urdbje8sqerhmb0b33e-8cgailtk9yzt7am7gfjh64foc \
9 192.168.61.10:2377
10
11To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
当前节点被切换成swarm模式,同时作为集群的manager节点。集群中的其他节点通过advertise-addr
访问manager节点。使用docker info
查看swarm状态:
1docker info
2
3Containers: 1
4 Running: 0
5 Paused: 0
6 Stopped: 1
7Images: 1
8Server Version: 1.13.1
9Storage Driver: overlay
10 Backing Filesystem: xfs
11 Supports d_type: false
12Logging Driver: json-file
13Cgroup Driver: cgroupfs
14Plugins:
15 Volume: local
16 Network: bridge host macvlan null overlay
17Swarm: active
18 NodeID: jqcx4dbr1y00espou3mtmp6m3
19 Is Manager: true
20 ClusterID: xk0l7eskn1116n9xv4h0romgp
21 Managers: 1
22 Nodes: 1
23 Orchestration:
24 Task History Retention Limit: 5
25 Raft:
26 Snapshot Interval: 10000
27 Number of Old Snapshots to Retain: 0
28 Heartbeat Tick: 1
29 Election Tick: 3
30 Dispatcher:
31 Heartbeat Period: 5 seconds
32 CA Configuration:
33 Expiry Duration: 3 months
34 Node Address: 192.168.61.10
35 Manager Addresses:
36 192.168.61.10:2377
37Runtimes: runc
38.......
使用docker node ls
查看swarm的节点信息:
1docker node ls
2ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
3jqcx4dbr1y00espou3mtmp6m3 * manager1 Ready Active Leader
需要重新初始化集群时,可以在manager1节点执行
docker swarm leave --force
添加节点(worker node) #
下面将worker1和worker2添加到swarm中,分别在worker1和worker2上执行:
1docker swarm join \
2 --token SWMTKN-1-55deh9541886sn6qsnwa2xq62f0s8k5urdbje8sqerhmb0b33e-8cgailtk9yzt7am7gfjh64foc \
3 192.168.61.10:2377
4
5This node joined a swarm as a worker.
在manager1上查看swarm的节点信息:
1docker node ls
2ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
340n3cemzy0e61tg0xngbnmavw workder2 Ready Active
4jqcx4dbr1y00espou3mtmp6m3 * manager1 Ready Active Leader
5zicuzcgvcaylyozivhap3h10e worker1 Ready Active
部署service #
创建服务 #
在manager1上执行:
1docker service create --replicas 1 --name helloworld alpine ping docker.com
1docker service ls
2ID NAME MODE REPLICAS IMAGE
3d15yu9fp6uan helloworld replicated 1/1 alpine:latest
4
5
6docker service inspect --pretty helloworld
7
8ID: d15yu9fp6uangvc42e3cvfz90
9Name: helloworld
10Service Mode: Replicated
11 Replicas: 1
12Placement:
13UpdateConfig:
14 Parallelism: 1
15 On failure: pause
16 Max failure ratio: 0
17ContainerSpec:
18 Image: alpine:latest@sha256:dfbd4a3a8ebca874ebd2474f044a0b33600d4523d03b0df76e5c5986cb02d7e8
19 Args: ping docker.com
20Resources:
21Endpoint Mode: vip
1docker service ps helloworld
2ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
3v7rz1q3jbd60 helloworld.1 alpine:latest manager1 Running Running 2 minutes ago
服务扩容 #
1docker service scale helloworld=5
2
3helloworld scaled to 5
4
5
6docker service ps helloworld
7ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
8v7rz1q3jbd60 helloworld.1 alpine:latest manager1 Running Running 3 minutes ago
9t0bueqxz59gh helloworld.2 alpine:latest workder2 Running Preparing 13 seconds ago
10g9e0cxaus7ue helloworld.3 alpine:latest workder2 Running Preparing 13 seconds ago
11qr3r6jxuib2v helloworld.4 alpine:latest manager1 Running Running 12 seconds ago
128y1rmhbnsaeu helloworld.5 alpine:latest worker1 Running Running 1 second ago
删除服务 #
1docker service rm helloworld
滚动升级 #
1docker service create \
2 --replicas 3 \
3 --name redis \
4 --update-delay 10s \
5 redis:3.0.6
6
7docker service update --image redis:3.0.7 redis
8
9docker service ps redis
drain node #
1 docker service ps helloworld
2ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
3jkadvsdxt0u4 helloworld.1 alpine:latest worker1 Running Running 27 seconds ago
4tq1d6puxllim helloworld.2 alpine:latest worker1 Running Running 18 seconds ago
5qokx1nezzs9w helloworld.3 alpine:latest workder2 Running Running 18 seconds ago
6p6aeuvxa62y1 helloworld.4 alpine:latest manager1 Running Running 18 seconds ago
7l7o1jutyitjj helloworld.5 alpine:latest manager1 Running Running 18 seconds ago
1docker node update --availability drain worker1
2
3docker node inspect --pretty worker1
4ID: zicuzcgvcaylyozivhap3h10e
5Hostname: worker1
6Joined at: 2017-02-25 11:20:45.483139499 +0000 utc
7Status:
8 State: Ready
9 Availability: Drain
10 Address: 192.168.61.11
11
12
13docker service ps helloworld
14ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
15ibwak6db70vj helloworld.1 alpine:latest workder2 Running Running 16 seconds ago
16jkadvsdxt0u4 \_ helloworld.1 alpine:latest worker1 Shutdown Shutdown 17 seconds ago
17aslp4gfmwpx3 helloworld.2 alpine:latest workder2 Running Running 16 seconds ago
18tq1d6puxllim \_ helloworld.2 alpine:latest worker1 Shutdown Shutdown 17 seconds ago
19qokx1nezzs9w helloworld.3 alpine:latest workder2 Running Running 2 minutes ago
20p6aeuvxa62y1 helloworld.4 alpine:latest manager1 Running Running 2 minutes ago
21l7o1jutyitjj helloworld.5 alpine:latest manager1 Running Running 2 minutes ago
22
23docker node update --availability active worker1
24
25docker node inspect --pretty worker1
26ID: zicuzcgvcaylyozivhap3h10e
27Hostname: worker1
28Joined at: 2017-02-25 11:20:45.483139499 +0000 utc
29Status:
30 State: Ready
31 Availability: Active
32 Address: 192.168.61.11
docker swam命令 #
- docker swarm - 集群管理, 子命令有init, join,join-token, leave, update
- docker node - 节点管理, 子命令有demote, inspect,ls, promote, rm, ps, update
- docker service - 服务管理, 子命令有create, inspect, ps, ls ,rm , scale, update
- docker stack - 用于应用部署,子命令有deploy, ls, ps, rm, services