Set up Multi-Nodes Kubernetes Cluster along with WordPress & MySQL on GCP Cloud

Project Requirements are as under

  1. Create 2 different projects one for dev & other for Prod environment
  2. Create 2 VPC’s in 2 different regions, one in Singapore region & other one in US region.
  3. Establish VPC peering between the 2 VPC’s created above
  4. Create 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 so as to access WordPress.
  7. Launch MySQL DB instance in Prod VPC (US region)
  8. Ensure that the LoadBalancer able to access WordPress.
  9. WordPress in turn able to connect to MySQL instance created above.
  10. Create IAM users & assign roles & test the privileges.

Let’s start creating separate projects i.e. one for Dev & other for Prod as follows

Devproject
mydevproject created

Create another project for Prod.

prodproject
myprodproject created

We can verify the same using gcloud command as under .

PS : My First Project is was another project created earlier.

all project lists

After creating the projects above we need to associate it with billing so that we can use it.

Enable Billing for the project
Billing enabled for myprodproject

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

Now let us try to create VPC in each of the above project

We’ll create VPC in Singapore region for mydevproject & another VPC within US region for myproproject.

vpc in mydevproject
vpc set up in Singapore region

VPC created in Singapore region

VPC set up in Singapore region

Now we need to create a firewall rule for above VPC

firewall rule for vpcdev-sg
allow all traffic
firewall rule created

Similarly we’ll create a VPC in US region & set up firewall rule for it.

VPC set up in myproproject
IP range selection
VPC created in US region under myproproject

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

firewall rule set up for vpcprod-us
allow all incoming traffic
firewall rule created

VPC Peering as a concept using with O.S. in different VPC can communicate with each other using pvt IP’s through GCP owned high speed otpical fiber network. It reduces cost, latency & improves performance to a great extent.

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

Launching instance in S’pore region

instance launch in mydevproject

we’ll select CentOS 7 image

selecting CentOS 7 image
allow all http traffic

select VPC from within the networking section as below & hit create to launch instance.

selecting VPC vpcdev-sg
instance launched in vpcdev-sg

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

gcloud command to verify instance launch & confirm IP address

Now , lets launch one instance in myprodproject

select compute engine service
selecting the region
selecting CentOS 7 image

select VPC from within the networking section as below & hit create to launch instance.

selecting VPC from networking section

finally instance launched

instance launched in vpcprod-us

We can verify the same using gclod command as under

As can be seen , Public & Private IPs are same as shown in WebUI.

gcloud command to verify instance launch & IP address

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

ssh to devinstance
ssh to prodinstance
ssh to prodinstance.

now let’s try to ping from devinstance to prodinstance using ping command

We can see that ping doesn’t happen from devinstance to prodinstance.

This can be resolved using VPC peering

ping from dev to prod instance

For VPC peering we need to create peering connection as under

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

VPC peering from devvpc to prodvpc
creating peering connection from vpcdev-sg to vpcprod-us

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

vpc peering inactive
vpc peering from vpcprod-us to vpcdev-sg

After doing this VPC peering is active at both the sides.

and we can see that the devinstance is able to ping prodinstance now

ping successful from devinstance to prodinstance

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.

Now we need to set up Kubernetes cluster in mydevproject

Before this we need to enable Kubernetes KPI engine

Kubernetes Engine API enabling

Once Kubernetes API Engine is enabled we can create cluster as “create cluster” button will be enabled as shown below

We can select appropriate region & node locations as below

configure default pool

select N1 machine type from within General Purpose Machine Family

select vpcdev-sg in Netwroking
kubernetes cluster created

We can connect to above kubernetes cluster as below

coonnet to kubernetes cluster
services list

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

Launch a POD with WordPress image

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

identify the node in which POD is running

Kubectl deployment monitors each POD & in case if it goes down it will launch another POD with similar configuration immediately. Thus it manages POD lifecyle as well as rolling updates.

kubectl launches another POD in case existing POD gets deleted/goes down

We now need to expose above

expose to port 80

We can verify that the services are running & can get the IP of LoadBalancer as well

running services

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

Next step is to set up MySQL instance in myprodproject as shown in following steps

select MySQL database engine

select MySQL database engine

Specify DB instance name, password & region details.

specify db instance name & password
sqldb instance created

We need to edit the networking settings for above MySQL db instance

Allow all Traffic
edit network settings to allow all traffic
MySQL Instance launched in myprodproject

We can connect to this instance using Active Cloud Shell as under

connected to MySQL DB Instance

Now let’s create our own database

creating student database

Now, last step is to connect WordPress instance to this MySQL database instance. As per the project requirement WordPress will serve as a front end & MySQL will be used to store WordPress data which we store in the website.

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

Public IP of WordPress
WordPress Launched using PublicIP

Next step is to configure MySQL database (created above in myprodproject) with WordPress

configure MySQL DB

We can specify MySQL DB instance details (IP of DB instance we can get from the following screen in GCP)

Public IP of MySQL DB instance
Specify MySQL DB instance details

Finally we are ready with WordPress installation

Ready for WordPress installation

Hurray !! We have set up installed WordPress instance successfully and now we can create our pages..

WordPress Dashboard

We are now ready to create our own blogs..

Blog Creation

We can confirm that the blog is published..

blog published

Same data we can verify in MySQL DB as below

WP tables in Students database

Below Table indicates that the website data has been stored in WordPress table in the configured database.

verification of blog data inserted in table

Now we’ll try to create IAM users & see how to play around with roles.

Using IAM service to create user & roles

We’ll need gmail id of the user so as to set up roles.

Viewer role created for user

Once we have created the account as above the user will receive mail from the sender as below

gmail received by new user
role created for new user
role created for user
user receives mail as above

User is unable to create MySQL DB as he has only viewer role

Unable to create DataBase

Finally, we can destroy both the projects, VM instances, SQL DB instance, VPC networks.

Same can be verified as below

project list

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

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

Cloud Professional experienced in BFSI ,Telecom, Insurance domain with fortune 500 clients spread across USA, Europe, Canada, Australia and India.