配置和体验Docker Swarm

2017-02-25 阅读: Docker Docker Swarm

本当记录按照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命令初始化集群:

docker swarm init --advertise-addr 192.168.61.10

Swarm initialized: current node (jqcx4dbr1y00espou3mtmp6m3) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join \
    --token SWMTKN-1-55deh9541886sn6qsnwa2xq62f0s8k5urdbje8sqerhmb0b33e-8cgailtk9yzt7am7gfjh64foc \
    192.168.61.10:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

当前节点被切换成swarm模式,同时作为集群的manager节点。集群中的其他节点通过advertise-addr访问manager节点。使用docker info查看swarm状态:

docker info

Containers: 1
 Running: 0
 Paused: 0
 Stopped: 1
Images: 1
Server Version: 1.13.1
Storage Driver: overlay
 Backing Filesystem: xfs
 Supports d_type: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: active
 NodeID: jqcx4dbr1y00espou3mtmp6m3
 Is Manager: true
 ClusterID: xk0l7eskn1116n9xv4h0romgp
 Managers: 1
 Nodes: 1
 Orchestration:
  Task History Retention Limit: 5
 Raft:
  Snapshot Interval: 10000
  Number of Old Snapshots to Retain: 0
  Heartbeat Tick: 1
  Election Tick: 3
 Dispatcher:
  Heartbeat Period: 5 seconds
 CA Configuration:
  Expiry Duration: 3 months
 Node Address: 192.168.61.10
 Manager Addresses:
  192.168.61.10:2377
Runtimes: runc
.......

使用docker node ls查看swarm的节点信息:

docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
jqcx4dbr1y00espou3mtmp6m3 *  manager1  Ready   Active        Leader

需要重新初始化集群时,可以在manager1节点执行docker swarm leave --force

添加节点(worker node)

下面将worker1和worker2添加到swarm中,分别在worker1和worker2上执行:

docker swarm join \
    --token SWMTKN-1-55deh9541886sn6qsnwa2xq62f0s8k5urdbje8sqerhmb0b33e-8cgailtk9yzt7am7gfjh64foc \
    192.168.61.10:2377

This node joined a swarm as a worker.

在manager1上查看swarm的节点信息:

docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
40n3cemzy0e61tg0xngbnmavw    workder2  Ready   Active
jqcx4dbr1y00espou3mtmp6m3 *  manager1  Ready   Active        Leader
zicuzcgvcaylyozivhap3h10e    worker1   Ready   Active

部署service

创建服务

在manager1上执行:

docker service create --replicas 1 --name helloworld alpine ping docker.com
docker service ls
ID            NAME        MODE        REPLICAS  IMAGE
d15yu9fp6uan  helloworld  replicated  1/1       alpine:latest


docker service inspect --pretty helloworld

ID:             d15yu9fp6uangvc42e3cvfz90
Name:           helloworld
Service Mode:   Replicated
 Replicas:      1
Placement:
UpdateConfig:
 Parallelism:   1
 On failure:    pause
 Max failure ratio: 0
ContainerSpec:
 Image:         alpine:latest@sha256:dfbd4a3a8ebca874ebd2474f044a0b33600d4523d03b0df76e5c5986cb02d7e8
 Args:          ping docker.com
Resources:
Endpoint Mode:  vip
docker service ps helloworld
ID            NAME          IMAGE          NODE      DESIRED STATE  CURRENT STATE          ERROR  PORTS
v7rz1q3jbd60  helloworld.1  alpine:latest  manager1  Running        Running 2 minutes ago

服务扩容

docker service scale helloworld=5

helloworld scaled to 5


docker service ps helloworld
ID            NAME          IMAGE          NODE      DESIRED STATE  CURRENT STATE             ERROR  PORTS
v7rz1q3jbd60  helloworld.1  alpine:latest  manager1  Running        Running 3 minutes ago
t0bueqxz59gh  helloworld.2  alpine:latest  workder2  Running        Preparing 13 seconds ago
g9e0cxaus7ue  helloworld.3  alpine:latest  workder2  Running        Preparing 13 seconds ago
qr3r6jxuib2v  helloworld.4  alpine:latest  manager1  Running        Running 12 seconds ago
8y1rmhbnsaeu  helloworld.5  alpine:latest  worker1   Running        Running 1 second ago

删除服务

docker service rm helloworld

滚动升级

docker service create \
  --replicas 3 \
  --name redis \
  --update-delay 10s \
  redis:3.0.6

docker service update --image redis:3.0.7 redis

docker service ps redis

drain node

 docker service ps helloworld
ID            NAME          IMAGE          NODE      DESIRED STATE  CURRENT STATE           ERROR  PORTS
jkadvsdxt0u4  helloworld.1  alpine:latest  worker1   Running        Running 27 seconds ago
tq1d6puxllim  helloworld.2  alpine:latest  worker1   Running        Running 18 seconds ago
qokx1nezzs9w  helloworld.3  alpine:latest  workder2  Running        Running 18 seconds ago
p6aeuvxa62y1  helloworld.4  alpine:latest  manager1  Running        Running 18 seconds ago
l7o1jutyitjj  helloworld.5  alpine:latest  manager1  Running        Running 18 seconds ago
docker node update --availability drain worker1

docker node inspect --pretty worker1
ID:                     zicuzcgvcaylyozivhap3h10e
Hostname:               worker1
Joined at:              2017-02-25 11:20:45.483139499 +0000 utc
Status:
 State:                 Ready
 Availability:          Drain
 Address:               192.168.61.11


docker service ps helloworld
ID            NAME              IMAGE          NODE      DESIRED STATE  CURRENT STATE            ERROR  PORTS
ibwak6db70vj  helloworld.1      alpine:latest  workder2  Running        Running 16 seconds ago
jkadvsdxt0u4   \_ helloworld.1  alpine:latest  worker1   Shutdown       Shutdown 17 seconds ago
aslp4gfmwpx3  helloworld.2      alpine:latest  workder2  Running        Running 16 seconds ago
tq1d6puxllim   \_ helloworld.2  alpine:latest  worker1   Shutdown       Shutdown 17 seconds ago
qokx1nezzs9w  helloworld.3      alpine:latest  workder2  Running        Running 2 minutes ago
p6aeuvxa62y1  helloworld.4      alpine:latest  manager1  Running        Running 2 minutes ago
l7o1jutyitjj  helloworld.5      alpine:latest  manager1  Running        Running 2 minutes ago

docker node update --availability active worker1

docker node inspect --pretty worker1
ID:                     zicuzcgvcaylyozivhap3h10e
Hostname:               worker1
Joined at:              2017-02-25 11:20:45.483139499 +0000 utc
Status:
 State:                 Ready
 Availability:          Active
 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

参考

标题:配置和体验Docker Swarm
本文链接:https://blog.frognew.com/2017/02/docker-swarm-mode-setup.html
转载请注明出处。

目录