Bobcares

Terraform Docker swarm | Installation steps

by | Aug 23, 2022

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.

Terraform Docker swarm

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.ymlin 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 ” inventorywithin 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
Apply complete! Resources:4 added, 0 changed, 0 destroyed.

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.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF