We aim to create a Highly Available apache-tomcat cluster with ansible, packer and terraform. This automation is supported currently for Ubuntu only.
There are 3 parts of this automation:
- Using Terraform creating a VPC with public and private subnets.
- Create an ami which has apache, tomcat & one sample application configured using Packer and Ansible. The AJP connection is used between apache & tomcat.
- Create ELB, security groups and autoscaling group with scaling policies using Terraform
- The Ansible playbook to install and configure Apache-Tomcat is included independently as well in case user does not want to use packer.
- I've used Amazon Linux box to setup the automation instance where I've configured Terraform, Ansible & Packer.
-
Use of tags reduces the human errors and simplify the code. Hence, in case VPC is not created using the given Terraform code then the subnets must be tagged as below:
Name:
tag_name
a\b\c
Example for tag named "public_subnet":
For subnet of Public AZ 1:
Name: public_subneta
For subnet of Public AZ 2:
Name: public_subnetb
For subnet of Public AZ 3:
Name: public_subnetc
The code given in 1-Create VPC is use to setup a new VPC. It creates:
- A VPC
- Three Public Subnets. They are created with tag
Name = public_subneta \ public_subnetb \ public_subnetc
- Three Private Subnets.
To setup Terraform:
- Download Terraform (linux) from: https://www.terraform.io/downloads.html
- Extract the zip and move the terraform binary in /usr/sbin directory
The following variables can be modified by the user. User need to edit 1-Create VPC/variables.tf file
access_key
: AWS access key for user's projectsecret_key
: AWS secret Key for User's projectaws_region
: Region to use Default is ap-southeast-2vpc_cidr
: CIDR for whole VPC, default is 192.168.0.0/22public_subnet_cidr_a\b\c
: CIDRs for the public subnets. The default values are given in the variables.tf file.private_subnet_cidr_a\b\c
: CIDRs for the private subnets. The default values are given in the variables.tf file.
Once all the info is correctly entered in variables.tf file user need to run below from 1-Create VPC:
- To initialize terraform:
terraform init
- To check the different resources being created:
terraform plan
- To create the resources:
terraform apply
Creating AMI part consist of Packer and Ansible which are integrated together. This part focuses on Packer configs.
The default AMI is created with Tag Name = apache-tomcat-0.1
To setup Packer:
- Download Packer (linux) from: https://www.packer.io/downloads.html
- Extract the zip and move the packer binary in /usr/sbin directory
The following variables can be modified by the user. User need to edit 2-Create AMI/vars.json file
aws_access_key
: AWS access key for user's projectaws_secret_key
: AWS secret Key for User's projectaws_region
: Region to use Default is ap-southeast-2aws_ami_image
: Ubuntu ami Id for the specific region.aws_instance_type
: The instance type with which the ami will be created by packer.image_version
: In case more images are created using the same automation this can be used to add versions. Default value is 0.1. AMI created is Name = apache-tomcat-0.1
Below is the example config available:
{
"aws_access_key": "",
"aws_secret_key": "",
"aws_region": "ap-southeast-2",
"aws_ami_image": "ami-cab258a8",
"aws_instance_type": "t2.small",
"image_version" : "0.1"
}
Go to the directory 2-Create AMI and run:
- packer build -var-file=vars.json apache-ami.json
To Integrate packer with Ansible:
- The ansible code is copied ino the playbook directory inside Create AMI directory
- The ansible provisioner is then used to integrate ansible with packer.
- Once a system is created with AMI the apache & tomcat services will be up and running.
- The ansible playbook is integrated with Packer. As well same playbook is provided separately "Independent apache-tomcat Configuration.
- The ansible version tested is 2.3.1 but playbook should also support 2.4
- The playbook does below:
- Install and configures apache
- Install and configure tomcat (version 8) and Create tomcat service file.
- Ensure that only AJP connector is used between Apache & Tomcat.
- Deploy a sample application "clusterjsp" in the end of automation.
The following variables can be modified by the user. User need to edit 2-Create AMI/playbook/variables.yml file
java_home
: The java home being used to create tomcat service file. (Default: java-1.8.0-openjdk-amd64 )tomcat_url
: The url to download the tomcat .tar.gz file. (Default: http://apache.mirror.digitalpacific.com.au/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz)
- All the instances where apache should be installed should have their IP's mentioned in "inventory.txt" file.
- Run below command from "Independent apache-tomcat Configuration":
- ansible-playbook main.yml
- The ansible logs will be created in /tmp/ansible.log file
- The ansible configurations are mentioned in "ansible.cfg" file. User can modify them as per need basis.
- Here we are using Terraform to create:
- Creates Security Groups
- Creates public key for user to use with launch configuration.
- Creates Elastic Load balancer
- Creates Launch Configuration & Autoscaling Group
- Creates policies for Autoscaling group for Increase & Decrease of number of instance as per CPU and Memory usage
- Creates CPU & Memory based Cloud Watch Metrics to be used with policy of autoscaling group.
- Info about Autoscaling Group Created:
- The Desired and Minimum number of instances are 3.
- The maximum number of instances is 5.
- One additional instance is created when the CPU or Memory threshold crosses 80% with cool down of 300 seconds.
- One instance is removed when the CPU or Memory threshold drops below 20%.
- In the end of terraform run, the ELB DNS is generated as output. That can be used to test the ASG created.
The following variables can be modified by the user. User need to edit 3- Create ASG/vars.tf file
access_key
: AWS access key for user's projectsecret_key
: AWS secret Key for User's projectaws_region
: Region to use Default is ap-southeast-2filter_ami
: The tag name for AMI being used. The default is apache-tomcat-0.1 (This is also the default tag created by packer)subnet_tag_name
: This is the name of Tag's Key being used. The default is Name.subnet_tag_value
: This is the tag's value being used. The default is public_subnet (This is also the default subnet name created by VPC code)instance_type
: The size of instance to be launched with autoscaling group.port_22_cidr
: The cidr for the Ip range which will be used for ssh to this instances. (E.g. 103.11.226.0/24)max_instance_asg
: The max limit for the instances in autoscaling group. (Default = 5)
For creation of key part:
- Enter the public key in key.pub file.
- If user does not want to create new key.pub then update the "key_name" part of "aws_launch_configuration" part in 3-Create ASG/create_autoscaling_group.tf file :
resource "aws_launch_configuration" "apache" {
key_name = "enter_your_key_name"
}
Once all the info is correctly entered in vars.tf file user need to run below from 2-Create ASG directory:
- To initialize terraform:
terraform init
- To check the different resources being created:
terraform plan
- To create the resources:
terraform apply
Take the output of ELB DNS created in the last terrafrom run, append clusterjsp to it and hit the url in brouwer: http://ELB_DNS_GENERATED/clusterjsp
example: http://apache-elb-993864931.ap-southeast-2.elb.amazonaws.com/clusterjsp/