Construction of docker swarm cluster

Time:2021-7-30

@[TOC]

Environment introduction

My two virtual hosts

The host name CentOS IP 192.168.137.200 is the management node
The host name node1 IP 192.168.137.2 is a normal node

The system is CentOS 7 64 bit

Install docker

Install docker in two hosts respectively

curl -sSL https://get.daocloud.io/docker | sh

Power on

service docker start
systemctl enable docker

Execute docker info. My current server version is 19.03.2

open port

firewall-cmd --zone=public --add-port=2377/tcp --permanent &&
firewall-cmd --zone=public --add-port=7946/tcp --permanent &&
firewall-cmd --zone=public --add-port=7946/udp --permanent &&
firewall-cmd --zone=public --add-port=4789/udp --permanent &&
firewall-cmd --reload

Or turn off the firewall directly

Systemctl stop firewalld.service # stop firewall
Systemctl disable firewalld.service # disable firewall startup

Port 2377 is a cluster management communication port, which only needs to be opened at the management node

7946 TCP and UDP are the communication ports between nodes,

4789 is the port used by overlay network.

If you need to expose other services in the cluster, you need to open the corresponding port, such as 80

Of course, if you are an alicloud host, the corresponding port is enabled in the security policy. If all nodes are in the same region, you can use intranet IP without opening the port.

Create cluster

Execute on management node

docker swarm init --advertise-addr 192.168.137.200

We get

[[email protected] ~]# docker swarm init --advertise-addr 192.168.137.200
Swarm initialized: current node (ykiyahkjoq3q0dn2rrnqd4ery) is now a manager.

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

    docker swarm join --token SWMTKN-1-4ggr6t9qpoipck2ksobnphhwe0kcr47idiwaezh9uu672ueokf-7vchqxxkk4p7vavh9h4hijydw 192.168.137.200:2377

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

It has been explained in detail here. Adding a common node only requires execution

docker swarm join --token SWMTKN-1-4ggr6t9qpoipck2ksobnphhwe0kcr47idiwaezh9uu672ueokf-7vchqxxkk4p7vavh9h4hijydw 192.168.137.200:2377

We execute in node1, where we specify the local IP through advertise addr

docker swarm join --advertise-addr 192.168.137.2 --token SWMTKN-1-4ggr6t9qpoipck2ksobnphhwe0kcr47idiwaezh9uu672ueokf-7vchqxxkk4p7vavh9h4hijydw 192.168.137.200:2377

As you can see, our node1 has successfully joined the cluster

[[email protected] ~]# docker swarm join --advertise-addr 192.168.137.2 --token SWMTKN-1-4ggr6t9qpoipck2ksobnphhwe0kcr47idiwaezh9uu672ueokf-7vchqxxkk4p7vavh9h4hijydw 192.168.137.200:2377
This node joined a swarm as a worker.

We can also see this node by executing docker node list on the management node

[[email protected] ~]# docker node list
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
ykiyahkjoq3q0dn2rrnqd4ery *   centos              Ready               Active              Leader              19.03.2
hr3u7u010po2w6ugtcbbil8e7     node1               Ready               Active                                  19.03.2

The token can be obtained by executing docker swarm join token worker on the management node

[[email protected] ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-4ggr6t9qpoipck2ksobnphhwe0kcr47idiwaezh9uu672ueokf-7vchqxxkk4p7vavh9h4hijydw 192.168.137.200:2377

If you want to join a management node, execute docker swarm join token manager to obtain the token to join the management node. It is recommended to have more than three management nodes for formal deployment

[[email protected] ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-4ggr6t9qpoipck2ksobnphhwe0kcr47idiwaezh9uu672ueokf-9e6xiw7k9krf3abc2p3jrw9yn 192.168.137.200:2377

Create private network

Services in the cluster can communicate freely, but at present, we have some single containers deployed on the management node, such as MySQL and redis. A private network is required to provide communication. Execute on the management node

Docker network create - D overlay -- attachable my net # adds the attachable parameter so that a single container can also be attached to this network

Installing the portal panel

This is a visual management panel of the cluster, which is executed at the management node

docker run -d --name portainer --restart=always -p 8000:8000 -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v /home/data/portainer/data:/data portainer/portainer

Restart docker

service docker restart

The browser accesses our management node: 192.168.137.200:9000. The password needs to be set for the first access
Construction of docker swarm cluster
Select local to connect to our local docker cluster
Construction of docker swarm cluster
Enter and select local, and the visual management panel is installed
Construction of docker swarm cluster

Install MySQL and redis

docker run -d --name mysql8 \
   --restart=always \
   --network my-net \
   -v /home/data/mysql8:/var/lib/mysql \
   -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=user_center \
   mysql:8.0.18 \
   --sql_mode=NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION \
   --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

MySQL configuration

docker exec -it mysql8 bash
mysql -uroot -p123456
alter user 'root'@'%' identified with mysql_ native_ password by '123456'; # Change authentication plug-in
docker run -d --name redis \
   --restart=always \
   --network my-net \
   -v /home/data/redis:/data \
   redis:5.0.6 \
   --appendonly yes

Here, we do not map the port to the host, and join the private network my net, and then the services in the cluster can pass through
MySQL 8: 3306 root 123456 and redis: 6379 access MySQL and redis. MySQL now has a user_ Center database. If you need to create a database, you can enter the container and execute the command. Future tutorials will talk about deploying MySQL and redis clusters.

PHP microservice training series indexBlog: PHP micro service training series tutorial

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Programming Xiaobai must understand the network principle

How is the network composed? Why can we easily surf the Internet now?Whether you are a computer major or not, you may always have such questions in your heart!And today we will solve this matter and tell you the real answer! Basic composition of network First, let’s look at this sentence Connect all computers together […]