Setting up a Multi-Node Kubernetes Cluster using Google Kubernetes Engine and deploying a WordPress site

Shirsha Datta
10 min readSep 24, 2020
Kubernetes | Docker | Google Kubernetes Engine

Let's start by understanding what do we want:

  • Use GKE(Google Kubernetes Service) to deploy our WordPress site.
  • For the WordPress site, we will be using MySQL as our database. This will be provided by GCP.

Now what we should do to achieve the same:

  1. Create 2 different projects one for dev & other for Prod environment
  2. Create 2 VPC’s in 2 different regions, one in the Singapore region & other one in the US region.
  3. Establish VPC peering between the 2 VPC’s created above
  4. Create a multi-node Kubernetes cluster in Singapore VPC.
  5. Launch WordPress instance using docker image in above cluster’
  6. Create a LoadBalancer & expose it to port 80 to access WordPress.
  7. Launch MySQL DB instance in Prod VPC (US region)
  8. Ensure that the LoadBalancer can access WordPress.
  9. WordPress in turn able to connect to the MySQL instance created above.

As we have understood our requirement let us get on the hands-on part. Follow the steps to achieve a fully functional WordPress-site.

  1. Let's start by creating 2 different project one for the dev(mydevproject) and the other for the production(myprodproject) environment.

Assign them under no organization and an auto-generated ID.

We can also check from the GCloud shell provided by using the below command:

gcloud projects list
GCloud shell output

After creating both the projects above, we need to associate it with billing( if not already associated) so that we can use it.

Projects associated with billing

Also, we’ll need to enable Compute Engine API for myprodproject

Click on ENABLE to enable Compute Engine API for myprodproject

2. Now let us try to create VPC in each of the above projects.

🎯So what is VPC?

Google Cloud Virtual Private Cloud (VPC) provides networking functionality to Compute Engine virtual machine (VM) instances, Google Kubernetes Engine (GKE) containers, and the App Engine flexible environment. VPC provides networking for your cloud-based services that are global, scalable, and flexible.

To know more about VPC. Since we got to know let us create and implement our concepts:

First, let's start by creating a VPC in the Singapore region for mydevproject.

VPC for mydevproject

Things to keep in mind:

> Assign a name to the VPC and a description for future understanding

> Set the region as Singapore(asia-southeast1)

> Add an IP address range and keep this in mind as it will be required later.

Finally, VPC was created in the Singapore region.

Now we need to create a firewall rule for the above VPC.

Assign a name to the firewall rule and a description for future understanding

Add target All instances in the network and filter by IP ranges.

0.0.0.0/0 — has a significance as it allows all traffic.

Firewall created for mydevproject

Similarly, we’ll create another VPC in the US region for myprodproject and set up a Firewall rule for it.

VPC for myprodproject
Set the region as the US and provide an IP range for the instance to connect
VPC created for myprodproject

Now, let’s set up a firewall rule for the above VPC:

Give a relevant name and add a description
Add target as All instances in the network and filter by IP ranges and CREATE
Firewall created for myprodproject

3. Now let us try to launch instances in each of the above VPCs

Launching instance in the Singapore region

Specify the region and select any zone in that region. Also, select the machine series as N1
We will use CentOS 7 as our boot image
Allow HTTP traffic
Add the firewall rule created for mydevproject before.

We can verify the same using the GCloud command as given below. As can be seen, Public & Private IPs are the same as shown in WebUI.

gcloud compute instances list --project mydevproject-290319
Verified that the instance is created

Now, let's launch one more instance in myprodproject

Set region as the US and select a zone of your choice in the region | Select series as N1 type
Use CentOS 7 as the boot image
Add firewall group created for myprodproject

We can verify the same using the below GCloud command in our Cloud shell:

gcloud compute instances list --project myprodproject-290319
The instance created for myprodproject

We can install the GCloud SDK in our systems and also check using this same command:

We can test connectivity to both the above VM instances using GCP provided Open Browser as below:

Do the same for both the projects. If it opens then there is no issue in connectivity.

To check for connectivity

Now let’s try to ping from devinstance to prodinstance using the ping command.

Ping unsuccessful

4. We can see that ping doesn’t happen from devinstance to prodinstance. This can be resolved using VPC peering.

So you might be wondering 🧐

What is VPC peering?

➡Google Cloud VPC Network Peering allows internal IP address connectivity across two Virtual Private Cloud (VPC) networks regardless of whether they belong to the same project or the same organization.

VPC Network Peering enables you to connect VPC networks so that workloads in different VPC networks can communicate internally. Traffic stays within Google’s network and doesn’t traverse the public internet.

For VPC peering we need to create a peering connection as under

We should note the project id & VPC network name before creating a VPC peering connection.

VPC network -> VPC network peering -> Create Connection
Add the project ID and VPC network name

The status still shows inactive as we need to allow peering from the other side as well i.e. from vpcprod-us to vpcdev-sg.

Status inactive

Follow the same procedure of VPC peering for myprodproject as well.

Peering for myprodproject
After doing this VPC peering is active at both sides.

And we can see that the devinstance can ping prodinstance now.

Ping successful after VPC peering

Thus, VPC peering helps to communicate instances in different VPC’s using private IP’s instead of public IP’s. Thus it’s more secure, high speed & reduces latency.

5. Now we need to set up the Kubernetes cluster in mydevproject

Before this, we need to enable the Kubernetes Engine (GKE)

Kubernetes engine -> Clusters
Kubernetes Engine API enabling ->enabled->create a cluster
Give a name to the cluster and set region as Singapore
Select the default given in the Static version
No of nodes per region as 1 — i.e 3 nodes in total
Select machine-type as N1 standard
Make it a public cluster and add the network
It shows creating and after a few minutes the cluster is ready

We can connect to the above Kubernetes cluster as 👇

Connect to the Cluster

To configure the GCloud SDK :

Run the gcloud init command to configure the GCloud SDK

Using the cmd:

gcloud container cluster get-credentials kubecluster-sg --region asia-southeast1 --project mydevproject-290319

Using the Dashboard:

We can check all the services present using the below command. But we need kubectl to be installed and configured for it. To do so check the below link:

Now run this command.

kubectl.exe get services

6. The next step is to launch a pod & deploy WordPress in it.

kubectl.exe create deployment mywp --image=wordpress

We can verify in which node the POD is running as under

Kubectl deployment monitors each POD & in case if it goes down it will launch another POD with a similar configuration immediately. Thus it manages the POD lifecycle.

We can check the same using:

kubectl.exe delete pod *insert pod name*

We now need to expose the above pod created so that it is visible to the outside world. We can verify that the services are running & can get the IP of LoadBalancer as well

It takes some time for the services to be exposed | The service is exposed at port 30679

Once WordPress is setup we need to create a LoadBalancer service & expose it.

7. The next step is to set up a MySQL instance in myprodproject as shown below:

SQL -> Instances -> Create instance
Select the MySQL
Give instance name and set root password | As well as select the database version
After some time the SQL instance will be ready

If you have not set the network to allow all traffic, edit the instance and do it:

Edited SQL instance

We can connect to this instance as the root user using Active Cloud Shell as under:

mysql -h 35.231.56.224 -u root -p
Enter password: redhat123
Connecting to the SQL Instance

Now let’s create our own database:

create database students;

To see all the databases:

show databases;
Creating a database and showing all the databases present

8. Now, the last step is to connect the WordPress instance to this MySQL database instance.

Using the public IP of WordPress instance we can launch it as below:

WordPress setup

9. The next step is to configure the MySQL database (created above in myprodproject) with WordPress. We can specify MySQL DB instance details.

IP of DB instance we can get from the following screen in GCP

Enter the database you created and login with the root user and password, database host can be copied from the above screen-shot.

Copy this password somewhere as it will be required later.
Paste the copied password here

Hola from the WordPress dashboard 👋

Draft created and can be published

Click on Publish at the top right corner to publish your article
The article is published and can be accessed

Same data we can verify in MySQL DB as below:

Use the student DB created and show tables of that DB

Finally, we can destroy both the projects. I have shown for one project. Follow the same process for the other one too.

Destroy project

The same can be verified as below:

gcloud projects list

Thus using Google Kubernetes Engine, we deployed a multi-node cluster & successfully launched the WordPress application inside a container using the docker image.

Also, set up MySQL DB as a back-end to store WordPress blog data.

Thank you, folks. 🤗

To know more connect with me on Linkedin. 🔗

--

--

Shirsha Datta

I am a DevOps Enthusiast and recently taken to Cloud Computing. Learning Flutter App Development currently. In my free time I engage in competitive coding.