Docker basic knowledge notes (5)--Docker Swarm

Table of contents

1. Working mode

2. Build a cluster

 3. Raft protocol

4. Docker Stack

V. Summary


1. Working mode

It is mainly divided into two types of nodes, a management node and a working node, which operate on the management node.

2. Build a cluster

Four Alibaba Cloud servers

1. Configure the management node and configure your own ip address

docker swarm init --advertise-addr 172.16.2.176

2. Other nodes join the management node

work node

# 加入成为工作节点
docker swarm join --token SWMTKN-1-3lxtmz61k5zy62u7w42a0wxan8y08r9j8cl90c6c8l8gqni3m6-etym2ans20zf4u8b5vffnri4u 172.16.2.176:2377

# 查看是否加入成功
[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker node ls
ID                            HOSTNAME                  STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
r6a6kr6vim4cwbyku6oqkcs3i     iZuf6ejgqhj1us1mu9aue2Z   Ready     Active                          20.10.14
uhfwu7g0fz3eo1tohkfo1id0u *   iZuf6ejgqhj1us1mu9aue4Z   Ready     Active         Leader           20.10.14

management node

# 生成加入管理节点token
[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-3lxtmz61k5zy62u7w42a0wxan8y08r9j8cl90c6c8l8gqni3m6-djn81xulnxlf1wciudn7jqukk 172.16.2.176:2377

Generate join token

# 管理节点
[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-3lxtmz61k5zy62u7w42a0wxan8y08r9j8cl90c6c8l8gqni3m6-djn81xulnxlf1wciudn7jqukk 172.16.2.176:2377

# 工作节点
[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-3lxtmz61k5zy62u7w42a0wxan8y08r9j8cl90c6c8l8gqni3m6-etym2ans20zf4u8b5vffnri4u 172.16.2.176:2377

 View node status

# 查看节点
[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker node ls
ID                            HOSTNAME                  STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
jf89a7hleaznfn4gvri2nqcff     iZuf6ejgqhj1us1mu9aue1Z   Ready     Active         Reachable        20.10.14
r6a6kr6vim4cwbyku6oqkcs3i     iZuf6ejgqhj1us1mu9aue2Z   Ready     Active                          20.10.14
q3jtib8o046x7ezy8i2l2ykqd     iZuf6ejgqhj1us1mu9aue3Z   Ready     Active                          20.10.14
uhfwu7g0fz3eo1tohkfo1id0u *   iZuf6ejgqhj1us1mu9aue4Z   Ready     Active         Leader           20.10.14

3. Cluster deployment service

1. Create a service

[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker service create -p 6000:80 --name mynginx nginx
govvq5j2iwamqs1icdzb096j0
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service converged 

[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker service ps mynginx
ID             NAME        IMAGE          NODE                      DESIRED STATE   CURRENT STATE                ERROR     PORTS
pw178f83qskl   mynginx.1   nginx:latest   iZuf6ejgqhj1us1mu9aue4Z   Running         Running about a minute ago             

2. Scaling

# 扩到5个
[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker service update --replicas 5 mynginx
mynginx
overall progress: 5 out of 5 tasks 
1/5: running   [==================================================>] 
2/5: running   [==================================================>] 
3/5: running   [==================================================>] 
4/5: running   [==================================================>] 
5/5: running   [==================================================>] 
verify: Service converged 

# 缩到2个
[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker service scale mynginx=2
mynginx scaled to 2
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged 

3. Delete service

[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker service rm mynginx 
mynginx
[root@iZuf6ejgqhj1us1mu9aue4Z ~]# docker service ls
ID        NAME      MODE      REPLICAS   IMAGE     PORTS

 3. Raft protocol

        Raft protocol: It can be used only when the majority of nodes are guaranteed to survive. As long as it is >1, the cluster must be at least 3!

4. Docker Stack

docker-compose stand-alone deployment project!

docker stack deployment, cluster deployment!

# 单机
docker-compose up -d wordpress.yaml
# 集群
docker stack deploy wordpress.yaml

# docker-compose 文件
version: '3.3'
services:
	mongo:
		image: mongo
		restart: always
		networks:
			- mongo_network
		deploy:
			restart_policy:
			  condition: on-failure
			replicas: 2
	mongo-express:
		image: mongo-express
		restart: always
		networks:
			- mongo_network
		ports:
			- target: 8081
			  published: 80
			  protocol: tcp
			  mode: ingress
		environment:
			ME_CONFIG_MONGODB_SERVER: mongo
			ME_CONFIG_MONGODB_PORT: 27017
		deploy:
			restart_policy:
			  condition: on-failure
			replicas: 1
networks:
	mongo_network:
	  external: true

V. Summary

Docker can initialize a swarm cluster, and other nodes can join. (management, work)

Node is a docker node. Multiple nodes form a network cluster. (management, work)

Service tasks can be run on management nodes or worker nodes. core.

Commands in the Task container, detailed tasks!

Guess you like

Origin blog.csdn.net/qq_38295645/article/details/124172883