#docker #swarm #awsdevops #aws #nginx
Container Orchestration and Docker Swarm
Project Roadmap:
• Run the Two-tier Application with the Docker Swarm
• Scale up the docker containers using Docker Swarm
• Set up multiple nodes to avoid a single point of failure
Run the Two-tier Application with the Docker Swarm
Create an ec2 named docker, ubuntu AMI, inbound rule all traffic. Connect with Mobaxterm
sudo apt update && sudo apt install docker.io -y
sudo systemctl status docker see status active
To avoid writing sudo in order to run Docker commands every time, the ubuntu user can be added to the docker user group. Run the command:
sudo usermod -aG docker ubuntu
cat /etc/group at bottom, see ubuntu
exit
R
docker system info | grep -i swarm to check docker swarm (inactive by default)
docker swarm init
docker system info | grep -i swarm
vim stack.yaml
version: "3"
services:
backend-service:
image: azizul2go/two-tier-backend-image:latest
deploy:
replicas: 1
frontend:
image: azizul2go/two-tier-frontend-image:v2
ports:
8081:80
Run application with docker swarm:
Run the backend and frontend services with the stack.yaml file
docker stack deploy two-tier -c stack.yaml
docker stack ls (see running docker swarm services)
docker service ls (see docker all services as well)
pub IP of docker ec2 : 8081
High availability enabling by docker swarm
docker ps
Now delete the containers
docker kill cont id 1 cont id 2
docker ps container is empty now
But if you run see the containers after a minute, you'll see that the containers are back up.
docker ps
pub IP :8081
You can view that the Two-tier the Application is running even after the containers were killed. Both the backend and frontend containers came back up.
Scale up Application using docker swarm
docker service scale two-tier_backend-service=3
Now, scale up frontend application
docker service scale two-tier_frontend=3
Now see that there are total six Docker containers running with three backend and three frontend containers
docker ps
Kill 2 frontend and 2 backend containers
Docker kill backend_1 backend_2 frontend_1 frontend_2
View the Application to verify that the application faces no downtime. Even if you kill 2 frontend and 2 backend containers the 3rd backend and frontend containers will run the application without zero downtime. You can see the Docker containers again and see that deleted Docker containers are all back up and running in a few seconds.
docker ps
Distribute the Application in Multiple Nodes:
Run the following command to get the command to run a worker node
docker swarm join-token worker
Launch an ec2 named docker-worker-1, ubuntu, all traffic. Write user data
#!/bin/bash
apt update
apt install -y docker.io
..token from upper command
pub IP:8081
From the docker-master server terminal you can view the Nodes running
docker node ls 6 Docker containers running
docker ps
Remove two-tier docker stack. That will kill all the running Docker Swarm containers.
docker stack rm two-tier
Distribute application in multiple nodes
vim stack.yaml
Run the new stack.yaml file with Docker Swarm
docker stack deploy two-tier -c stack.yaml
docker ps
docker stack deploy two-tier -c stack.yaml
docker ps
Now, if you Go to Public_IPv4_address:8081 of the docker-worker-1 or docker server you will see that both the fronend and the backend are running even though they are in the separate node in separate servers
Run the following command to get the command to run a worker node
docker swarm join-token worker
create ec2 named docker-worker-2, AMI ubuntu, All traffic
Write the following script in User data to run the server as a worker node from the get-go.
Pub IP of worker-2:8081 You can view Two-tier the Application. in action from the worker node 2. Both the backend and frontend work.
#!/bin/bash
apt update
apt install -y docker.io
token from upper command
Demonstrate the Docker Swarm Behaviour when Nodes are Down
Select the docker-worker-1 instance and click on Stop instance
docker node ls
See that the one of the nodes is Down. That was the docker-worker-1 node.
docker service ls
The node docker-worker-1 was running the frontend server replica. Docker Swarm ran another replica of the frontend server back up. So it must be running in the docker-worker-2 node. We'll verify it in a moment.
Public_IPv4_address:8081 of the docker-worker-2 server.
You can still view Two-tier the Application in action from the worker node 2. Both the backend and frontend work.
Connect to the docker-swarm-2 Node:
Verify that Docker Swarm ran the frontend image replica in the docker-worker-2
Connect this ec2 with Mobaxterm
Promote a Node to Manager:
sudo docker ps
See that the frontend container is running in our docker-worker-2 node.
You can promote the docker nodes to Swarm manager. Run
docker node ls
docker node promote node ID
See that the node's manager status is Reachable
Thank You