Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build Docker Swarm Cluster

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to build Docker Swarm cluster, the content is very detailed, interested friends can refer to it, I hope it can help you.

1. Docker Swarm

Docker Swarm provides Docker container clustering services, which is Docker's official core solution for supporting the container cloud ecosystem.

Using it, users can package multiple Docker hosts into a single large virtual Docker host to quickly build a container cloud platform.

Note: Docker 1.12.0+ Swarm mode has been embedded into Docker engine as docker subcommand docker swarm, most users have started to use Swarm mode, Docker engine API has removed Docker Swarm.

2. Docker Swarm mode

Docker 1.12 Swarm mode has been built into the Docker engine as docker swarm. Please note that it is different from the old Docker Swarm.

Swarm mode has built-in kv storage and provides many new features, such as decentralized design with fault tolerance, built-in service discovery, Load Balancer, routing grid, dynamic scaling, rolling updates, secure transmission, etc. Docker's native Swarm cluster has the strength to compete with Mesos and Kubernetes.

3. Swarm mode basic concept

Swarm is a built-in (native) cluster management and orchestration tool for Docker engines built using SwarmKit.

There are a few concepts you need to understand before using Swarm clustering.

3.1 node

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

Nodes are divided into manager nodes and worker nodes.

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

Work node: a task execution node. The management node delivers services to the work node for execution. By default, administrative nodes are also worker nodes. You can also configure services to run only on administrative nodes.

This image from Docker's website graphically shows the relationship between the administrative nodes and the worker nodes in a cluster.

3.2. services and tasks

A task is the smallest scheduling unit in a Swarm and is currently a single container.

A service is a collection of tasks that define the attributes of the task.

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 Two modes are specified by the--mode parameter of docker service create.

This image from Docker's website graphically illustrates the relationship between containers, tasks, and services.

4. Creating a Swarm Cluster

Reading the Basic Concepts section we know that a Swarm cluster consists of administrative nodes and worker nodes. In this section we create a minimal Swarm cluster with one admin node and two worker nodes.

4.1 preparations

Prepare three virtual machines and install Docker:

192.168.199.241 localhost

192.168.199.243 H243

192.168.199.245 H2454.2. Initializing the cluster

In Docker Machine we learned that Docker Machine can create a virtual Docker host in seconds, let's use it to create three Docker hosts and join them in a cluster.

Initialize a Swarm cluster natively using docker swarm init.

docker swarm init --advertise-addr 192.168.199.241

If your Docker host has more than one NIC and has more than one IP, you must specify the IP using--advertise-addr.

The node that executes docker swarm init automatically becomes the administrative node.

If the execution results are as follows, the initialization is successful:

Swarm initialized: current node (uw3q9lmtglwy3plvcjqv3gunm) is now a manager.

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

docker swarm join --token SWMTKN-1-1zcf4bhlhdarrkw1566xbbt8vrdd91llaj1zhu2nekz56491z2-db9ampabiyc9ynb5fmobr6bob 192.168.199.241:2377

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

On the other two servers execute the Join swarm command output from the previous step when creating the admin node.

docker swarm join --token SWMTKN-1-1zcf4bhlhdarrkw1566xbbt8vrdd91llaj1zhu2nekz56491z2-db9ampabiyc9ynb5fmobr6bob 192.168.199.241:2377

Results:

This node joined a swarm as a worker.

If joining fails, check whether the firewall of the machine managing the node is managed or open the corresponding port.

If you do not save the above command to join the cluster, and there is a machine that wants to join the cluster, you can get the command to join the cluster by issuing the command:

##Get the command to join the cluster again

docker swarm join-token manager 4.4 View clusters

Use docker node ls to view clusters on the admin node

docker node ls

At this point, the smallest cluster is set up.

4.5 Upgrade worker node to admin node

Work nodes can be upgraded to management nodes to ensure high availability of management nodes. Run the following command on the admin node:

##Command Format

docker node promote node [node...]

##Example

docker node promote H243 H2455. deployment services

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

5.1 new service

Run a service called nginx in the created Swarm cluster.

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

Now we use the browser, enter any node IP, you can see the default page of nginx.

If you want to modify the number of service copies, you can execute the command:

##This command assumes that the nginx service already exists

docker service scale nginx=25.2 View Services

Use docker service ls to view the services currently running on the Swarm cluster.

docker service ls

Use docker service ps to view details of a service.

docker service ps nginx

Use docker service logs to view logs for a service.

docker service logs nginx5.3 Remove service

Use docker service rm to remove a service from a Swarm cluster.

docker service rm nginx6. Graphic management tool Portainer

Docker Swarm allows you to quickly build a minimal cluster and deploy services on the cluster, but there is no unified entry to view node resource usage.

Portainer is Docker's graphical management tool, providing status display panel, rapid deployment of application templates, basic operations of container image network data volumes (including uploading and downloading images, creating containers, dynamic expansion, etc.), event log display, container console operations, centralized management and operations of Swarm clusters and services, login user management and control, etc. The functions are very comprehensive and can basically meet all the needs of small and medium-sized enterprises for container management.

6.1 Download Portainer Image and Run Container ##Find Portainer Image

docker search portainer

##Pull Mirror

docker pull portainer/portainer

##Running containers

docker run -d -p 9000:9000 \

--name portainer --restart=always \

-v /var/run/docker.sock:/var/run/docker.sock \

portainer/portainer6.2 Access Portainer

Enter http://192.168.199.241:9000/in the browser

About how to build Docker Swarm cluster to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report