Docker-swarm is still alive. Creating a cluster.
During the hype around Kubernetes, docker-swarm technology is considered dead. However, in my opinion, it is still very convenient, and also simple, to use it to combine several physical or virtual machines into a cluster, on top of which you can start up an encrypted VPN network and distribute the load across several machines.
Create a Docker-swarm cluster.
This assumes that docker is already installed on all three machines. Now they need to be combined into a cluster. To do this, you need to create this cluster on one machine, and then add the remaining machines to it.
The machines in my example will have hostnames
lena. I connect to the first server via ssh and initialize the cluster with the following command:
$ docker swarm init
The following command is needed to expand the cluster. It returns the text of the command, which will contain the IP of the current machine and a token for connecting other machines in manager mode to the current cluster;
$ docker swarm join-token manager
We copy the text of the command that it returned and execute it on the remaining two machines. In my case, it will be something like:
$ docker swarm join --token SWMTKN-1-6bf4fib37rx0ya50gzknpq62irxu2sdo326itt60on324fazc8pgq31v-3g2nm7olt8iqu95f361r1hwa2p 188.8.131.52:2377
I enter this command into the consoles of two other machines, after which they all become connected and can perform coordinated actions.
The following command will help you see that 3 nodes are in the cluster and which of them is the leader at the moment.
$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION mmуfzg7sqd47wdk8jxh4hhhk8i8 clara Ready Active Leader 20.10.7 yhk2zpifyazzrq4t3if730wmy6x lena Ready Active Reachable 20.10.7 dqlf92hm0k3he2fdn9dqsguv38c * markus Ready Active Reachable 20.10.3
Now, being on any of these machines, we can execute commands for [creating and deploying services] (https://docs.docker.com/engine/reference/commandline/service/), which will probably be a separate article. But an example of how to deploy nginx + php-fpm in such a cluster can be found in my github.
Example of running services on top of SWARM
To start services, you need to prepare a docker-compose file, but with some features that describe the features of placement in a cluster. All services described in this file are launched within the same "stack" - an entity that combines services, networks, config in one context. Launch features are specified in the
deploy section. In addition, the network linking the services in this example is encrypted.
version: '3.7' services: php: image: registry.gitlab.com/floor12/images:basic80 networks: -example deploy: replicas: 3 mode:replicated nginx: image: nginx configs: source: nginx_config target: /etc/nginx/conf.d/default.conf ports: - '8888:80' networks: -example deploy: replicas: 1 mode:replicated configs: nginx_config: file: ./app.conf networks: example: driver:overlay driver_opts: encrypted: "true"
To start services on this file, you must run the command:
docker stack deploy --compose-file swarm-php.yml example
example is the name of the context for these services.
To complete the job:
docker stack rm example
Thus, knowing just a few commands, you can quickly horizontally scale your services to several machines, at the same time for free and without kubernetes.