Google Kubernetes Engine with WordPress and MySQL Database Using Terraform

It was a task given by my training instructor from LinuxWorld Informatics Pvt. Ltd. This task has to be done directly from Google cloud Console, but I tried to create the complete infrastacture using only Terraform.

Task Overview :

1. Create multiple projects, namely developer and production

2. Create VPC network for both the projects

3. Create a link between both the VPC networks using VPC Peering

4. Create a Kubernetes Cluster in the developer project and launch any web application with the Load balancer

5. Create a SQL server in the production project and create a database

6. Connect the SQL database to the web application launched in the Kubernetes cluster

I Divided this Task into three parts:

  1. Creating 2 VPC and Peering them.
  2. MySQL Instance in Production Project.
  3. GKE Cluster with WordPress, PVC & LoadBalancer Service.
  • GCloud SDK Configured (Here)
  • Kubectl (Here)

For Controlling GCP from Terraform, we Need a Credential file That can be Downloaded From IAM Service > Service Account. From the service account key page in the Cloud Console, choose an existing account, or create a new one. Next, download the JSON key file.

For Complete Code, Check my Github Repo: Here

In file I Wrote 2 Providers of Google because I have to Work on 2 Different Project at a time. For that, I Used Alias option in Terraform.

Here, I Made 2 VPC one in Dev. And another in Production Project with Firewall rules and subnet. For Working in Production Project, I used the Provider argument to specify to use the Production project instant of Dev. Project which the Default One.

This will Connect both VPC networks so that workloads in different VPC networks can communicate internally regardless of whether they belong to the same project or the same organization.

Note: Each side of a peering association is set up independently. Peering will be active only when the configuration from both sides matches. Either side can choose to delete the peering association at any time.

This Code will Create a MySQL instance with Database in the Production Project. In Authorized Networks, I allowed it for the Public to access it, But we can modify it as per our needs.

This SQL Database will be Added to WordPress During Deployment.

I divided it into three Steps

  1. Creating a GKE Cluster
  2. Updating the Kubeconfig file for Kubectl
  3. Installing WordPress with LoadBlancer

This Will Create a Kubernetes Cluster in Our Dev. VPC with 2 Initial Node.

For This Command to work correctly, make sure that your GCloud SDK is Properly Configured.

PersistentVolumeClaim (PVC) for WordPress

WordPress Deployment with Google SQL as Database.

⭐ For Complete Code, Check my Github Repo: Here

LoadBalancer Servicer for WordPress

Done! Finally, We Have our Code Ready with all Requirements.

Now Save All files in One Folder And goto that dir. Using Terminal and Execute the Code.

  1. Terraform init → performs different initialization steps to prepare a working directory for use.
  2. Terraform Plan → Detecting current infrastructure tests and calculating differences between known state and desired state
  3. Terraform Apply → To apply the changes required to reach the desired state of the configuration.
  4. Terraform Destroy → Infrastructure managed by Terraform will be destroyed.

Thank You!!

Get The Complete Terraform Code: Here

LinkedIn: Here

Any Suggestions or Query. Ping Me

Tech. Explorer