lessandro-ugulino / k8s-nginx-ingress-wordpress

Automation to deploy K8s + Nginx Ingress and AWS infrastructure via Terraform and Ansible.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Index

Summary

This project was created to be 100% automated, which means, Terraform will create all AWS resources (VPC, Subnets, Route tables, IGW, NAT and EC2) and then Ansible will install, set Kubernetes up on the master and the EC2 nodes and deploy the Nginx Ingress, WordPress and Bananas apps as soon as Terraform finishes the deployment without the need to update any file, but before this make sure you've done all prereqs.

Observation

Terraform will create the MySQL RDS as well as export the endpoint to Ansible to be used on WordPress Deployment. Also, the RDS password for the user, wpmysql, will be created via Terraform as well as the password via SSM Parameter, secure string, and then as soon as Ansible starts the deployment, it'll grep this password and store it in a variable to be used on WordPress Deployment as well.

Therefore, sensitive data won't be exposed.

Diagrams

AWS

Diagram

Kubernetes

k8s

Prereq

These tools were installed locally.

⚠️ Make sure to check out the below information.

  1. Make sure the file in /group_vars/all/default.yml is updated according to your infrastructure.
  1. Update the file ansible.cfg, parameter private_key_file to your EC2 key pair in the Dev directory. This Key Pair will be used to access the EC2 by Ansible.
[defaults]
inventory=./inventory/hosts
host_key_checking=false
private_key_file= path_to_your_key_pair (eg. /myfolder/mykey.pem)
  1. Update key_pair_name variable on main.tf file in the Dev directory to your EC2 key pair.

Terraform

Terraform project

terraform

terraform init

Update the main.tf file in the Dev directory if you want to change any AWS resource name.

Access the Dev folder and run terraform init to initialize the project.

terraform_init

terraform plan

Access the Dev folder and run terraform plan to check out any issue.

terraform apply

Once it’s OK, run terraform apply

Testing

Wordpress Website

web

Banana website

banana

Output

Ansible Deployment ansible

ELB Ingress ingress

AWS Nginx Network Load Balancer elb

K8s svc

svc

K8s Pods pods

AWS RDS

rds

AWS SSM Parameter

ssm

Horizontal Pod Autoscaler

hpa

Running the below command in a couple of terminal windows, we can stress the environment and then test the HA.

while true; do curl http://my-website.ugulino.com -v; echo; done

As soon as the CPU will be consumed more than 50%, the pods will scale up.

hpa_increase

hpa_pods

As soon as the CPU will be consumed less than 50%, the pods will scale down.

hpa_decrease

Destroy

You can destroy your environment running the below command.

terraform destroy

About

Automation to deploy K8s + Nginx Ingress and AWS infrastructure via Terraform and Ansible.

License:MIT License


Languages

Language:Jinja 60.5%Language:HCL 39.5%