An overview of the

Docker Swarm is a very simple docker native cluster deployment environment. Before Docker swarm came out, to build a Docker cluster required complex network operation and peacekeeping configuration capabilities, but Docker Swarm liberated all of these. To put it simply, Docker swarm connects multiple Docker nodes into a cluster, which can be considered as an independent LAN that can communicate with each other, and manages the application life cycle by way of service. For example, there is a microservice of MyService, in the single-machine mode, you need to start the corresponding Docker container on two nodes respectively, while Docker swarm directly has the concept of “service”, as long as the service creation command is executed on a certain management node, and the number of nodes is specified as 2. The cluster automatically finds two nodes to run your MyService microservice separately. If you want to increase to four nodes when two nodes are no longer viable, you only need to execute a single extension command.

Docker installation

Docker cluster constructed based on centos version 7.2 in this paper, docker version 17.09 is used.

The node has two nodes 192.168.0.37 and 192.168.0.38, which are management nodes and working nodes respectively. The work node can run services, and the management node can run services, but with more “administrative control” than the work node, that is, the ability to create services. To avoid brain splitting, the number of management nodes is 1,3, and 5 odd. Install Docker on 0.37 and 0.38 respectively.

Medium to download

Download.docker.com/linux/cento… Wget download.docker.com/linux/cento…

The installation

Yum install docker – ce – 17.09.0. Ce – 1. El7. Centos. X86_64. RPM

Start the

systemctl start docker

Powered up

Systemctl enable docker or chkconfig docker on

hostname

Vi /etc/hosts Add the host and IP address of the two nodes.

Build and use a Docker Hub private repository

Build docker Hub private warehouse (0.37)

The repository is used to store docker images. When creating and starting the service, multiple nodes need to pull images from the repository.

sudo docker run -d -p 5000:5000 -v /root/hub:/var/lib/registry --restart=always --name registry2 registry:2Copy the code

By default, repositories are stored in /var/lib/registry, so if the container is deleted, images stored in the container will also be lost, so we usually specify a local directory to mount.

Github.com/docker/dock…

Configuring Pull Mirror Authentication (0.37/0.38)

Client access docker hub pull mirror, walk the HTTPS by default, will be submitted to the https://192.168.0.37:5000 image not found or does not exist (No to image: 192.168.0.37:5000 / cehome/operation – service:), the solution is a kind of by adding the HTTPS proxy nginx, we adopt is another is a safe way to:

  • Open the
  1. /usr/lib/systemd/system/docker.service
  • ExecStart=/usr/bin/dockerd –insecure — registry 192.168.0.37:5000
  • Overloading systemctl daemon – reload
  • Service docker restart

Cluster Environment Construction

Reference: docs.docker.com/engine/swar… Docs.docker.com/engine/refe…

Docs.docker.com/engine/swar… Docs.docker.com/engine/swar…

Disabling the Firewall (0.37/0.38)

Need to access port 2377 and other ports (for convenience, directly turn off the firewall, otherwise there may be unexpected things)

  • To view
  1. is-enabled firewalld
  • Shut down
  1. stop firewalld.service
  • Disable the systemctl disable firewalld service

Initializing management Nodes (0.37)

Docker swarm init –advertise-addr 192.168.0.37

Docker swarm init –advertise-addr docker swarm init –advertise-addr docker swarm init –advertise-addr

Check tokens before adding other nodes (0.37)

To add other nodes to the cluster, you must first run the following command on the management node, which prints out the complete script containing the tokens to be executed on the other nodes.

Docker swarm join-token manager docker swarm join-token manager docker swarm join-token manager docker swarm join-token manager docker swarm join-token manager

Adding a Work Node (0.38)

You can run the preceding command to find the script executed, copy it to 0.38, and execute it (that is, the script is from the printed result of executing the Docker swarm join-token worker in 0.37). After executing 0.38, the script is added to the cluster. docker swarm join\ –token SWMTKN-1-3jcd5cjgwy8ct1gfyqqy8oqnokz4jjaul7el97h77qii4e6zw3-1vrjlin1bl5k9whzc4fulqfh2 192.168.0.37:2377

Viewing nodes (0.37)

Docker node ls can be executed only on the management node

Create overlay Network

Why do YOU need overlay networks? Ensure communication between multiple machines and different containers!

Docker network create –attachable — Driver overlay tieJIA –attachable parameters can be added to the network for compatibility with standalone containers. Tiejia is the network name.

Back up and restore management node information

Because at present is single management node, it is best to backup information, the main steps is to stop the docker, backup directory/var/lib/docker swarm/reference: docs.docker.com/engine/swar…

Commands to remove nodes (this is just an introduction, do not execute it)

Docker swarm Leave-f docker swarm Leave-f docker swarm Leave-f docker swarm Leave-f Docker swarm Leave-f

Example: Deploying an Nginx service

Deploy the nginx service on manager node 0.37, the number of services is 3, the specified port is 8080 mapping container 80, using nginx mirroring. The nginx image will be downloaded directly from the official website. The private repository deployed above will not be used.

docker service create --replicas 3 --name nginx --publish 8080:80  nginxCopy the code

Run the docker service ps nginx command to check service distribution.