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:
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.
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:
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.
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.
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.