2020年9月7日星期一

Create Docker Services under Docker swarm mode

1. Change Docker to Swarm Mode

$ docker swarm init 
Error response from daemon: could not choose an IP address to advertise 
since this system has multiple addresses on different interfaces 
(192.168.0.13 on eth0 and 172.18.0.40 on eth1) - specify one with --advertise-addr
$ docker swarm init  --advertise-addr  192.168.0.13
Swarm initialized: current node (v6kxstlvaocjhffxfq2ot6z4o) is now a manager.

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

  docker swarm join \
  --token SWMTKN-1-607eycc5hm1djdoufl39o5s1n7mwjk9y62gp9fs1szj3gyek4g-619nxsma142dco21h08lgdp2j 192.168.0.13:2377

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

2. Create a docker service under swarm mode.

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

ze4gh6yymi1qb3oe95zn518vx
overall progress: 3 out of 3 tasks 
1/3: running   
2/3: running   
3/3: running   
verify: Service converged 

2. Inspect the redis service.

$ docker service inspect --pretty redis   #if no --pretty, will use json format.

ID:             0u6a4s31ybk7yw2wyvtikmu50
Name:           redis
Service Mode:   Replicated
 Replicas:      3
Placement:
 Strategy:	    Spread
UpdateConfig:
 Parallelism:   1
 Delay:         10s
ContainerSpec:
 Image:         redis:3.0.6
Resources:
Endpoint Mode:  vip

3. Update service to a New Version or a New Image

The swarm manager applies the update to nodes according to the UpdateConfig policy:

$ docker service update --image redis:3.0.7 redis  #update the image to redist:3.0.7
redis
overall progress: 3 out of 3 tasks 
1/3: running   
2/3: running   
3/3: running   
verify: Service converged

#you can update to another image too, like.
# docker service update --image lqwangxg:redis:latest redis 

The scheduler applies rolling updates as follows by default:

  • Stop the first task.
  • Schedule update for the stopped task.
  • Start the container for the updated task.
  • If the update to a task returns RUNNING, wait for the specified delay period then start the next task.
  • If, at any time during the update, a task returns FAILED, pause the update.

Run docker service inspect --pretty redis to see the new image in the desired state:

$ docker service inspect --pretty redis

ID:             0u6a4s31ybk7yw2wyvtikmu50
Name:           redis
Service Mode:   Replicated
 Replicas:      3
Placement:
 Strategy:	    Spread
UpdateConfig:
 Parallelism:   1
 Delay:         10s
ContainerSpec:
 Image:         redis:3.0.7
Resources:
Endpoint Mode:  vip

To restart a paused update run docker service update <SERVICE-ID>. For example:

docker service update redis

To avoid repeating certain update failures, need to reconfigure the service by passing flags to docker service update.

Run docker service ps <SERVICE-ID> to watch the rolling update:

$ docker service ps redis
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
wuxmv524gcfx        redis.1             redis:3.0.7         node1               Running             Running 5 minutes ago                        
i4d67rmdh5q3         \_ redis.1         redis:3.0.6         node1               Shutdown            Shutdown 5 minutes ago                       
bjrgwsvciakh        redis.2             redis:3.0.7         node1               Running             Running 5 minutes ago                        
igi4f40b3v3a         \_ redis.2         redis:3.0.6         node1               Shutdown            Shutdown 5 minutes ago                       
hgimbu95ae5j        redis.3             redis:3.0.7         node1               Running             Running 4 minutes ago                        
xylmkd3fkat1         \_ redis.3         redis:3.0.6         node1               Shutdown            Shutdown 4 minutes ago                       

Before Swarm updates all of the tasks, you can see that some are running redis:3.0.6 while others are running redis:3.0.7. The output above shows the state once the rolling updates are done.

4. Docker node ls.

$ docker node ls 
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
v6kxstlvaocjhffxfq2ot6z4o *   node1               Ready               Active              Leader              19.03.11

5. Open the work node and join to master node by docker join.

$ ip addr  
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: ip_vti0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN qlen 1
    link/ipip 0.0.0.0 brd 0.0.0.0
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:f8:eb:ea:17 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
89158: eth0@if89159: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 16:c1:62:b9:55:a3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.12/23 scope global eth0
       valid_lft forever preferred_lft forever
89162: eth1@if89163: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:12:00:17 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.23/16 scope global eth1
       valid_lft forever preferred_lft forever
# IP=192.168.0.12
$docker swarm join \ 
  --token SWMTKN-1-607eycc5hm1djdoufl39o5s1n7mwjk9y62gp9fs1szj3gyek4g-619nxsma142dco21h08lgdp2j 192.168.0.13:2377
This node joined a swarm as a worker.
# IP=192.168.0.12
$ 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.
# IP=192.168.0.13
$ docker node ls 
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
v6kxstlvaocjhffxfq2ot6z4o *   node1               Ready               Active              Leader              19.03.11
am2twgjm2r44fpejexlurdyty     node2               Ready               Active                                  19.03.11
# IP=192.168.0.11
$docker swarm join \ 
  --token SWMTKN-1-607eycc5hm1djdoufl39o5s1n7mwjk9y62gp9fs1szj3gyek4g-619nxsma142dco21h08lgdp2j 192.168.0.13:2377
This node joined a swarm as a worker.
# IP=192.168.0.13
$ docker node ls 
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
v6kxstlvaocjhffxfq2ot6z4o *   node1               Ready               Active              Leader              19.03.11
am2twgjm2r44fpejexlurdyty     node2               Ready               Active                                  19.03.11
tnnl6qq0h6fucjqojbpbufcgn     node3               Ready               Active                                  19.03.11






没有评论: