Deploy the Wordpress application on Kubernetes and AWS using terraform

Deploy the Wordpress application on Kubernetes and AWS using terraform

Problem Statement :

Deploy the Wordpress application on Kubernetes and AWS using terraform including the following steps;

1. Write an Infrastructure as code using terraform, which automatically deploy the Wordpress application

2. On AWS, use RDS service for the relational database for Wordpress application.

3. Deploy the Wordpress as a container either on top of Minikube or EKS or Fargate service on AWS

4. The Wordpress application should be accessible from the public world if deployed on AWS or through workstation if deployed on Minikube.

What is Aws RDS ?

Amazon Relational Database Service (or Amazon RDS) is a distributed relational database service by Amazon Web Services (AWS). It is a web service running “in the cloud” designed to simplify the setup, operation, and scaling of a relational database for use in applications.Administration processes like patching the database software, backing up databases and enabling point-in-time recovery are managed automatically. Scaling storage and compute resources can be performed by a single API call as AWS does not offer an ssh connection to RDS instances.

No alt text provided for this image

What is Wordpress ?

WordPress (WP, WordPress.org) is a free and open-source content management system (CMS) written in PHP and paired with a MySQL or MariaDB database. Features include a plugin architecture and a template system, referred to within WordPress as Themes. WordPress was originally created as a blog-publishing system but has evolved to support other types of web content including more traditional mailing lists and forums, media galleries, membership sites, learning management systems (LMS) and online stores. WordPress is used by more than 60 million websites, including 33.6% of the top 10 million websites as of April 2019, WordPress is one of the most popular content management system solutions in use. WordPress has also been used for other application domains such as pervasive display systems (PDS).

No alt text provided for this image


What is Kubernetes ?

Kubernetes (commonly stylized as K8s) is an open-source container-orchestration system for automating computer application deployment, scaling, and management.

It was originally designed by Google and is now maintained by the Cloud Native Computing Foundation. It aims to provide a “platform for automating deployment, scaling, and operations of application containers across clusters of hosts”. It works with a range of container tools, including Docker.

Many cloud services offer a Kubernetes-based platform or infrastructure as a service (PaaS or IaaS) on which Kubernetes can be deployed as a platform-providing service.

No alt text provided for this image


What is Mysql ?

MySQL (/?ma???s?kju???l/ “My S-Q-L”)is an open-source relational database management system (RDBMS). Its name is a combination of “My”, the name of co-founder Michael Widenius’s daughter, and “SQL”, the abbreviation for Structured Query Language. A relational database organizes data into one or more data tables in which data types may be related to each other; these relations help structure the data. SQL is a language programmers use to create, modify and extract data from the relational database, as well as control user access to the database. In addition to relational databases and SQL, an RDBMS like MySQL works with an operating system to implement a relational database in a computer’s storage system, manages users, allows for network access and facilitates testing database integrity and creation of backups.

MySQL is free and open-source software under the terms of the GNU General Public License, and is also available under a variety of proprietary licenses. MySQL was owned and sponsored by the Swedish company MySQL AB, which was bought by Sun Microsystems (now Oracle Corporation). In 2010, when Oracle acquired Sun, Widenius forked the open-source MySQL project to create MariaDB.

No alt text provided for this image


What is Terraform ?

Terraform is an open-source infrastructure as code software tool created by HashiCorp. Users define and provision data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON.

No alt text provided for this image

What is Minikube ?

Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a Virtual Machine (VM) on your laptop for users looking to try out Kubernetes or develop with it day-to-day.

Pre-requisites :

  • Install minikube and kubectl 
  • Download terraform CLI also in your system.
  • Also install AWS CLI in your system to login to AWS console using command line.
  • Install virtual box

Note : To install virtual box you need virtualization to be enabled from bios, hyper-v stopped in your machine.

Note : Download and install all these as this code can't be used without these.

Solution :

For solving this problem statement, I have written infrastructure as a code to perform this task.

Step1: Declaring provider details and Creating security groups

## Declaring  Provider
provider "aws" {
  region = "ap-south-1"
 // your profile for AWS here.
 profile = "task2"   
}




//Security Group for RDS
	resource "aws_security_group" "sg_rds" {
		name        = "sg_rds"
		description = "Sec Group for RDS"
	


		ingress {
			from_port   = 3306
			to_port     = 3306
			protocol    = "tcp"
			cidr_blocks = ["0.0.0.0/0"]
		}
	}
	


	


	

Step 2:. On AWS, use RDS service for the relational database for Wordpress application.

	


	//Launching the RDS MySQL Database
	resource "aws_db_instance" "wp_rds" {
		engine            = "mysql"
		engine_version    = "5.7.21"
		identifier        = "mysql-db"
		username          = "divya"
		password          = "divya1234"
		instance_class    = "db.t2.micro"
		storage_type      = "gp2"
		allocated_storage = 15
		publicly_accessible = true
		vpc_security_group_ids = [aws_security_group.sg_rds.id ]
		port = 3306
	


		name = "wprdsdb"
		skip_final_snapshot = true
		final_snapshot_identifier = "fnlSnpt"
	


		auto_minor_version_upgrade = false
	  
		depends_on = [aws_security_group.sg_rds]
	

}

Step3: . Deploy the Wordpress as a container either on top of Minikube or EKS or Fargate service on AWS

	//Connecting to Kubernetes in Local System with minikube
	provider "kubernetes" {
	}
	


	


	


	//Launching PVC for WordPress
	resource "kubernetes_persistent_volume_claim" "pvc_wp" {
		depends_on = [aws_db_instance.wp_rds]
		metadata {
			name   = "pvc-wp"
			labels = {
			env     = "Production"
			Country = "India" 
			}
		}
	


		wait_until_bound = false
		spec {
			access_modes = ["ReadWriteOnce"]
			resources {
				requests = {
				storage = "5Gi"
				}
			}
		}
	}
	


	


	


	//Launching WordPress Deployment
	resource "kubernetes_deployment" "wp" {
		metadata {
			name   = "wp"
			labels = {
			env     = "Prod"
			Country = "Ind" 
			}
		}
		depends_on = [kubernetes_persistent_volume_claim.pvc_wp]
		spec {
			replicas = 1
			selector {
				match_labels = {
					pod     = "wp"
					env     = "Prod"
					Country = "Ind" 
	        
				}
			}
	


			template {
				metadata {
					labels = {
						pod     = "wp"
						env     = "Prod"
						Country = "Ind"  
					}
				}
	


				spec {
					volume {
						name = "wp-vol"
						persistent_volume_claim { 
							claim_name = kubernetes_persistent_volume_claim.pvc_wp.metadata.0.name
						}
					}
	


					container {
						image = "wordpress"
						name  = "wp-app"
	


						env {
							name  = "WORDPRESS_DB_HOST"
							value = aws_db_instance.wp_rds.address
						}
						env {
							name  = "WORDPRESS_DB_USER"
							value = "divya"
						}
						env {
							name  = "WORDPRESS_DB_PASSWORD"
							value = "divya1234"
						}
						env{
							name  = "WORDPRESS_DB_NAME"
							value = "wprdsdb"
						}
						env{
							name  = "WORDPRESS_TABLE_PREFIX"
							value = "wp_"
						}
	


						volume_mount {
							name       = "wp-vol"
							mount_path = "/var/www/html/"
						}
	


						port {
							container_port = 80
						}
					}
				}
			}
		}
	}
	


	


	


	//Launching LoadBalancer for WordPress Pods
	resource "kubernetes_service" "wpLb" {
		metadata {
			name   = "wp-svc"
			labels = {
				env     = "Prod"
				Country = "Ind" 
			}
		}  
	


		depends_on = [kubernetes_deployment.wp]
	


		spec {
			type     = "NodePort"
			selector = {
			pod = "wp"
			}
			port {
				port = 80
			}
		}
	}
	


	


	//Output LoadBalancer IP
	output "final_output" {
		value = kubernetes_service.wpLb.spec.0.port.0.node_port
	

}

Note : Before executing the code, we need to initialize the code as terraform needs it's modules for it's code to work. So this initialization is compulsory. Use terraform init command to download these modules.

We validate the code we have written to reduce run-time errors. For validation, we use terraform validate command.

Now, to deploy the database and our application we have to execute our terraform code. For this, we use the command terraform apply.

No alt text provided for this image
No alt text provided for this image


After all the resources created successfully, you will see that on AWS cloud one RDS database comes up.

No alt text provided for this image

Now in Cmd run Kubectl get all ,to see all services running

No alt text provided for this image

Now, we need minikube IP to connect to the wordpress app and use it's services. For getting minikube IP, we can use kubectl describe pod/pod id or kubectl describe all command.

No alt text provided for this image

After we get the IP from here, we search for the IP on the port on which we have exposed wordpress app. For ex :- IP:Port

Provide the information related to your database host, password etc. here and submit it to proceed. you will get database host endpoint here :

AWS => login => service => RDS => Databases => Select database => Endpoint

This endpoint is your database host endpoint.

Now on this installation page as you can see, you have to provide your login credentials for present and future login for the app. After providing these all, press install wordpress button.

Your app is deployed successfully, press login to use your app.

Now, login using the credentials you have provided for you account.

No alt text provided for this image

Great !! Our wordpress app is ready to use now. All your data will be stored in AWS RDS database.

Destroying the whole setup we have done :

If some misbehaving happens or some other problem happens, you can destroy the whole setup just by using one command "terraform destroy". It can destroy each and every thing created by using terraform code only.

No alt text provided for this image

Note : All the terraform commands must be used only in the directory where the terraform code is located. Otherwise, they will not work or give some error.

All suggestions are welcomed. If you need to ask anything regarding this article you can dm me

Github code link :- https://github.com/drlraj2805/Hctask6.git

Thanks For investing ur time !

要查看或添加评论,请登录

Divya Raj Lavti的更多文章

社区洞察

其他会员也浏览了