create a swarm
- 创建一个新的集群
docker swarm init --advertise-addr <MANAGER-IP>
- Run docker info to view the current state of the swarm:
$ docker info
Containers: 2
Running: 0
Paused: 0
Stopped: 2
..snip...
Swarm: active
NodeID: dxn1zf6l61qsb1josjja83ngz
Is Manager: true
Managers: 1
Nodes: 1
...snip...
[root@centos5 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
ip2fs5kkfpllyexal8pxuxnlq centos4 Ready Active
t7998hft3zw479evc7lr8irx2 * centos5 Ready Active Leade
- run the following command on a manager node to retrieve the join command for a worker
docker swarm join-token worker
[root@centos5 ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1705vcetk9pgd3nb9zph2izwtcudq1ajeh7b13wqcst46epk18-bltg74jq3ql7zrswirt6xhwm3 10.173.38.40:2377
- use command to create a severvice:
docker service create --replicas 1 --name helloworld alpine ping docker.com
- Run docker service inspect --pretty
to display the details about a service in an easily readable format.
[root@centos5 ~]# docker service inspect --pretty helloworld
ID: vk639rdtes0pw8anp7dxnznzu
Name: helloworld
Service Mode: Replicated
Replicas: 1
Placement:
UpdateConfig:
Parallelism: 1
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: alpine:latest@sha256:1072e499f3f655a032e88542330cf75b02e7bdf673278f701d7ba61629ee3ebe
Args: ping docker.com
Resources:
Endpoint Mode: vip
- Run docker service ps
to see which nodes are running the service:
[root@centos5 ~]# docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
zgeljyk5fqf6 helloworld.1 alpine:latest centos5 Running Running 8 minutes ago
*Run the following command to change the desired state of the service running in the swarm:
$ docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>
- Now you can update the container image for redis. The swarm manager applies the update to nodes according to the UpdateConfig policy:
docker service update --image redis:3.0.7 redis