Docker swarm is used to cluster docker containers. It helps in scaling containers easily. It is a clustering and scheduling tool for docker containers.
Creating services in docker swarm
docker service create <image>
Example creating a service that will ping google
docker service create alpine ping www.google.com
list all running services in docker host docker service update <service_name> --replicas <number_of_services>
Example
docker service objective_lalande --replicas 2
Create a docker swarm cluster
Creating a 4 node docker swarm cluster (where each node is in a diff VMs) Switch manager node in docker
docker node update --role manager <node_name>
Get token to make other nodes join the swarm at run time
docker swarm join-token manager
To make a node leave the swarm
docker swarm leave -f
Docker swarm uses a overlay network for communication purposes.
There are two types of overlay network used:
Ingress (manages the control and data traffic related to swarm services[container comm]) bridge network[docker_gwbridge] (manages the node to node communication)
User defined overlay networks can be created but there are few conditions before creating user defined networks:
TCP Port 2377, TCP AND UDP Port 7946, UDP Port 4789 should be open for comms docker swarm must be initialised on the node or join it to a existing swarm. The above conditions must satisfy before a overlay network can be created.
Create an overlay network
docker network create -d overlay <network_name>
Adding service to custom network
docker service create --name <service_name> --network <network_name> -e <env_variables_if_any> <image_name>
Example
docker service create --name postgres --network custom_overlay_network -e POSTGRES_PASSWORD=mypassword postgres
Creating a service with custom network exposed to outer world
docker service create --name drupal --network custom_overlay_network -p 80:80 drupal
-p <published_port>:<container_port> published_port is the port where the swarm makes the service available. Ingress routing mess algorithm in docker swarm
Find the published port for a service
docker service inspect --format="{{json .Endpoint.Spec.Ports}}" <service_name>