Skip to content

Instantly share code, notes, and snippets.

@xwlee
Last active May 21, 2017 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xwlee/e27da5738be60cfa92566a18faa2dc27 to your computer and use it in GitHub Desktop.
Save xwlee/e27da5738be60cfa92566a18faa2dc27 to your computer and use it in GitHub Desktop.

Docker version 17.05.0-ce

Create nodes using Docker Machine

$ docker-machine create -d virtualbox --virtualbox-memory "512" manager1
$ docker-machine create -d virtualbox --virtualbox-memory "512" manager2
$ docker-machine create -d virtualbox --virtualbox-memory "512" worker1
$ docker-machine create -d virtualbox --virtualbox-memory "512" worker2

Initialize a Swarm cluster

$ docker swarm init --advertise-addr 192.168.99.100
Swarm initialized: current node (ym4485yyo6jk47c4fsk0gd1q9) is now a manager.

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

    docker swarm join \
    --token SWMTKN-1-05p5dx5rda01ey4m4adwz4gnx3jkonf0ni0inl4rfr4nzfxg7x-7e5506uo3ajss9oi9ntwu5rcg \
    192.168.99.100:2377

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

Join a node to a swarm as a manager

On manager1 node

$ docker-machine ssh manager1

$ docker swarm join-token manager
To add a manager to this swarm, run the following command:

    docker swarm join \
    --token SWMTKN-1-05p5dx5rda01ey4m4adwz4gnx3jkonf0ni0inl4rfr4nzfxg7x-2skdzyrzwlcdgwg0z751mq368 \
    192.168.99.100:2377

On manager2 node

$ docker-machine ssh manager2

$ docker swarm join \
    --token SWMTKN-1-05p5dx5rda01ey4m4adwz4gnx3jkonf0ni0inl4rfr4nzfxg7x-2skdzyrzwlcdgwg0z751mq368 \
    192.168.99.100:2377
This node joined a swarm as a manager.

Join nodes to a swarm as workers

On worker1 node

$ docker-machine ssh worker1

$ docker swarm join \
    --token SWMTKN-1-05p5dx5rda01ey4m4adwz4gnx3jkonf0ni0inl4rfr4nzfxg7x-7e5506uo3ajss9oi9ntwu5rcg \
    192.168.99.100:2377
This node joined a Swarm as a worker.

On worker2 node

$ docker-machine ssh worker2

$ docker swarm join \
    --token SWMTKN-1-05p5dx5rda01ey4m4adwz4gnx3jkonf0ni0inl4rfr4nzfxg7x-7e5506uo3ajss9oi9ntwu5rcg \
    192.168.99.100:2377
This node joined a Swarm as a worker.

List the nodes in the cluster

$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
xn8hpbf333nhgm8teb2ff4rzo *   manager1            Ready               Active              Leader
tle637lhv75eivhjd5g34usph     manager2            Ready               Active              Reachable
mx6wb7hy98tqmel385xp54766     worker1             Ready               Active
584am7k58ry82cuzdvei9zr7i     worker2             Ready               Active

Inspect a node

$ docker node inspect --pretty worker1
ID:			vnk6jaw3s6bwy2knivk4do05m
Hostname:              	worker1
Joined at:             	2017-05-21 03:25:13.459803822 +0000 utc
Status:
 State:			Ready
 Availability:         	Active
 Address:		192.168.99.102
Platform:
 Operating System:	linux
 Architecture:		x86_64
Resources:
 CPUs:			1
 Memory:		492.5MiB
Plugins:
 Network:		bridge, host, macvlan, null, overlay
 Volume:		local
Engine Version:		17.05.0-ce
Engine Labels:
 - provider=virtualbox

Run a service

$ docker service create \
--name frontend \
--replicas 3 \
--publish 80:80/tcp \
--update-delay 10s \
--update-parallelism 1 \
nginx:1.10.1

List services

$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
2e5i31w5k5vq        frontend            replicated          3/3                 nginx:1.10.1        *:80->80/tcp

List all tasks in a service

$ docker service ps frontend
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
u3zyytbzghu1        frontend.1          nginx:1.10.1        worker1             Running             Running 7 minutes ago
caqnkm14qpru        frontend.2          nginx:1.10.1        worker2             Running             Running 8 minutes ago
365uf66huudq        frontend.3          nginx:1.10.1        manager1            Running             Running 8 minutes ago

Inspect a service

$ docker service inspect --pretty frontend
ID:		2e5i31w5k5vqiosh4yxhiprwq
Name:		frontend
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:		nginx:1.10.1@sha256:35779791c05d119df4fe476db8f47c0bee5943c83eba5656a15fc046db48178b
Resources:
Endpoint Mode:	vip
Ports:
 PublishedPort = 80
  Protocol = tcp
  TargetPort = 80
  PublishMode = ingress

Apply rolling updates to a service

Update nginx image from 1.10.1 to 1.11.1

$ docker service update --image nginx:1.11.1 frontend
frontend

Scale the service in the swarm

$ docker service scale frontend=5
frontend scaled to 5

Remove a service

$ docker service rm frontend
frontend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment