Portainer connects to swarm cluster

To install portainer, it is recommended to install portainer to a non-application server, so that it is separated from the application, and portainer can also manage multiple docker nodes at the same time

The process of installing portainer is very simple and can be installed directly using docker.

Method 1: Run from the command line

docker run -d -p 9000:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock \
--name prtainer-test portainer/portainer

Method 2: Start via compose-file.

docker-compose.yml

version: '3'
services:
  portainer:
    image: portainer/portainer
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - 9000:9000
   
#使用命令启动portainer
docker-compose up -d

Access portainer is accessed through ip + port. Such as 10.99.23.10:9000.

Enter the portainer to set the username and password first, and then you can start connecting to the docker host.

04swarm18

There are three types of management docker hosts:

  • Manage local docker hosts
  • Manage remote docker hosts
  • Manage swarm clusters

1. Manage the local host.
Take node-1 as an example
, visit http://node-1IP:9000 and set account password to log in

04swarm19

Just click the Connect button to directly manage the local Docker container

2. Manage remote hosts
To manage node-2 on node-1, you first need to open port 2375 of node-2

# 被管理节点
vi /usr/lib/systemd/system/docker.service
#修改为
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
#重启docker
systemctl daemon-reload
systemctl restart docker

node-1portainer management interface to add endpoint
04swarm20

3. Manage swarm cluster
To add swarm cluster management, you need to install agent on each node and write yml file

portainer-agent.yml

version: '3.2'
services:
  agent:
    image: portainer-agent
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /var/lib/docker/volumes:/var/lib/docker/volumes
    ports:
      - target: 9001
        published: 9001
        protocol: tcp
        mode: host
    networks:
      - portainer_agent
    deploy:
      mode: global
      placement:
        constraints: [node.platform.os == linux]

networks:
  portainer_agent:
    driver: overlay

The deploy.mode = global defined in compose-file indicates that each node in the cluster will deploy a service, and placement indicates the deployment constraints.

What needs to be noted here is to ensure that each node has a portainer-agent image, or each node can connect to Harbor (dockerhub). At the same time, ensure that each node has the same mapping, that is, according to the above script, ensure that each node has a /var/lib/docker/volumes path. When I started the agent service just now, I found that the agent service was only started on the node I executed, and other nodes found that the agent service was not started. Later, I found that other nodes did not have mapped paths, and there was no portainer-agent image. After these are resolved, all nodes are ready for use.

If you really can't find the reason, you can use the command to find the reason for the startup failure.

# 查找服务列表
docker service ls
# 查看服务详细信息
docker service ps pa_agent

At the same time, there are two parameters in the portainer-agent.yml file that need attention:

  • The mode value under port is host, indicating that port 9001 is released on each node, but this port has no load balancing function, and the default ingress has load balancing function, which can be tested by deploying a similar nginx test service, here is attached swarm The host mode and ingress mode of the port are explained in the document
  • The mode value under deploy is global, which means that a task must be started on each node, and the newly added swam node will automatically discover and start the service, while replicated only guarantees the total number of tasks, and does not mandate tasks for each node number, so the global mode is especially suitable for deploying applications that need to run on each node, such as monitoring agents

After ensuring these, you can start portainer-agent.

# docker swarm Leader节点执行
docker stack deploy -c portainer-agent.yml portainer-agent

After startup, you can see whether each node has started the portainer-agent service.

Then configure the address of the agent on the management page of portainer.

04swarm21

The port number here is fixed, and the ip address only needs to fill in the IP address of the leader node of the swarm cluster (if a non-leader node is filled in, even if it is added, the information cannot be obtained and an error will be reported) .

Click to connect, and you can manage the swarm cluster in portainer.

At the same time, click the Swarm menu on the left to see the current cluster status and node list.

It should be noted that a swarm cluster can only be managed by one portainer and cannot be managed by multiple portainers. If you want to change the managed portainer, you need to stop portainer-agent first and then restart it, and then configure portainer-agent in the new portainer.

For example: the original 10.0.12.13:9000 manages a swarm cluster. Now I want to use 10.0.12.14:9000 to manage A swarm cluster.

Then first stop the portainer-agent service of A swarm cluster.

# 在集群的Leader节点执行
docker stack rm pa
# 然后重新启动
docker stack deploy -c portainer-agen.yml pa

Then configure the address of A swarm cluster in 10.0.12.14:9000.

04swarm21

At this time, it is found that the A swram cluster cannot be managed in 10.0.12.13:9000

The Dashboard shows that the cluster status is Down.

Multiple swarm clusters are configured in portainer, how to switch clusters?

First click the Home menu on the left, and then a list of managed clusters will appear, select the cluster to be managed, and then the currently managed cluster will be displayed under the Home menu. Similarly, when you want to switch clusters, you must first click the Home menu. Just select the cluster.

If 10.0.12.13:9000 needs to manage the swarm cluster again, first delete the A swarm cluster configuration in the Endpoints menu in 10.0.12.14:9000, then restart the portainer-agent service of A swarm, and then in 10.0.12.13:9000 Endpoints menu to update the A swarm cluster (in fact, nothing needs to be changed, just click an update button), then click the Home menu of 10.0.12.13:9000 (the cluster may still be in the DOWN state at this time), and then click the cluster Discovery is already manageable.

When initializing the cluster (9 nodes) recently, it was found that when deploying the portainer-agent, there are always 3 nodes deployed in the pending state.

hint:

 host-mode port already in use on 1 node

But in fact, I went to the node to see that port 9001 was not occupied, and then searched for information. I only found relevant instructions on github , but it did not solve the problem. I thought it would be okay if the portanier-agent was not deployed, and then I went to deploy the application directly. , I found that as long as the application on those 3 nodes cannot be started, I went to the three nodes and found that the application container has been in the CREATED state. There is no way to remove the 3 nodes from the cluster, and then restart the 3 nodes The docker service, and then joined the cluster again , and found that everything is normal, and the protainer-agent is also normal (since the above protainer-agent.yml uses global, so when a new node joins, it will be automatically deployed).

When deploying a docker-swarm cluster recently, because the server uses the Kirin system and the firewall cannot be turned off, several problems were encountered:

1.docker-swarm service cannot connect to the external server address

2. The docker-swarm service cannot communicate with each other (expression: unknownHost, connection timeout)

The above problems are solved by opening the server port and restarting the docker service.

It should be noted that the use of Docker Swarm needs to open ports 2376, 2377, 2375, 7946, and 4789 of the server

Guess you like

Origin blog.csdn.net/kanyun123/article/details/117038098