Deploying WordPress with Kubernetes-Cluster on GCP.

Ankit
7 min readSep 8, 2020

Hello readers,in this article we are going to launch a fully configured WordPress website which will be launched within a kubernetes cluster having one master and two slave nodes.

For this, we are using GCP as our cloud provider.We will use various resources provided by GCP like Compute-Engine, VPC, VPC-Peering, LoadBalancer, SQL-instance and Kubernetes-Engine.At the lowest level, resources are the fundamental components that make up all Google Cloud services.

GCP i.e. Google Cloud Platform is a Public cloud computing service offered by google.It offers various services on demand.its one of top three cloud provider by market share globally.

Google Compute Engine: Compute Engine lets you create and run virtual machines on Google infrastructure.

VPC: A Virtual Private Cloud (VPC) network is a virtual version of a physical network.It’s a service provided by cloud providers that lets you create your own private network/data-center on cloud.It provides networking for your cloud-based services.

VPC-Peering: VPC Network Peering allows internal IP address connectivity across two Virtual Private Cloud (VPC) networks.

LoadBalancer: Load Balancer distributes incoming application or network traffic across multiple targets, such as vm instances, containers, and IP addresses.It increases the availability and fault tolerance of your applications.

Cloud SQL Instance: A Cloud SQL instance is a machine with relational database running in the cloud. You can use Cloud SQL instances to store, replicate, and protect your relational databases.

Google Kubernetes Engine: Created by the same developers that built Kubernetes, Google Kubernetes Engine (GKE) is an easy to use cloud based Kubernetes service for running containerized applications.It provides a managed environment for deploying, managing, and scaling your containerized applications using Google infrastructure.It’s a management and orchestration system for Docker container and container clusters that run within Google’s public cloud services. Google Kubernetes Engine is based on Kubernetes, Google’s open source container management system.

WordPress: WordPress is a free and open-source content management system (CMS) written in PHP and paired with a MySQL & MariaDB databases.

Approach we will use to achieve this task:

  1. Create multiple projects namely developer and production.
  2. Create VPC network for both the projects, ‘region:us-east1’ for developer & ‘region:asia-southeast1’ for production.
  3. Create a link between both the VPC networks using VPC-Peering.
  4. Create a Kubernetes Cluster in developer project and launch an web application(WordPress) with the external Load Balancer attached to cluster.
  5. Create a SQL Instance in the production project and create a database.
  6. Connect the SQL database to the web application launched in the Kubernetes cluster.

Requirements :

  1. A local machine.
  2. A verified GCP account.
  3. Already installed ‘gcloud’ sdk and kubernetes client program ‘kubectl’ on local machine.

Project are just an organized way for managing our services/resources.It provides isolation to our resources, while also adding security to them.

So, we will start by logging into our GCP account and creating 2 projects named ‘developer’ and ‘production’.

Create project from local machine terminal (using gcloud):

gcloud projects create developer-288507 --name developer
gcloud projects create prod-9012 --name production
#check for all available projects:
gcloud projects list

once projects are created we will create a VPC network for creating your own private network.Coming from using AWS VPC service, i found GCP VPC a little confusing as they have a different architecture for how to use VPC. For me AWS VPC is more understandable.

Difference in VPC Architecture

Before using any resource we need to enable its service-API.

#Check for current active service-API.
gcloud services list --project=developer-288507
gcloud services list --project=prod-9012
#All available services list.
gcloud services list --available --project=developer-288507

Enabling service-API, if not active:

gcloud services enable compute.googleapis.com --project=developer-288507

Create VPC network

gcloud compute networks create vpc1 --bgp-routing-mode=regional --subnet-mode=custom --project=developer-288507#list all vpc 
gcloud compute networks list --project=developer-288507

Create subnet in this VPC network

gcloud compute networks subnets create subnet1 --network=vpc1 --region=us-east1 --range=192.168.1.0/28 --project=developer-288507#list subnets in a vpc
gcloud compute networks subnets list --network vpc1 --project=developer-288507
#list all available regions:
gcloud compute regions list

Create a VM instance

gcloud compute instances create instance1 --subnet=subnet1 --zone=us-east1-b --machine-type=n1-standard-1 --project=developer-288507#list machine-type
gcloud compute machine-types list
#list os-image type
gcloud compute image list
#list all zones
gcloud compute zones list

Set firewall-rule for VPC

#exposing port 22 and icmp protocol for ssh and ping.
gcloud compute firewall-rules create firewall1 --network vpc1 --allow tcp:22,icmp --source-ranges 0.0.0.0/0

Now, switch to production project and create above resources there.

#for switching projects
gcloud config set project production

Output after creating all resources in project:production.

Create private link between VPC networks.(VPC-Peering)

gcloud compute networks peerings create vpcpeer1 --network=vpc1 --peer-project=prod-9012 --peer-network=vpc2 --export-custom-routes --import-custom-routes --project=developer-288507gcloud compute networks peerings create vpcpeer2 --network=vpc2 --peer-project=developer-288507 --peer-network=vpc1 --export-custom-routes --import-custom-routes --project=prod-9012

Now, SSH to any one vm instance and try to ping other vm instance with its private address.

#ssh from vm in developer project.
gcloud compute ssh instance1 --zone=us-east1-b

We are successfully able to ping vm instance present in different private network in diff region using its private ip after activating vpc peering between VPC’s.

Launch a kubernetes cluster in developer project using container service:deploy and manage clusters of machines for running containers.

gcloud container clusters create k8-1 --region=us-east1 --network=vpc1 --num-nodes=1 --node-locations=us-east1-b,us-east1-c --machine-type=n1-standard-1 --subnetwork=subnet1

This will create a K8 cluster named K8–1 within subnet1 of vpc1, having total 2 slave nodes, 1 in each mentioned zones with master node being manged by GCP.

#check for active services attached with cluster
kubectl get service
#get active nodes info
kubectl get nodes -o wide
#get active pods info
kubectl get pods-o wide
#get active deployments info
kubectl get deployments

Create SQL instance with MySQL DBMS/database-engine in production project.

#set project to production
gcloud config set project prod-9012

Cloud SQL requires Google’s Service Networking API for private IP connectivity.

For creating SQL Instance, i am using Web-UI of GCP, as i was having some issue with command.

SSH to vm instance and login to SQL Instance.

gcloud compute ssh instance2 --zone=asia-southeast1-a#install mysql-client 
apt-get install default-mysql-client
#login to sql instance
mysql -h 10.170.80.5 -u root -p

and we are successfully able to login to our sql instance.

Create a database which will be used by WordPress.

create database1;#view databases
show databases;

Now, with all things sorted, its time to launch our WordPress Application container in K8’s cluster.

Create a deployment using wordpress image.This will launch our app container as a pod on the top of kubernetes slave nodes.

kubectl create deployment wordpress --image=wordpress

Attach an external LoadBalancer service with this cluster exposing its port 80 for external web-access.

kubectl expose deployment wordpress --type=LoadBalancer --port=80

*Note: If you want to scale-out no of pods manually to handle large traffic.You can also enable auto-scaling.

kubectl scale deployment wordpress --replicas=2

Access WordPress with loadbalancer public IP-address and fill in the required details.

Here, we have successfully set up a WordPress app as a container in a k8's cluster using Google cloud resources.

❤❤❤

--

--