Traditional Linux replaces Docker microservice operation and maintenance-swarm, Portainer

Traditional Linux replaces Docker microservice operation and maintenance-swarm, Portainer

Swarm is a cluster management tool officially provided by Docker. Its main function is to abstract several Docker hosts into a whole, and manage various Docker resources on these Docker hosts through a single entry.

Portainer is a lightweight Docker graphical management solution. It is easy to deploy Portainer and Portainer Agent to manage Swarm clusters! You can deploy Portainer as a service directly in a Docker cluster. Please note that this method will automatically deploy a single instance of Portainer Server and deploy Portainer Agent as a global service on each node in the cluster.

Prerequisites

  • Docker must be installed on the server first, because of various versions, check the official documentation for the installation method .
  • The mirroring service uses Alibaba Cloud's container mirroring service, which needs to be activated. The automatic construction at that time will be served by this.

Initialize a cluster node and set the management node as the local IP

docker swarm init --advertise-addr 192.168.0.150 # IP IP  ip -a 
 

Verify whether the swarm is initialized successfully, and check whether the node is mounted.

docker node ls
ID                            HOSTNAME                  STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
xbf82bf03t6r7mrvxzxmkv9mm *   iZbp12d0p2o2at8bmvb033Z   Ready               Active              Leader              19.03.5
 

Create a private network

docker network create --driver overlay www_net
 

The purpose of creating an overlay www_net network is to create a condition for these dockers to access each other within the network.

For example, there are three micro-applications in the scene nginx, web, and redis. The requirement is that nginx and web need intranet access to each other, but nginx and redis do not need to be accessed, and web it and redis need to be accessed, you can configure nginx: a_net network, web: a_net, b_net two networks, redis: b_net The internet. In this way, the demand can be met.

Step 3: Create Portainer

You can also refer to the official deployment document to view other deployments

$ curl -L https://downloads.portainer.io/portainer-agent-stack.yml -o portainer-agent-stack.yml
 

The above command will download an official swarm deployment yml file and modify it to the following configuration:

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
    networks:
      - agent_network
    deploy:
      mode: global
      placement:
        constraints: [node.platform.os == linux]

  portainer:
    image: portainer/portainer
    command: -H tcp://tasks.agent:9001 --tlsskipverify
    ports:
      - "9000:9000"
      - "8000:8000" #  web 8000 
    volumes:
      - portainer_data:/data
    networks:
      - agent_network
      - www_net
    deploy:
      mode: replicated
      replicas: 1
      placement:
        constraints: [node.role == manager]

networks:
  agent_network:
    driver: overlay
    attachable: true
  www_net:
    external: true

volumes:
  portainer_data:
 

It should be noted that the above configuration portainer there are two networks, one is www_neta external: truemean to use externally created www_net, if he will not set the current stack to create a network name is not set automatically _www_net.

The configuration file is ready to deploy on the command line now:

$ docker stack deploy --compose-file=portainer-agent-stack.yml portainer
 

Verify that the deployment is successful:

docker service ls
ID                  NAME                  MODE                REPLICAS            IMAGE                        PORTS
td1uhbhmsb5f        portainer_agent       global              1/1                 portainer/agent:latest
aeoqcexzg7hu        portainer_portainer   replicated          1/1                 portainer/portainer:latest   *:9000->9000/tcp
 

Replace domestic mirror

If REPLICAS 0/1when the note also deploy please wait. If it is too slow, it is because you have not set up a domestic mirror. You can use the accelerator by modifying the daemon configuration file/etc/docker/daemon.json

sudo mkdir -p/etc/docker
sudo tee/etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://48udk7jr.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
 

The 48udk7jr.mirror.aliyuncs.com above uses the exclusive Alibaba Cloud mirroring, and the mirroring service can be activated in the Alibaba Cloud control panel.

Login portainer

When the deployment is complete, you can visit http://ip:9000 to access, if it is some cloud server, you must first determine whether port 9000 is open

When logging in for the first time, he will let you set a password, this is the interface after logging in:

Note that the subsequent automatic deployment requires the use of Alibaba Cloud's container mirroring service, and the use of private images, so it is necessary to register a registry with Portainer. Otherwise, the authorization is unsuccessful and the mirror pull fails. note:

  • registry.cn-hangzhou.aliyuncs.com is based on which URL is used behind the access credential, and mine is the region of Hangzhou.
  • The account password is obtained from the access credentials of Alibaba Cloud's container mirroring service.

The image image deployed later will match the prefix registry.cn-hangzhou.aliyuncs.com. He will automatically use this registered authorization.

The above configuration is equivalent to our command line deployment stack is the same:

  1. sudo docker login --username= registry.cn-hangzhou.aliyuncs.com
  2. docker stack deploy -c xxx.yml web --with-registry-auth The effect of the middle and back --with-register-auth is a principle.

sample graph:

At this point, the basic management environment is over.