[root@manager1 ~]# docker swarm init --advertise-addr 192.168.237.128
Swarm initialized: current node (qfge07btc9daqarfrwm6q0hy7) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-2u8nmafs0zun1w6fxfpo06s1lpj53nzo5ugnqi0k7l00makawo-axiiswn6c1iciieul9eysw08v \
192.168.237.128:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
Swarm: active
NodeID: qfge07btc9daqarfrwm6q0hy7
Is Manager: true
ClusterID: 914p9w26eztw122j30cetlvsx
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.237.128
Manager Addresses:
192.168.237.128:2377
运行docker node ls可以查看节点信息
[root@manager1 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
qfge07btc9daqarfrwm6q0hy7 * manager1 Ready Active Leader
在*旁边的节点ID表明当前连接此节点上。
在worker1和worker2上加入
[root@worker2 ~]# docker swarm join \
> --token SWMTKN-1-2u8nmafs0zun1w6fxfpo06s1lpj53nzo5ugnqi0k7l00makawo-axiiswn6c1iciieul9eysw08v \
> 192.168.237.128:2377
This node joined a swarm as a worker.
[root@worker1 ~]# docker swarm join \
> --token SWMTKN-1-2u8nmafs0zun1w6fxfpo06s1lpj53nzo5ugnqi0k7l00makawo-axiiswn6c1iciieul9eysw08v \
> 192.168.237.128:2377
This node joined a swarm as a worker.
现在在manager1上查看信息
[root@manager1 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
164tibjpsoq80to1ktc4rp5ej worker2 Ready Active
qfge07btc9daqarfrwm6q0hy7 * manager1 Ready Active Leader
sr304jpfeprorr77olhqgqkkp worker1 Ready Active
在工作节点上没有查看节点信息的权限
[root@worker1 ~]# docker node ls
Error response from daemon: This node is not a swarm manager. Worker nodes can't be used to view or modify cluster state. Please run this command on a manager node or promote the current node to a manager.
在manager1节点上创建服务docker service create --replicas 1 --name helloworld alpine ping docker.com
[root@manager1 ~]# docker service create --replicas 1 --name helloworld alpine ping docker.com
1d5qs34fqw885n4uy47xo93x0
Since --detach=false was not specified, tasks will be created in the background.
In a future release, --detach=false will become the default.
[root@worker1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7acea91a6a8a alpine:latest "ping docker.com" 15 minutes ago Up 15 minutes helloworld.1.c3tin0odi0sivv7rf1kzixbh8
提示:如果helloworld正在管理器节点以外的节点上运行,则必须ssh到该节点。
运行以下命令以更改在swarm中运行的服务的所需状态:docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>
[root@manager1 ~]# docker service scale helloworld=5
helloworld scaled to 5
运行docker service ps <SERVICE-ID>以查看更新的任务列表:
[root@manager1 ~]# docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
c3tin0odi0si helloworld.1 alpine:latest worker1 Running Running 18 minutes ago
mwxqhw1n6na4 \_ helloworld.1 alpine:latest manager1 Shutdown Failed 18 minutes ago "task: non-zero exit (1)"
j2stxm2vvaz2 \_ helloworld.1 alpine:latest manager1 Shutdown Failed 19 minutes ago "task: non-zero exit (1)"
yolm4hgnoand \_ helloworld.1 alpine:latest manager1 Shutdown Failed 19 minutes ago "task: non-zero exit (1)"
tado3i2ti0mz \_ helloworld.1 alpine:latest manager1 Shutdown Failed 19 minutes ago "task: non-zero exit (1)"
baj5429fptoq helloworld.2 alpine:latest worker1 Running Running 20 seconds ago
9v1uzjbqu5k2 \_ helloworld.2 alpine:latest manager1 Shutdown Failed 26 seconds ago "task: non-zero exit (1)"
iu6xmkojng6w \_ helloworld.2 alpine:latest worker2 Shutdown Failed 36 seconds ago "task: non-zero exit (1)"
upo12ke12z58 \_ helloworld.2 alpine:latest worker2 Shutdown Failed 47 seconds ago "task: non-zero exit (1)"
80un6zvuw7q9 helloworld.3 alpine:latest worker1 Running Running 20 seconds ago
sn5gx3opagqr \_ helloworld.3 alpine:latest worker2 Shutdown Failed 25 seconds ago "task: non-zero exit (1)"
0iv09zlttz5c \_ helloworld.3 alpine:latest worker2 Shutdown Failed 36 seconds ago "task: non-zero exit (1)"
s1cidjv5ycjl \_ helloworld.3 alpine:latest worker2 Shutdown Failed 47 seconds ago "task: non-zero exit (1)"
pxv0vcr0yicb helloworld.4 alpine:latest worker1 Running Running about a minute ago
y6lafc2d8lid helloworld.5 alpine:latest worker1 Running Running 11 seconds ago
nplgwq5cyu40 \_ helloworld.5 alpine:latest worker2 Shutdown Failed 16 seconds ago "task: non-zero exit (1)"
9lua203a11se \_ helloworld.5 alpine:latest manager1 Shutdown Failed 26 seconds ago "task: non-zero exit (1)"
330jk7ut93ze \_ helloworld.5 alpine:latest manager1 Shutdown Failed 37 seconds ago "task: non-zero exit (1)"
igptfi644btq \_ helloworld.5 alpine:latest manager1 Shutdown Failed 47 seconds ago "task: non-zero exit (1)"
[root@manager1 ~]# docker service create \
> --replicas 3 \
> --name redis \
> --update-delay 10s \
> redis:3.0.6
36yu81hd03hl5amuy3wi8neax
Since --detach=false was not specified, tasks will be created in the background.
In a future release, --detach=false will become the default.
[root@manager1 ~]# docker service inspect --pretty redis
ID: 36yu81hd03hl5amuy3wi8neax
Name: redis
Service Mode: Replicated
Replicas: 3
Placement:
UpdateConfig:
Parallelism: 1
Delay: 10s
On failure: pause
Monitoring Period: 5s
Max failure ratio: 0
Update order: stop-first
RollbackConfig:
Parallelism: 1
On failure: pause
Monitoring Period: 5s
Max failure ratio: 0
Rollback order: stop-first
ContainerSpec:
Image: redis:3.0.6@sha256:6a692a76c2081888b589e26e6ec835743119fe453d67ecf03df7de5b73d69842
Resources:
Endpoint Mode: vip
现在可以更新容器镜像redis。swarm管理器根据UpdateConfig策略将更新应用于节点:
[root@manager1 ~]# docker service update --image redis:3.0.7 redis
redis
Since --detach=false was not specified, tasks will be updated in the background.
In a future release, --detach=false will become the default.
调度程序默认情况下应用滚动更新,如下所示:
停止第一个任务。
安排已停止任务的更新。
启动容器以获取更新的任务。
如果对任务的更新返回RUNNING,请等待指定的延迟时间,然后开始下一个任务。
如果在更新期间的任何时间返回任务FAILED,请暂停更新。
运行docker service inspect --pretty redis以查看所需状态的新图像:
[root@manager1 ~]# docker service inspect --pretty redis
ID: 36yu81hd03hl5amuy3wi8neax
Name: redis
Service Mode: Replicated
Replicas: 3
UpdateStatus:
State: updating
Started: About a minute
Message: update in progress
Placement:
UpdateConfig:
Parallelism: 1
Delay: 10s
On failure: pause
Monitoring Period: 5s
Max failure ratio: 0
Update order: stop-first
RollbackConfig:
Parallelism: 1
On failure: pause
Monitoring Period: 5s
Max failure ratio: 0
Rollback order: stop-first
ContainerSpec:
Image: redis:3.0.7@sha256:730b765df9fe96af414da64a2b67f3a5f70b8fd13a31e5096fee4807ed802e20
Resources:
Endpoint Mode: vip
重新启动暂停的更新运行docker service update <SERVICE-ID>。例如:
[root@manager1 ~]# docker service update redis
redis
Since --detach=false was not specified, tasks will be updated in the background.
In a future release, --detach=false will become the default.
为避免重复某些更新失败,您可能需要通过传递标志来重新配置服务docker service update。
运行docker service ps <SERVICE-ID>以查看滚动更新:
Since --detach=false was not specified, tasks will be updated in the background.
In a future release, --detach=false will become the default.
[root@manager1 ~]# docker service ps redis
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
tb3hhkawq941 redis.1 redis:3.0.6 manager1 Running Preparing 9 minutes ago
uglfpty2fwvq redis.2 redis:3.0.7 worker1 Running Preparing 3 minutes ago
jfzv1prt72j7 \_ redis.2 redis:3.0.6 worker1 Shutdown Shutdown 3 minutes ago
mi95dmqjrviz redis.3 redis:3.0.6 worker2 Running Preparing 9 minutes ago
[root@manager1 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
164tibjpsoq80to1ktc4rp5ej worker2 Ready Active
qfge07btc9daqarfrwm6q0hy7 * manager1 Ready Active Leader
sr304jpfeprorr77olhqgqkkp worker1 Ready Active
[root@manager1 ~]# docker service create \
> --name my-web \
> --publish published=8080,target=80 \
> --replicas 2 \
> nginx
ir1aln8w0387t5uq9408grc2r
Since --detach=false was not specified, tasks will be created in the background.
In a future release, --detach=false will become the default.
global
log /dev/log local0
log /dev/log local1 notice
...snip...
# Configure HAProxy to listen on port 80
frontend http_front
bind *:80
stats uri /haproxy?stats
default_backend http_back
# Configure HAProxy to route requests to swarm nodes on port 8080
backend http_back
balance roundrobin
server node1 192.168.99.100:8080 check
server node2 192.168.99.101:8080 check
server node3 192.168.99.102:8080 check