Day 08:Docker Swarm part 1 | RAFT Algorithm | Swarm Commands

"Hello, I'm Kiran Pawar, a passionate Cloud and Devops Engineer with a strong background in cloud automation, configuration, and deployment. My journey in the world of technology has been a thrilling adventure, where I've had the privilege to work with cutting-edge tools and practices.
🚀 As a DevOps Engineer:
I specialize in automating, configuring, and deploying instances in cloud environments and data centers. My expertise extends to DevOps, GitOps, CI/CD pipeline management, HashiCorp Terraform, and containerization. I'm proficient in AWS and Linux/Unix administration, ensuring robust infrastructure and application performance.
🔧 My Tech Stack:
Front-end skills: HTML, CSS, SCSS, Tailwind CSS, Bootstrap, React, Material-UI, JavaScript DevOps toolbox: GIT, OWASP,Nexus,Trivy, Github, Gitlab, Terraform, Ansible, Docker, Kubernetes, Helm, Jenkins, Prometheus, Grafana, Argo CD, AWS EKS.
🌐 My Cloud Expertise:
I have hands-on experience managing AWS services, including EC2, S3, EBS, VPC, ELB, RDS, IAM, Route53, and more.
🔒 Networking and Security:
My skills include managing networking concepts such as TCP/IP protocols, security policies, and subnet interfacing. I have a strong understanding of infrastructure and networking, covering topics like firewalls, IP addressing, DNS, and more.
💡 What Sets Me Apart:
I bring a positive attitude, a strong work ethic, and a collaborative spirit to every project. I'm a self-starter, a fast learner, and an effective team player with strong interpersonal skills. In addition to my DevOps skills, I've developed shell scripts (Bash) for automating tasks and have proficiency in Python scripting. My ability to communicate and manage projects, along with a track record of resolving client issues, adds value to every team I work with. If you're looking for a DevOps engineer who is also well-versed in front-end technologies, feel free to connect with me. Let's explore new possibilities and create exceptional technical solutions together!"
Welcome to the Docker Swarm Setup and Management guide! This guide will walk you through setting up a Docker Swarm cluster, deploying services, and managing the cluster with simple commands.
Introduction
Docker Swarm is a native clustering and orchestration tool for Docker containers. It allows you to manage a cluster of Docker engines as a single virtual system. This guide provides step-by-step instructions to set up and manage your Docker Swarm cluster.
Prerequisites
Docker installed on all nodes and add ubuntu user to docker group and reboot all instances
Launch EC2 Instances
Launch 6 EC2 instances (Ubuntu 20.04 or later).
Assign a Security Group that allows required ports.
Ensure all instances use the same VPC and Subnet for private communication.
⚡ Required Ports
| Port | Protocol | Purpose |
| 2377 | TCP | Swarm management |
| 7946 | TCP/UDP | Node communication |
| 4789 | UDP | Overlay network |
- Access to multiple instances (machines or VMs)
Setting Up Docker Swarm
Steps
Create 6 EC2 instances: 3 master and 3 worker nodes



Initialize Docker Swarm on the first node: This command initializes the first node as the
# Initialize Docker Swarm on the first node docker swarm init
Join worker nodes: Use the worker join token on each worker node..
# Join worker nodes docker swarm join --token <worker-token> <manager-ip>:<manager-port>


Join manager nodes: Use the manager join token on each additional manager node.
# Join manager nodes docker swarm join --token <manager-token> <manager-ip>:<manager-port>

joined rest 2 master nodes as manager
Verify the setup: Check the status of the Swarm and list all nodes to ensure they are correctly joined.
# Check Swarm status docker info # List nodes docker node ls

Check networks: View the networks available in the Swarm.
# Check networks docker network ls
Visualizing the Cluster
Use Swarm Visualizer from GitHub to visualize your cluster. Access the visualizer at http://<manager-ip>:8080.
https://github.com/dockersamples/docker-swarm-visualizer
docker run -it -d -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer
install visualizer on master server 1

Paste the master server 1 public IP with port 8080 into the browser.

Deploying Services
Steps
Deploy a sample service: Create a service with 3 replicas and publish it on port 8000.Commands
# Deploy a sample service docker service create --name nginx-service --replicas 3 --publish 8000:80 nginx

Scale the service up and down: Adjust the number of replicas for the service.Commands
# Scale the service up docker service scale nginx-service=6



Deploy service only on worker nodes: Ensure the service runs only on worker nodes.
# Deploy service only on worker nodes docker service create --name nginx-service --constraint node.role==worker --replicas 6 --publish 8000:80 nginx



Deploy a global service: Run the service on every node.
# Deploy a global service (runs on every node) docker service create --name monitor --publish 9100:9100 --mode global prom/node-exporter

Managing Services and Nodes
Steps
Check service tasks: List the tasks of a service.Commands
# Check service tasks docker service ps nginx-service

Put a node in maintenance mode: Drain the node to prevent it from running tasks.Commands
# Put a node in maintenance mode docker node update <node-id> --availability drain

Reactivate a node: Set the node back to active status.Commands
# Reactivate a node docker node update <node-id> --availability active
Conclusion
Docker Swarm makes it easy to manage a cluster of Docker containers. With these simple commands, you can set up, deploy, and manage services across multiple nodes. Happy clustering!
