docker swarm in action

Docker Swarm is one of Docker's official Three Musketeers projects, providing Docker container cluster services, and is the core solution of Docker's official support for the container cloud ecosystem.


1. Basic concepts:

    A host running Docker can actively initialize a Swarm cluster or join an existing Swarm cluster, so that the host running Docker becomes a node of a Swarm cluster.

     Nodes are divided into management (manager) nodes and work (worker) nodes.

    The management node is used for the management of the Swarm cluster, and the docker swarm command can only be executed on the management node (the node exiting the cluster command docker swarm leave can be executed on the worker node). A Swarm cluster can have multiple management nodes, but only one management node can become the leader, and the leader is implemented through the raft protocol.

    The worker node is the task execution node, and the management node delivers the service to the worker node for execution. Manager nodes also act as worker nodes by default. You can also configure the service to run only on the management node.

2. Services and tasks:

    Task (Task) is the smallest scheduling unit in Swarm, which is currently a single container.

    Services (Services) refers to a collection of tasks, and services define the properties of tasks. There are two modes of service:

        replicated services run a specified number of tasks on each worker node according to certain rules.

        global services run a task on each worker node

    The two modes are specified by the --mode parameter of docker service create


3. Create a swarm cluster:

    Create a minimal swarm cluster with 1 manager node and 2 worker nodes.

docker-machine create -d xhyve --xhyve-boot2docker-url ~/.docker/machine/cache/boot2docker.iso --engine-registry-mirror=https://registry.docker-cn.com manager
docker-machine ssh manager
docker swarm init --advertise-addr 192.168.99.100

docker-machine create -d xhyve --xhyve-boot2docker-url ~/.docker/machine/cache/boot2docker.iso --engine-registry-mirror=https://registry.docker-cn.com worker1
docker-machine ssh worker1
docker swarm join --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
192.168.99.100:2377

docker-machine create -d xhyve --xhyve-boot2docker-url ~/.docker/machine/cache/boot2docker.iso --engine-registry-mirror=https://registry.docker-cn.com worker2
docker-machine ssh worker2
docker swarm join --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
192.168.99.100:2377

4. Deployment Services

    Use the docker service command to manage services in a Swarm cluster, which can only be run on the management node.  

    New service:

docker service create --replicas 3 -p 80:80 --name nginx nginx:latest

    View Services:

$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                 PORTS
kc57xffvhul5        nginx               replicated          3/3                 nginx:1.13.7-alpine   *:80->80/tcp

    To view the details of a service:

$ docker service ps nginx
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
pjfzd39buzlt        nginx.1             nginx:1.13.7-alpine   swarm2              Running             Running about a minute ago
hy9eeivdxlaa        nginx.2             nginx:1.13.7-alpine   swarm1              Running             Running about a minute ago
36wmpiv7gmfo        nginx.3             nginx:1.13.7-alpine   swarm3              Running             Running about a minute ago

    View log:

$ docker service logs nginx
nginx.3.36wmpiv7gmfo@swarm3    | 10.255.0.4 - - [25/Nov/2017:02:10:30 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0" "-"
nginx.3.36wmpiv7gmfo@swarm3    | 10.255.0.4 - - [25/Nov/2017:02:10:30 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0" "-"
nginx.3.36wmpiv7gmfo@swarm3    | 2017/11/25 02:10:30 [error] 5#5: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.255.0.4, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.99.102"
nginx.1.pjfzd39buzlt@swarm2    | 10.255.0.2 - - [25/Nov/2017:02:10:26 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0" "-"
nginx.1.pjfzd39buzlt@swarm2    | 10.255.0.2 - - [25/Nov/2017:02:10:27 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0" "-"
nginx.1.pjfzd39buzlt@swarm2    | 2017/11/25 02:10:27 [error] 5#5: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.255.0.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.99.101"

    Delete service:

$ docker service rm nginx


5. Use the compose file to deploy multiple services at a time

    The deployment service uses docker stack deploy, where the -c parameter specifies the compose file name.

$ docker stack deploy -c docker-compose.yml wordpress


Remark:

    Reference article: http://docker_practice.gitee.io/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325992391&siteId=291194637