Let’s have a closer look at how to deploy Terraform Docker swarm Cluster and Ansible by our Support team as a part of our Docker hosting support service. We Bobcares respond to all your queries, no matter how minor.
Deploy Terraform Docker swarm using Terraform & Ansible
A node in a swarm cluster of any machine with a docker engine installed has the capability of hosting the containers or services, we often call applications as services when the docker engine is running under swarm mode. This is also described as the Docker node.
A Docker node can be a physical machine or even with one or more virtual machines running on a physical host or on cloud server. It is suggested to develop docker nodes across multiple physical machines to provide availability and reliability for the applications that is been running on the hosts.
The manager node executes orchestration and cluster management functions that require to maintain the desired state of the farm. If there are various manager nodes in a swarm cluster, then the nodes have the ability to elect one leader among the process to conduct orchestration which that implements a leader election strategy.
Now we’ll check on how to deploy our Terraform docker swarm cluster step-by-step procedure.
Step-1 :
Initially install the terraform and set up an AWS account for working forward.
Step-2 :
Create a directory “swarm-deploy “. create files named variable.tf, security-groups.tf, main.tf and output.tf.
Additionally, add the following code in a variable.tf file:
variable "aws_region" {
description = "AWS region on which we will setup the swarm cluster"
default = "eu-west-1"
}variable "ami" {
description = "Amazon Linux AMI"
default = "ami-04d10c7d"
}variable "instance_type" {
description = "Instance type"
default = "t2.micro"
}variable "key_path" {
description = "SSH Public Key path"
default = "/path-to-keyfile/docker-key.pem"
}variable "key_name" {
description = "Desired name of Keypair..."
default = "docker-key"
}variable "bootstrap_path" {
description = "Script to install Docker Engine"
default = "install_docker_machine_compose.sh"
}
Now In the “security-groups.tf” file, add the given below code:
resource "aws_security_group" "sgswarm" {
name = "sgswarm"
tags {
Name = "sgswarm"
}# Allow all inbound
ingress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}egress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}# Enable ICMP
ingress {
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
}
In “main.tf” file add the following:
# Specify the provider and access details
provider "aws" {
access_key = "your-aws-access-key"
secret_key = "your-aws-secret-access-key"
region = "${var.aws_region}"
}resource "aws_instance" "master" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
user_data = "${file("${var.bootstrap_path}")}"
vpc_security_group_ids = ["${aws_security_group.sgswarm.id}"]tags {
Name = "master"
}
}resource "aws_instance" "worker1" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
user_data = "${file("${var.bootstrap_path}")}"
vpc_security_group_ids = ["${aws_security_group.sgswarm.id}"]tags {
Name = "worker 1"
}
}resource "aws_instance" "worker2" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
user_data = "${file("${var.bootstrap_path}")}"
vpc_security_group_ids = ["${aws_security_group.sgswarm.id}"]tags {
Name = "worker 2"
}
}
Finally, to “output.tf “file add the below code:
output "master_public_ip" {
value = ["${aws_instance.master.public_ip}"]
}output "worker1_public_ip" {
value = ["${aws_instance.worker1.public_ip}"]
}output "worker2_public_ip" {
value = ["${aws_instance.worker2.public_ip}"]
}
Step-3 :
With help of a shell script, we can install docker. You just need to create a shell script named install_docker_machine_compose.sh. This script will execute in the provisioning time.
#!/bin/bash
export LC_ALL=C
sudo apt-get update -y
#sudo apt-get upgrade -y### install python-minimal
sudo apt-get install python-minimal -y# install docker-engine
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
echo "Docker installed..."
sudo usermod -aG docker ${whoami}
sudo systemctl enable docker
sudo systemctl start dockerecho "########################################"
echo "########################################"echo "##################### install docker-compose ########################"
sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
echo "docker-compose installed..."echo "########################################"
echo "########################################"echo "#################### install docker-machine #########################"
curl -L https://github.com/docker/machine/releases/download/v0.12.2/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine
chmod +x /tmp/docker-machine
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine
echo "docker-machine installed..."
Step-4 :
We now need to Install Ansible, you can install them using the code:
$ sudo apt-add-repository ppa:ansible/ansible
Press ENTER to accept the PPA addition.
$ sudo apt-get update
$ sudo apt-get install ansible
Step-5 :
There are many techniques for setting up a swarm cluster. Can create using any virtualized environment like Hyper-V, a virtual box. The number of Host running in the cluster will be highly restricted to the server’s CPU and memory capacity.
Traditionally on-premise environments are set up using multiple physical nodes. The alternative way of setting up a swarm environment is by using hosted environments like Azure or AWS. We’ll create an ansible script for creating a swarm cluster by a manager node and two worker nodes.
To proceed with this first create a file named ” playbook.yml ” in the swarm-deploy directory and add the following:
- name: Init Swarm Master
hosts: masters
become: true
gather_facts: False
remote_user: ubuntu
tasks:
- name: Swarm Init
command: sudo usermod -aG docker {{remote_user}}
command: docker swarm init --advertise-addr {{ inventory_hostname }}- name: Get Worker Token
command: docker swarm join-token worker -q
register: worker_token- name: Show Worker Token
debug: var=worker_token.stdout- name: Master Token
command: docker swarm join-token manager -q
register: master_token- name: Show Master Token
debug: var=master_token.stdout- name: Join Swarm Cluster
hosts: workers
become: true
remote_user: ubuntu
gather_facts: False
vars:
token: "{{ hostvars[groups['masters'][0]]['worker_token']['stdout'] }}"
master: "{{ hostvars[groups['masters'][0]]['inventory_hostname'] }}"
tasks:
- name: Join Swarm Cluster as a Worker
command: sudo usermod -aG docker {{remote_user}}
command: sudo docker swarm join --token {{ token }} {{ master }}:2377
register: worker- name: Show Results
debug: var=worker.stdout- name: Show Errors
debug: var=worker.stderr
Create a new directory ” inventory ” within that create a file named ” hosts “. Change the public IP and key-file-path which you will get after running the terraform apply command.
[masters]52.51.138.1 ansible_user=ubuntu ansible_private_key_file=/path-to-your-keyfile/docker-key.pem[workers]34.240.19.111 ansible_user=ubuntu ansible_private_key_file=/path-to-your-keyfile/docker-key.pem
52.208.83.236 ansible_user=ubuntu ansible_private_key_file=/path-to-your-keyfile/docker-key.p
Step-6 :
All the configuration files and scripts are been successfully updated. Further, run the following commands to deploy three instances by using terraform and to create a swarm cluster using ansible in those instances.
$ terraform init
$ terraform plan
By this, the Terraform has successfully initialized.
$ terraform apply
Outputs:
master public_ip = [ IP ]
worker_public_ip = [ IP ]
worker2_public_ip = [ IP ]
$ ansible-playbook -i inventory/hosts playbook.yml
Our swarm cluster is perfectly ready. Simultaneously let’s check the cluster using ssh in the manager node.
$ ssh -i your_key_file.pem ubuntu@manager_public_ip
$ sudo docker node ls
That’s all! by this sudo docker node ls
Command will show the status and availability as ready and active respectively with the manager status as a leader by leader election strategy.
Conclusion:
The Terraform Docker swarm environment includes one or more manager nodes. To deploy an application on the Docker Swarm cluster we submit a request in the form of service to a manager node for performing the action.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server
24/7 so that it remains lightning fast and secure.
0 Comments