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 use Swarm in Docker

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

In this issue, the editor will bring you about how to use Swarm in Docker. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Swarm itself means "swarm", crowd, bee swarm. This refers to the state of the computer cluster (cluster) after connecting with Docker. The docker swarm command creates, joins, and leaves a cluster.

Node is a computer node, and it can also be thought of as a Docker node. Node is divided into two categories: Manager and Worker. A Swarm must have at least one Manager, and some administrative commands can only be used on the Manager. Both types of Node can run Service, but the run command can only be executed on Manager. For example, only in Manager can you use the docker node command to view, configure, and delete Node.

Stack is a set of Service, similar to docker-compose. By default, a Stack shares one Network, is accessible to each other, and is isolated from other Stack networks. This concept is only for the convenience of arrangement. The docker stack command makes it easy to manipulate a Stack instead of manipulating Service one by one.

Service is a type of container. For users, Service is the core content of interacting with Swarm. Service has two modes of operation, one is replicated, which specifies the number of Service running containers, and the other is global, which runs one of these containers on all Node that meet the operating conditions. The docker service command can manipulate Service in Swarm.

Task refers to the task of running a container and is the smallest unit in which Swarm executes commands. To run a Service successfully, you need to execute one or more Task (depending on the number of containers in a Service) to ensure that each container starts smoothly. Typically, users operate on Service, not Task.

Load balancing is load balancing and also includes reverse proxies. Swarm uses load balancer in the form of Ingress, that is, accessing a Published port of each node can automatically proxy to the real service. The general principle is shown in the following figure.

Ingress network

Replicated Mode

Services: some-serivce:... Deploy: mode: replicated replicas: 3

By default, mode is replicated, so this line can be omitted. The default number of replicas is 1, which means that the Service only launches 1 container. In this mode, you can start multiple services as needed, Swarm will adjust automatically, and sometimes a Node will start multiple containers.

Global Mode

Services: some-serivce:... Deploy: mode: global placement:...

Deploy one of all deployable Node. With placement, you can limit the Node that meets the criteria and avoid deployment in an inappropriate Node.

Operation

Here are some common specific operations.

Create the first Node

Docker swarm init-- advertise-addr $IP

IP is the externally accessible IP of the current Node, making it easy for other Node to address.

In this way, a Swarm is initialized and has only one Manager node.

Add a new Node to Swarm

On the Manager node, execute the following command to see how to join a Node:

$docker swarm join-token manager To add a manager to this swarm, run the following command: docker swarm join--token SWMTKN-1-2zspelk468gb6wgw5adea4wlbw4kfy3q1uhr86zpafl9m5a3ho-ezs4fylj526e801b3cl0pojr5 10.174.28.52 docker swarm join- 2377$ docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join--token SWMTKN-1-2zspelk468gb6wgw5adea4wlbw4kfy3q1uhr86zpafl9m5a3ho-164iqklrfv8o3t55g088hylyk 10.174.28.52 docker swarm join- 2377

On a machine without any Swarm, execute the command docker swarm join-- token... shown above to become the Manage or Worker node of this Swarm.

Set Node Label

On the Manager node, you can label any node:

Docker node update $node_name-- label-add main=true

$node_name is the ID or HOSTNAME of the node to be set. Label is in the form of key-value pairs, main in main=true is the key, and true is the value.

Once Label is set, you can use constraints in placement to restrict available nodes in the Compose file.

Services: some-serivce:... Deploy: placement: constraints:-node.labels.main = = true.

The above configuration allows some-service to be used only on nodes where Label is set to main=true.

Start and stop servic

Docker stack deploy $stack_name-c docker-compose.yaml-c other.yaml...

$stack_name is the Stack name. You can specify multiple docker-compose files with-c, or you can deploy multiple files in batches under the same Stack. These YAML files are written in a way that is not fundamentally different from those previously executed with the docker-compose command, except that the following unique configurations are added and some configurations that are not supported in Swarm scenarios are ignored.

It is recommended that you use a docker-compose file to orchestrate the Stack, rather than manually creating it directly with docker service create. For detailed configuration items, please refer to "Compose file version 3 reference | Docker Documentation".

When you need to stop all services of Stack, you can execute the following command.

Docker stack rm $stack_name

Update the image of a running service

Docker service update-- image $image:$tag $service_name above is how Swarm is used in the Docker shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Servers

Wechat

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

12
Report