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:
- Creating 2 VPC and Peering them.
- MySQL Instance in Production Project.
- GKE Cluster with WordPress, PVC & LoadBalancer Service.
Configure Provider In Terraform
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 Provider.tf 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.
Creating 2 VPC and Peering them.
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.
MySQL Instance in Production Project
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.
GKE Cluster with WordPress, PVC & LoadBalancer Service
I divided it into three Steps
- Creating a GKE Cluster
- Updating the Kubeconfig file for Kubectl
- Installing WordPress with LoadBlancer
Creating a GKE Cluster
This Will Create a Kubernetes Cluster in Our Dev. VPC with 2 Initial Node.
Updating the Kubeconfig file for Kubectl
For This Command to work correctly, make sure that your GCloud SDK is Properly Configured.
Installing WordPress with LoadBlancer and PVC
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.
- Terraform init → performs different initialization steps to prepare a working directory for use.
- Terraform Plan → Detecting current infrastructure tests and calculating differences between known state and desired state
- Terraform Apply → To apply the changes required to reach the desired state of the configuration.
- Terraform Destroy → Infrastructure managed by Terraform will be destroyed.
Get The Complete Terraform Code: Here
Any Suggestions or Query. Ping Me