How to use the command 'docker swarm' (with examples)
Docker Swarm is a container orchestration tool that allows you to create a swarm of Docker nodes and deploy services across them. It provides high availability and scalability for your applications through a decentralized architecture.
Use case 1: Initialize a swarm cluster
Code:
docker swarm init
Motivation: Initializing a swarm cluster is the first step towards deploying services and managing a swarm. It creates a single-node swarm with the current node as the manager. This command is useful when you want to start using Docker Swarm for the first time.
Explanation:
docker swarm init
initializes a swarm cluster with the current node as the manager.
Example output:
Swarm initialized: current node (manager) IP:Port
Use case 2: Display the token to join a manager or a worker
Code:
docker swarm join-token worker|manager
Motivation: When you want to add new nodes to the swarm cluster, you need a token that allows the nodes to join either as workers or managers. This command is used to display the respective tokens for joining a worker or a manager to the cluster.
Explanation:
docker swarm join-token worker
displays the command to join a worker node to the swarm cluster.docker swarm join-token manager
displays the command to join a manager node to the swarm cluster.
Example output:
docker swarm join --token token manager_node_url:2377
Use case 3: Join a new node to the cluster
Code:
docker swarm join --token token manager_node_url:2377
Motivation: When you have the join token and the URL of the manager node, you can use this command to add a new worker or manager node to the swarm cluster. This allows you to scale your cluster and distribute the workload across multiple nodes.
Explanation:
docker swarm join
is used to join a new node to the swarm cluster.--token token
specifies the join token obtained from the manager node.manager_node_url:2377
is the IP address or hostname of the manager node along with the swarm listening port.
Example output:
This node joined a swarm as a worker.
Use case 4: Remove a worker from the swarm
Code:
docker swarm leave
Motivation: In certain scenarios, you may need to remove a worker node from the swarm cluster. This command allows you to gracefully leave the swarm without disrupting the availability of the services running on other nodes.
Explanation:
docker swarm leave
is executed inside the worker node to gracefully leave the swarm cluster.
Example output:
Node left the swarm.
Use case 5: Display the current CA certificate in PEM format
Code:
docker swarm ca
Motivation: The Certificate Authority (CA) certificate is used to authenticate and secure communications between nodes in the swarm. This command is useful for retrieving the current CA certificate in PEM format.
Explanation:
docker swarm ca
displays the current CA certificate.
Example output:
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
Use case 6: Rotate the current CA certificate and display the new certificate
Code:
docker swarm ca --rotate
Motivation: Rotating the CA certificate helps to ensure that the swarm’s communications remain secure over time. This command allows you to rotate the current CA certificate and verify the new certificate by displaying it.
Explanation:
docker swarm ca --rotate
rotates the current CA certificate and displays the new certificate.
Example output:
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
Use case 7: Change the valid period for node certificates
Code:
docker swarm update --cert-expiry hourshminutesmsecondss
Motivation: By default, node certificates in the swarm have an expiry time of 90 days. This command enables you to change the valid period for node certificates, providing flexibility in managing certificate lifetimes.
Explanation:
docker swarm update --cert-expiry hourshminutesmsecondss
changes the valid period for node certificates. Thehourshminutesmsecondss
argument specifies the desired period in hours, minutes, seconds format.
Example output:
Updated the cert expiry of the swarm.
Conclusion:
In this article, we explored various use cases of the docker swarm
command. We learned how to initialize a swarm cluster, display join tokens, add and remove nodes from the cluster, manage CA certificates, and change the valid period for node certificates. By understanding and using these commands, you can effectively manage and scale your Docker swarm clusters.