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 run docker swarm mode in docker

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to run docker swarm mode in docker. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Install docker

Install using yum

1. Update yum source, sudo yum update;,.

two。 Add docker's yum repository

3. Install docker-engine,sudo yum install docker-engine

4. Start the docker daemon, sudo systemctl start docker

This is the end of the installation of docker. Here are some issues that you need to pay attention to when installing docker through agents in the company's intranet environment.

1. First of all, let the machine surf the Internet through the agent.

Modify / etc/profile to add export http_proxy= proxy username: password @ proxy address: Port

Make the changes effective, source / etc/profile

two。 Configure yum to use proxies

Vim / etc/yum.conf adds proxy=http:// proxy username: password @ proxy address: Port

After these two steps, you can install docker through an agent on the intranet.

Configure docker

Open docker remote management port 2375 and configure docker hub domestic warehouse.

1. Create a new docker profile

Vim / etc/sysconfig/docker

Increase

DOCKER_OPTS= "- H unix:///var/run/docker.sock-H 0.0.0.0 registry-mirror= 2375-- address of domestic warehouse"

If you need to access through a proxy, you also need to add

HTTP_PROXY=http:// proxy username: password @ proxy address: Port

two。 Load the configuration file when you start the docker daemon

Vim / lib/systemd/system/docker.servic

Modify the ExecStart configuration to

ExecStart=/usr/bin/dockerd $DOCKER_OPTS

Add the configuration of configuration file path in [Service]

EnvironmentFile=-/etc/sysconfig/docker

Specify the configuration file load path to the configuration file / etc/sysconfig/docker created in the first step

3. Restart the docker daemon

Sudo systemctl stop docker

Sudo systemctl start docker

4. Check whether the configuration is in effect

Ps-ef | grep docker

If the configuration takes effect, the parameters just configured will be added after the dockerd process.

Start swarm mode

You only need to initialize the swarm cluster on one docker node, and the other nodes join the cluster.

Select a docker node as the leader of swarm mode, and run

Docker swarm init-- advertise-addr host ip

Follow the prompts to execute commands on other nodes to join the swarm cluster

Execute docker node ls on the leader node to view node information

Create a cluster network

1. Run on any node in the swarm cluster

Docker network ls to view current network status

The network whose scope is local can only work on the local machine. Create the network used by the swarm cluster below.

two。 Run docker network create-- driver overlay my-network

Create a cluster network called my-network

Run docker network ls again, and you can see that the my-network you just created, scope is swarm, and this network can be used in swarm mode and allows containers on multiple nodes to communicate with each other.

When you create a service in swarm, you can specify the network to use by the-- network network name. If multiple service uses the same network whose scope is swarm, they can communicate with each other through the name of the service.

Docker swarm is easy to use

The following is a brief introduction to the use of docker swarm, if you are interested, you can check the official docker documentation for in-depth understanding.

The following commands are all run on the leader node of swarm!

Docker service create-replicas 1-name tomcat-publish 9090 name tomcat 8080 tomcat:latest

A service,swarm named tomcat is created to encapsulate the container as service, similar to pod in kubernates, and a service is a combination of containers that can be dispatched by swarm.

The-- replicas 1 here specifies the number of copies of service.

-- publish 9090 8080 publishes port 8080 in the container to port 9090 of the host

Run docker service ls to see the current list of services, and you can see that there is a service named tomcat.

By running docker service ps tomcat, you can see which swarm node the tomcat service is running on and its current running status.

Switch to the node where the tomcat service is running, and run docker ps to see the ID of the container and the port mapping.

Run the docker logs-f container ID to view the tomcat run log.

After tomcat starts successfully, you can access the tomcat home page in the browser, the address is the ip of the docker node (any node is fine), the port is 9090, which is the publish specified when the service is created.

A tomcat service has been successfully run above, and the service is expanded here.

Run docker service scale tomcat=2 to extend the tomcat service to two instances.

When you run docker service ls, you can see that the replicas of the tomcat service changes to 1max 2, and when the second instance starts successfully, it becomes 2max 2.

Run docker service ps tomcat to see which docker node the two tomcat service instances are running on, and the running status.

Swarm will automatically do load balancing in two service

Swarm also has some other commands, such as deleting the specified service from the docker service rm service name, and publishing in grayscale. For more information, please refer to the official documentation.

Problems that should be paid attention to in swarm mode

Swarm only schedules containers between docker nodes, not the mount volume volume used by the container. When deploying a database container with swarm, you need to pay attention to the data file problem. Or containers with external configuration files are loaded, and you also need to pay attention to the problems with configuration files.

A simple solution is to have these containers dispatched to the same docker node each time. You need to specify the-- constraint parameter when creating the service, such as-- constraint 'node.hostname==myhost', so that the created service will only be dispatched to the docker node with the hostname myhost.

On how to run the docker swarm mode in docker to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Original link: https://my.oschina.net/lhztt/blog/789502

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