Hosting WordPress on GCP K8s Cluster With MySQL Database on AWS- Using Terraform

Vikas Verma
The Startup
Published in
4 min readSep 13, 2020

We are going to deploy a WordPress application on top of Kubernetes cluster using Kubernetes service of Google Cloud Platform (GCP) along with Load Balancer and for database, we will be creating a MySQL db instance using RDS of Amazon Web Services (AWS). All of these using Terraform.

So, these are the steps -

  1. Creating a VPC, subnet and firewall rule inside a project on GCP.
  2. Create a Kubernetes cluster, deploy WordPress on top of it with LoadBalancer service.
  3. Using Relational Database Service (RDS) of AWS and creating a MySQL db instance for connecting it to the WordPress site.

Every resource on GCP needs to be created inside a project, so first we should have a project. We can create or delete projects using the cloud shell as shown in the image. ( the project should be connected with billing account and also required APIs should be enabled)

google cloud shell

For connecting Terraform with your GCP account, you can create a key from your service account and download that json format key.

service account (GCP)

Now, let’s start writing Terraform code.

First we need the provider which is google, give the credential (the downloaded key of the service account) and also write the project in which we are going to create the resources.

Creating a VPC and subnet,

Adding firewall rule to our created vpc,

Now creating a Kubernetes cluster in our vpc,

We are now using Kubernetes provider and connect with the created cluster, creating a deployment using WordPress docker image .

Using the LoadBalancer service of Kubernetes, it will create an external Load Balancer using the Load balancing service provided by GCP.

Now, let’s move to AWS, (you should have configured aws cli with your credentials of the profile you are using)

Creating a security group to allow inbound or ingress to port number 3306 (default port on which MySQL work),

We are creating a MySQL db instance, publicly accessible so that WordPress can connect to it.

This is just to print the output at the end with all the things required by us.

So, time to run the code…

First we do terraform init to download all the providers used (google, terraform and aws)

terraform init

Now, terraform plan to see what resources will be created actually after apply

terraform plan

Finally, apply to create all the resources.

creating resources

We can go to GCP console and see all the resourecs are created there (VPC, kubernetes cluster, wordpress deployment, Load balancer)

GCP Console

Also, in AWS our MySQL database instance is created

AWS Console

Let’s connect to our WordPress site using the load balancer ip address, connect to the database and complete the setup.

WordPress site

So, after doing everything we can delete all the created resources with terraform destroy command,

destroy resources

This is how we can create a complete WordPress-MySQL architecture using two different cloud platforms.

Thank You for reading ! 😊

--

--

Vikas Verma
The Startup

Tech and Programming, MLOps, DevOps Assembly Lines, Hybrid Multi Cloud, Flutter and Ansible Automation