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 the engine Swarm of Docker

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the Docker engine Swarm how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that we read this Docker engine Swarm how to use the article will have a harvest, let's take a look.

Swarm is a platform launched by Docker to manage docker clusters, which is almost entirely developed in go language. It turns a group of Docker hosts into a single virtual host, and Swarm uses the standard Docker API interface as its front-end access, in other words, various forms of Docker

Client (compose,docker-py, etc.) can communicate with Swarm directly, and even Docker itself can easily integrate with Swarm, which greatly facilitates users to transplant the system based on single node to Swarm. At the same time, Swarm has built-in support for Docker network plug-ins, and users can easily deploy container cluster services across hosts.

Like Docker Compose, Docker Swarm is the official container orchestration project of Docker, but the difference is that Docker Compose is a tool for creating multiple containers on a single server or host, while Docker Swarm can create container cluster services on multiple servers or hosts. Obviously, Docker Swarm is more suitable for micro-service deployment.

Since Docker version 1.12.0, Docker Swarm has been included in the Docker engine (docker swarm), and the service discovery tool has been built in, so we don't need to configure Etcd or Consul for service discovery configuration as before.

N is just a Scheduler and router. Swarm does not run the container itself. It only accepts requests from Docker clients and dispatches appropriate nodes to run the container. This means that even if Swarm dies for some reason, the nodes in the cluster will run as usual. After Swarm resumes operation, it will collect and rebuild cluster information.

Technical summary

It would be humiliating to copy the detailed and useful documentation of Docker here, so I'll briefly summarize the technology. We already have Docker, right? Now, you want more servers as Docker hosts, but at the same time you want them to belong to the same logical entity. In other words, you want to build a cluster.

Let's start with a cluster of hosts. When you initialize a Swarm cluster on a host, the host becomes the administrator of the cluster, manager. From a technical point of view, it becomes a node node in the consensus group consensus group. The mathematical logic behind it is based on the Raft algorithm. The manager manager is responsible for scheduling tasks. The specific tasks will be assigned to each worker worker node that has joined the Swarm cluster. These operations will be managed by Node API. Although I hate the word API, I have to use it here.

Service API is the second component in this implementation. It allows the manager manager node to create a distributed service on all Swarm cluster nodes. This service can be replicated replicated, that is, they (LCTT) will be assigned to the cluster by the balancing mechanism (LCTT mode, in which multiple container instances will automatically schedule tasks to some qualified nodes in the cluster), or can be assigned globally (LCTT mode), which means that each node will run a container instance.

Configure mirrors and services

I will try to configure a load-balanced Apache service and use multiple container instances to provide page content through a unique IP address. It's pretty standard (LCTT). This example also highlights most of the reasons why you want to use clustering: availability, redundancy, scale-out, and performance. Of course, you need to consider both networking and storage, but they are beyond the scope of this guide.

This Dockerfile template can actually be found under httpd in the official image repository. You only need the simplest setting to get started. As for how to download or create your own image, please refer to my getting started guide, and the link can be found at the top of this tutorial.

Docker build-t my-apache2 .Sending build context to Docker daemon 2.048 kBStep 1: FROM httpd:2.4Trying to pull repository docker.io/library/httpd... 2.4: Pulling from docker.io/library/httpd8ad8b3f87b37: Pull completec95e1f92326d: Pull complete96e8046a7a4e: Pull complete00a0d292c371: Pull complete3f7586acab34: Pull completeDigest: sha256:3ad4d7c4f1815bd1c16788a57f81b413...a915e50a0d3a4Status: Downloaded newer image for docker.io/httpd:2.4--- > fe3336dd034dStep 2: COPY.. / public-html/ / usr/local/apache2/htdocs/...

Image created

Before you proceed with the following steps, you should make sure that you can launch a container instance without error and link to the web server. (LCTT: use the following command). Once you make sure you can connect, we can start creating a distributed service.

Docker run-dit-- name my-running-app my-apache2

Type this IP address into the browser and see what happens.

Swarm initialization and configuration

The next step is to start the Swarm cluster. You will need these basic commands to get started, which are very similar to the examples in the Docker blog:

Docker service create-name frontend-replicas 5-p 80:80/tcp my-apache2:latest

What have we done here? We created a service called frontent, which has five container instances. At the same time, we also bind port 80 of the host to the port 80 of these containers. We will use the newly created Apache image to do this test. However, when you type the above instructions directly on your computer, you will see the following error:

Docker service create-- name frontend-- replicas 5-p 80:80/tcp my-apache2:latestError response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

This means that you have not configured your host (node) as a Swarm manager manager. You can initialize the Swarm cluster on this host or let it join an existing cluster. Since we don't have an off-the-shelf cluster yet, we will initialize it (LCTT: initialize the Swarm cluster and make the current node manager):

Docker swarm initSwarm initialized: current node (dm58mmsczqemiikazbfyfwqpd) is now a manager.

To add a worker worker to the Swarm cluster, execute the following instructions:

Docker swarm join\-- token SWMTKN-1-4ofd46a2nfyvrqwu8w5oeetukrbylyznxla9srf9vxkxysj4p8-eu5d68pu5f1ci66s7w4wjps1u\ 10.0.2.15VR 2377

To add a manager manager to the Swarm cluster, execute docker swarm join-token manager and follow the instructions.

The output after the operation is clear without explanation. We successfully created a Swarm cluster. The new nodes will need the correct token token to join the Swarm cluster. If you need to configure a firewall, you also need to find its IP address and port. (the LCTT refers to the port required for Docker's Swarm mode communication, default is 2377). In addition, you can add a manager node to the Swarm cluster. Now, re-execute the service creation instructions you just did:

Docker service create-- name frontend-- replicas 5-p 80:80/tcp my-apache2:latest6lrx1vhxsar2i50is8arh5ud1 test connectivity

Now, let's verify that our service is really working. In some ways, this is very similar to what we did in Vagrant and coreOS. After all, their principles are almost the same. It's just different implementations of the same guiding ideology (LCTT). First of all, you need to make sure that docker ps gives the correct output. You should be able to see multiple container copies of the service you created.

Docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScda532f67d55 my-apache2:latest "httpd-foreground" 2 minutes ago Up 2 minutes 80/tcp frontend.1.2sobjfchdyucschtu2xw6ms9a75fe6e0aa77b my-apache2:latest "httpd-foreground" 2 minutes ago Up 2 minutes 80/tcp Frontend.4.ag77qtdeby9fyvif5v6c4zcpc3ce824d3151f my-apache2:latest "httpd-foreground" 2 minutes ago Up 2 minutes 80/tcp frontend.2.b6fqg6sf4hkeqs86ps4zjyq65eda01569181d my-apache2:latest "httpd-foreground" 2 minutes ago Up 2 minutes 80/tcp frontend.5.0rmei3zeeh8usagg7fn3olsp4497ef904e381 my-apache2:latest "httpd-foreground" 2 minutes ago Up 2 minutes 80/tcp frontend.3.7m83qsilli5dk8rncw3u10g5a

I also tested different, unconventional ports, and they all worked. You will have a lot of configurable leeway on how you connect to the server and receive requests. You can use the correct port of the IP address of the localhost or Docker network interface (docker0, the default bridge of Docker, whose gateway is 172.17.0.1). The following example uses port 1080:

Replicated Web service works

So far, this is a very rough and simple start. The real challenge is to create an optimized, extensible service, but they require an accurate technical use case. In addition, you will use the docker info and docker service (as well as inspect and ps) commands to learn more about how your cluster works.

Problems that may be encountered

You may encounter some minor problems (maybe not that small) when playing with Docker and Swarm. For example, SELinux may complain that you are performing some illegal operation (LCTT). However, these mistakes and warnings should not hinder you too much.

SELinux alert

Docker service is not a command (docker service is not a docker command) when you try to execute the necessary command to create a replication mode replicated service, you may encounter an error that says docker: 'service' is not a docker command. This means that your version of Docker is incorrect (use the-v option to check). We will discuss how to fix this in future tutorials.

Docker service create-- name frontend-- replicas 5-p 80:80/tcp my-apache2:latestdocker: 'service' is not a docker command.

Docker tag does not recognize (docker tag not recognized) you may see the following error:

Docker service create-name frontend-replicas 5-p 80:80/tcp my-apache2:latestError response from daemon: rpc error: code = 3 desc = ContainerSpec: "- name" is not a valid repository/tag

There have been many discussions and posts about this error. In fact, this mistake may be quite innocent. You may have pasted the command from the browser, and the lines in the browser may not have been parsed correctly (the author's note: you should use-name instead of-name). Is caused by such a simple reason.

This is the end of the article on "how to use Swarm, the engine of Docker". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use Swarm, the engine of Docker". If you want to learn more, 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

Development

Wechat

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

12
Report