Docker 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 unified portal.

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

A requirement

  • Docker must be installed on the server first, because of various versions, so the installation method check the official document.
  • The image service uses the container image service of Ali Cloud, which needs to be enabled. Automated builds will then use this service.

Initialize a cluster node and set the management node to the local IP address

Docker swarm init --advertise-addr 192.168.0.150# This IP address is your local IP address, which can be viewed by IP -A
Copy the code

Check whether swarm is successfully initialized and whether the node is mounted.

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

Create a private network

docker network create --driver overlay www_net
Copy the code

The purpose of creating an overlay WWW_NET network is to create conditions for these Dockers to access each other on the Intranet.

Such as scene nginx, Web, Redis three micro applications. Nginx and The Web need to access each other, but nginx and Redis do not need to access, and the Web and redis need to access, you can configure nginx: a_net network, Web: a_net, b_NET two networks, redis: b_net network. This will meet the requirements.

Step 3: Create the Portainer

You can also refer to the official deployment documentation for other deployments

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

Swarm yML swarm yML swarm yML swarm 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
    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" Use only the web management interface. 8000 can be deleted.
    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:
Copy the code

Www_net external: true means that the external network is created using www_net. If the external network is not set, it will create the current stack network. If the external network name is not set, it will automatically name _www_net.

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

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

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
Copy the code

Replace domestic mirror

Wait if REPLICAS 0/1 indicates that it is still deployed. If it’s too slow, it’s because you don’t have 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
Copy the code

The above 48 udk7jr.mirror.aliyuncs.com using the exclusive ali cloud image, can open the mirror in the ali cloud control panel.

Log in portainer

After the deployment is complete, you can visit http://ip:9000. In the case of some cloud servers, check whether 9000 ports are open

The first time you log in, he asks you to set your password. This is what happens when you log in:

Note that the automatic deployment will need to use ali Cloud’s container image service and use a private image, so register a Registries for The Portainer. Otherwise, authorization fails and the image fails to be pulled. Note:

  • Registry.cn-hangzhou.aliyuncs.com according to which URL is used behind the access certificate, mine is the region of Hangzhou.
  • The account password is obtained from the access certificate of ali Cloud’s container mirroring service.

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

The configuration above is the same as if we deployed stack on the command line:

  1. Sudo docker login --username= username registry.cn-hangzhou.aliyuncs.com
  2. docker stack deploy -c xxx.yml web --with-registry-authThe with-register-auth effect is a principle.

Figure:

This is the end of the basic management environment setup.