Setting up a Multi-Node Kubernetes Cluster using Google Kubernetes Engine and deploying a WordPress site
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:
- Create 2 different projects one for dev & other for Prod environment
- Create 2 VPC’s in 2 different regions, one in the Singapore region & other one in the US region.
- Establish VPC peering between the 2 VPC’s created above
- Create a multi-node Kubernetes cluster in Singapore VPC.
- Launch WordPress instance using docker image in above cluster’
- Create a LoadBalancer & expose it to port 80 to access WordPress.
- Launch MySQL DB instance in Prod VPC (US region)
- Ensure that the LoadBalancer can access WordPress.
- 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.
- 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
After creating both the projects above, we need to associate it with billing( if not already associated) so that we can use it.
Also, we’ll need 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.
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.
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.
Similarly, we’ll create another VPC in the US region for myprodproject and set up a Firewall rule for it.
Now, let’s set up a firewall rule for the above VPC:
3. Now let us try to launch instances in each of the above VPCs
Launching instance in the Singapore region
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
Now, let's launch one more instance in myprodproject
We can verify the same using the below GCloud command in our Cloud shell:
gcloud compute instances list --project myprodproject-290319
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.
Now let’s try to ping from devinstance to prodinstance using the ping command.
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.
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.
Follow the same procedure of VPC peering for myprodproject as well.
And we can see that the devinstance can ping prodinstance now.
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)
We can connect to the above Kubernetes cluster as 👇
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:
Install and Set Up kubectl
The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl…
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
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:
If you have not set the network to allow all traffic, edit the instance and do it:
We can connect to this instance as the root user using Active Cloud Shell as under:
mysql -h 188.8.131.52 -u root -p
Enter password: redhat123
Now let’s create our own database:
create database students;
To see all the databases:
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:
9. The next step is to configure the MySQL database (created above in myprodproject) with WordPress. We can specify MySQL DB instance details.
Enter the database you created and login with the root user and password, database host can be copied from the above screen-shot.
Hola from the WordPress dashboard 👋
Draft created and can be published
Same data we can verify in MySQL DB as below:
Finally, we can destroy both the projects. I have shown for one project. Follow the same process for the other one too.
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. 🔗