Swarm is native clustering for the docker. When docker engine runs in swarm mode, to manage cluster state manager node implement the Raft Consensus Algorithm.
In this article we will setup one manager node and 3 worker nodes which are separate virtual machines running in AWS.
Pre-Requisites: > Docker installed and running on all instances.
> Static IP for all instances
> Network connectivity between all instances
> Ports 2377 (network communications),7946(swarm node communication),4789(overlay network) should be open.
First go to our manager node.Now to check whether swarm is active or not
#docker system info | grep Swarm
Now to initialize # docker swarm init
or else we can also give
#docker swarm init --adverstise-addr ip-of-managernode
To list the nodes
#docker node ls
We can see only one manager node is there which is the leader.(Note: We can also run multiple manager nodes.But only one leader will be there)
To add a worker node, from manager node give the following command
#docker swarm join-token worker
This will give us a command to use in worker node to join with manager node
(Note: In similar way we can use [#docker swarm join-token manager] to make the manager to join the leader manager node)
Now go to a worker node and copy paste the command that we got from manager node.
Similarly go to the other worker node and do the same .Now from worker give
#docker node ls
We can see it is not listing any nodes since we can list nodes only from manager node.Try the same from manager node and we can see 2 worker nodes and one manager node running.
Generating New Token:
Suppose we want to generate a new token for the worker nodes to join, go to manager node and give following command
#docker swarm join-token --rotate worker
From now the old token won't work. Now go to third machine connect to manger node from there by using new command that we got.
Now go to manager node and give #docker node ls
It will list manager node and 3 worker nodes
Swarm Cluster Management:
Before getting into management we want to know the status of swarm cluster.
Availability: Active - it means scheduler can assign tasks to node
Pause - scheduler can't assign new tasks to node.But existing tasks will run.
Drain - scheduler won't assign new tasks to node. Existing tasks move to other nodes
Manager status: No value - Worker node does not participate in swarm management
Leader - it act as the primary manager
Reachable - Node is a manager participating in swarm management
Unreachable - Node is a manager that is not able to communicate with other managers
To drain a worker node:
#docker node update --availability drain node-name
Again we can make it active by
#docker node update --availability drain node-name
Promote/Demote node:
While promoting a node it moves from worker state to manager state.For that
#docker node promote node-name (note:Replace node-name with node-name we want to promote)
Now we can see that manager status of the node as Reachable.That means it is a manager node but not a leader.When the present leader gets down this will becomes leader)
Similarly to demote it
#docker node demote node-name
Inspect details about a node:
#docker node inspect node-name --pretty
Removing a particular node from cluster:
Suppose we want to remove node ip-10.0.1.219 from cluster, first go to that node and give command
#docker swarm leave
Now go to manager and give command
#docker node rm node-name
Note: If we again want to join this node to cluster, we want to create a join-token and use that.
DOCKER SWARM SERVICES
Service can be defined as tasks to execute on worker nodes.This is how we interact with the swarm.
While creating a service we want to specify which container image to use, which ports to use , which command to execute and so on.
Swarm service modes: Swarm mainly supports 2 modes - replicated mode and global mode
In replica mode we can pass number of replica of service and swarm will maintain that count.
If we dont mention anything by default service will be in replicated mode.
In global mode every time a new node is available scheduler will place tasks on these new node.
Running service in replicated mode:
By default swarm service runs in replicated mode.To start a service go to manager node
#docker service create -p 8080:80 --name=mywebserver nginx
This will start a service with name mywebserver using nginx container image and it maps port 80 of nginx to 8080 of cluster.
Now to list services use
#docker service ls
To see running process
#docker service ps mywebserver
Now try to access from any of our worker or manager node.We can see nginx home page.
By default docker service will create one replica on each node.If we want to create a replicated service with more than one replica use the following command
#docker service create --name=mywebserver2 --replicas 5 nginx
Now to check number of tasks running
#docker service ps mywebserver2
Here we can see task running on different nodes.
Running a service in global mode:
#docker service create --name=globalweb --mode=global nginx
On #docker service ps globalweb we can see that 3 tasks are running as there are 3 nodes.If we add a new node the scheduler will add a task to it.This is how global mode works
Scaling up replicated service:
To scale up a replicated service use the following command
#docker service scale mywebserver2=10
This will scale our service mywebserver2 from 5 to 10.
similarly we can also scale down.
Inspect details about a service:
To inspect details about a service
#docker service inspect mywebserver2 --pretty
Deleting a service:
To delete a service
#docker service rm mywebserver2
In this article we will setup one manager node and 3 worker nodes which are separate virtual machines running in AWS.
Pre-Requisites: > Docker installed and running on all instances.
> Static IP for all instances
> Network connectivity between all instances
> Ports 2377 (network communications),7946(swarm node communication),4789(overlay network) should be open.
First go to our manager node.Now to check whether swarm is active or not
#docker system info | grep Swarm
Now to initialize # docker swarm init
or else we can also give
#docker swarm init --adverstise-addr ip-of-managernode
To list the nodes
#docker node ls
We can see only one manager node is there which is the leader.(Note: We can also run multiple manager nodes.But only one leader will be there)
To add a worker node, from manager node give the following command
#docker swarm join-token worker
This will give us a command to use in worker node to join with manager node
(Note: In similar way we can use [#docker swarm join-token manager] to make the manager to join the leader manager node)
Now go to a worker node and copy paste the command that we got from manager node.
#docker node ls
We can see it is not listing any nodes since we can list nodes only from manager node.Try the same from manager node and we can see 2 worker nodes and one manager node running.
Generating New Token:
Suppose we want to generate a new token for the worker nodes to join, go to manager node and give following command
#docker swarm join-token --rotate worker
From now the old token won't work. Now go to third machine connect to manger node from there by using new command that we got.
Now go to manager node and give #docker node ls
It will list manager node and 3 worker nodes
Swarm Cluster Management:
Before getting into management we want to know the status of swarm cluster.
Availability: Active - it means scheduler can assign tasks to node
Pause - scheduler can't assign new tasks to node.But existing tasks will run.
Drain - scheduler won't assign new tasks to node. Existing tasks move to other nodes
Manager status: No value - Worker node does not participate in swarm management
Leader - it act as the primary manager
Reachable - Node is a manager participating in swarm management
Unreachable - Node is a manager that is not able to communicate with other managers
To drain a worker node:
#docker node update --availability drain node-name
#docker node update --availability drain node-name
Promote/Demote node:
While promoting a node it moves from worker state to manager state.For that
#docker node promote node-name (note:Replace node-name with node-name we want to promote)
Now we can see that manager status of the node as Reachable.That means it is a manager node but not a leader.When the present leader gets down this will becomes leader)
Similarly to demote it
#docker node demote node-name
Inspect details about a node:
#docker node inspect node-name --pretty
Removing a particular node from cluster:
Suppose we want to remove node ip-10.0.1.219 from cluster, first go to that node and give command
#docker swarm leave
Now go to manager and give command
#docker node rm node-name
Note: If we again want to join this node to cluster, we want to create a join-token and use that.
DOCKER SWARM SERVICES
Service can be defined as tasks to execute on worker nodes.This is how we interact with the swarm.
While creating a service we want to specify which container image to use, which ports to use , which command to execute and so on.
Swarm service modes: Swarm mainly supports 2 modes - replicated mode and global mode
In replica mode we can pass number of replica of service and swarm will maintain that count.
If we dont mention anything by default service will be in replicated mode.
In global mode every time a new node is available scheduler will place tasks on these new node.
Running service in replicated mode:
By default swarm service runs in replicated mode.To start a service go to manager node
#docker service create -p 8080:80 --name=mywebserver nginx
This will start a service with name mywebserver using nginx container image and it maps port 80 of nginx to 8080 of cluster.
Now to list services use
#docker service ls
To see running process
#docker service ps mywebserver
Now try to access from any of our worker or manager node.We can see nginx home page.
By default docker service will create one replica on each node.If we want to create a replicated service with more than one replica use the following command
#docker service create --name=mywebserver2 --replicas 5 nginx
Now to check number of tasks running
#docker service ps mywebserver2
Here we can see task running on different nodes.
Running a service in global mode:
#docker service create --name=globalweb --mode=global nginx
On #docker service ps globalweb we can see that 3 tasks are running as there are 3 nodes.If we add a new node the scheduler will add a task to it.This is how global mode works
Scaling up replicated service:
To scale up a replicated service use the following command
#docker service scale mywebserver2=10
This will scale our service mywebserver2 from 5 to 10.
similarly we can also scale down.
Inspect details about a service:
To inspect details about a service
#docker service inspect mywebserver2 --pretty
To delete a service
#docker service rm mywebserver2