How to create docker swarm cluster in AWS EC2
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed docker machine.
https://cloudaffaire.com/docker-machine/
In this blog post, we will discuss how to create a docker swarm cluster in AWS. We will also deploy a docker stack in this docker swarm cluster.
Prerequisite for this demo:
- AWS EC2 Linux 2 instance with internet access
- Docker 1.2 and above
- Docker machine
- AWS CLI
- IAM user with programmatic access to create and manage EC2 instance
- Your AWS VPC, subnet, Availability zone and Security Group information
How to create docker swarm cluster in AWS EC2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
-------------------------------------------- ## Docker: How to create a docker swarm in AWS ##-------------------------------------------- ########################## ## Docker swarm setup ## ########################## # Prerequisites # 1. AWS EC2 Linux 2 instance # 2. docker 1.2 and above # 3. docker machine # 4. aws cli # 5. IAM user with programetic access to create and manage EC2 instance # 6. Your AWS VPC, Subnet, Security Groups and AZ info ## Setup AWS environment variables that will be used to create the docker swarm nodes export AWS_ACCESS_KEY_ID= export AWS_SECRET_ACCESS_KEY= export AWS_DEFAULT_REGION= export AWS_VPC_ID= export AWS_AZ= export AWS_VPC_SUBNET= ## Check if env variables are set env | grep AWS* ############################# ## Create a docker swarm ## ############################# ## Create the docker swarm manager node first. docker-machine create -d amazonec2 --amazonec2-vpc-id $AWS_VPC_ID --amazonec2-region $AWS_DEFAULT_REGION --amazonec2-zone $AWS_AZ --amazonec2-instance-type t2.micro --amazonec2-subnet-id $AWS_VPC_SUBNET --amazonec2-security-group docker-swarm docker-swarm-manager ## Create the two worker nodes docker-machine create -d amazonec2 --amazonec2-vpc-id $AWS_VPC_ID --amazonec2-region $AWS_DEFAULT_REGION --amazonec2-zone $AWS_AZ --amazonec2-instance-type t2.micro --amazonec2-subnet-id $AWS_VPC_SUBNET --amazonec2-security-group docker-swarm docker-swarm-node1 docker-machine create -d amazonec2 --amazonec2-vpc-id $AWS_VPC_ID --amazonec2-region $AWS_DEFAULT_REGION --amazonec2-zone $AWS_AZ --amazonec2-instance-type t2.micro --amazonec2-subnet-id $AWS_VPC_SUBNET --amazonec2-security-group docker-swarm docker-swarm-node2 ## Check all the nodes docker-machine ls |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
Get the internal IP address of the swarm manager. docker-machine ssh docker-swarm-manager ifconfig eth0 #172.31.28.158 in my case ## Point your docker client to the swarm manager. eval $(docker-machine env docker-swarm-manager) ## Initialize Swarm mode. docker swarm init --advertise-addr 172.31.28.158 # This is the internal IP of docker manager node. #copy the join command along with token #docker swarm join --token SWMTKN-1-1j3422kebg2c2xbvvxa27httpzolbosj4eyzdp0jifg5i9zayj-5zedcpcgmwry1k5u7hicketpm 172.31.28.158:2377 ## Modify the security group to allow the swarm communication aws ec2 describe-security-groups --filter "Name=group-name,Values=docker-swarm" ## From this command you should get all the details of the security group. ## Including the GroupId. Copy that information and run the following commands: SECURITY_GROUP_ID=sg-0705c2cf9df00dade # Replace with your security group id obtained from previous command aws ec2 authorize-security-group-ingress --group-id $SECURITY_GROUP_ID --protocol tcp --port 2377 --source-group $SECURITY_GROUP_ID aws ec2 authorize-security-group-ingress --group-id $SECURITY_GROUP_ID --protocol tcp --port 7946 --source-group $SECURITY_GROUP_ID aws ec2 authorize-security-group-ingress --group-id $SECURITY_GROUP_ID --protocol udp --port 7946 --source-group $SECURITY_GROUP_ID aws ec2 authorize-security-group-ingress --group-id $SECURITY_GROUP_ID --protocol tcp --port 4789 --source-group $SECURITY_GROUP_ID aws ec2 authorize-security-group-ingress --group-id $SECURITY_GROUP_ID --protocol udp --port 4789 --source-group $SECURITY_GROUP_ID ## Join the workers to the cluster. eval $(docker-machine env docker-swarm-node1) docker swarm join --token SWMTKN-1-1j3422kebg2c2xbvvxa27httpzolbosj4eyzdp0jifg5i9zayj-5zedcpcgmwry1k5u7hicketpm 172.31.28.158:2377 # This is the command copied from docker swarm init command's output eval $(docker-machine env docker-swarm-node2) docker swarm join --token SWMTKN-1-1j3422kebg2c2xbvvxa27httpzolbosj4eyzdp0jifg5i9zayj-5zedcpcgmwry1k5u7hicketpm 172.31.28.158:2377 # This is the command copied from docker swarm init command's output #################################### ## Docker Swarm Node Management ## #################################### ## List nodes in the swarm eval $(docker-machine env docker-swarm-manager) docker node ls ## Display detailed information on one or more nodes docker node inspect self ## List tasks running on one or more nodes, defaults to current node docker node ps docker-swarm-manager ## Update a node docker node update --availability pause docker-swarm-node1 docker node ls docker node update --availability active docker-swarm-node1 ## Promote one or more nodes to manager in the swarm docker node promote docker-swarm-node1 ## Demote one or more nodes from manager in the swarm docker node demote docker-swarm-manager eval $(docker-machine env docker-swarm-node1) docker node ls docker node promote docker-swarm-manager docker node demote docker-swarm-node1 eval $(docker-machine env docker-swarm-manager) docker node ls ######################## ## Stack Deployment ## ######################## ## Create a docker-compose.yml vi docker-compose.yml ------------------ version: '3' services: web: image: httpd ports: - "8081:80" deploy: replicas: 3 networks: - webnet visualizer: image: dockersamples/visualizer:stable ports: - "8082:8080" volumes: - "/var/run/docker.sock:/var/run/docker.sock" deploy: placement: constraints: [node.role == manager] networks: - webnet networks: webnet: ------------------ :wq ## Deploy a new stack or update an existing stack docker stack deploy -c docker-compose.yml mySTACK ## List stacks docker stack ls ## List the services in the stack docker stack services mySTACK ## List the tasks in the stack docker stack ps mySTACK ## Check the application http://ec2-13-127-148-1.ap-south-1.compute.amazonaws.com:8082 #point to public dns of manager node ## make sure port 8082 is open |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
############### ## Cleanup ## ############### ## Remove one or more stacks docker stack rm mySTACK ## Remove one or more nodes from the swarm docker node rm -f docker-swarm-node1 docker node rm -f docker-swarm-node2 ## Remove docker machines docker-machine rm docker-swarm-node1 docker-machine rm docker-swarm-node2 docker-machine rm docker-swarm-manager ## Swithc to current host eval $(docker-machine env -u) ## Remove the environment variables unset AWS_ACCESS_KEY_ID \ unset AWS_SECRET_ACCESS_KEY \ unset AWS_DEFAULT_REGION \ unset AWS_VPC_ID \ unset AWS_VPC_SUBNET \ unset AWS_AZ \ env | grep AWS* |
Hope you have enjoyed this article. With this, we are concluding our introductory series in Docker.
To get more details on docker, please refer below docker documentation