Installing WordPress on GKE with Cloud SQL

Rohan Singh
Jan 8 · 8 min read

Recently, we need WordPress for some work hosted on Google Kubernetes Engine. So yeah it’s definite we would install WordPress on GKE with Helm but my Senior DevOps Engineer decided to use Google Cloud SQL for this instead MariaDB.

So here is another blog on Cloud from my side after learning and doing...

Credit: Me

Before starting this, I’m assuming that you have a little bit hands-on knowledge of Kubernetes (k8s), WordPress and understanding of basic terms of Kubernetes and SQL.

Note: This tutorial includes installation of WordPress via Helm, make sure you are following the same sequence of creation and installation of service/tools because once the user id/password of WP admin get store in SQL, it is not possible to change it via helm upgrade through values.yaml file(it could be a bug in wordpress helm). We were facing same, then after we have to delete SQL instance, delete DB and install everything again, put values in file to login in WP Admin.

Google Kubernetes Engine (GKE)

Google Kubernetes Engine (GKE) is a managed, production-ready environment for deploying containerized applications. It’s a perfect combination or I should say a blend of Iaas and PaaS making it as Kubernetes as a Service or Container as a Service which Google was using since 2003 as its internal orchestrator till 2015 when it gets open-source with a benefit of autoscaling of the cluster, integration of Stackdriver, fluentd, Prometheus, VPN etc. Kubernetes Engine enables rapid application development and iteration by making it easy to deploy, update, and manage our applications and services with no vendor lock-in which means you’re free to take your applications out of Kubernetes Engine and run them anywhere Kubernetes is supported, including on your own on-premises servers.

Let’s start from creating a Kubernetes Cluster:

It will take some time to create K8S cluster:

Run below command to view cluster or you can view it in UI too

We need to create namespace first; namespaces are virtual clusters which scopes of resources among users, environments and projects. Read about it from official docs.

We will install every resource in the cluster within wordpress namespace.

Create Cloud SQL Instance and Database

Cloud SQL is a fully managed or no apps database service that makes it easy to set up, maintain, manage, and administer your relational databases on GCP. Cloud SQL has a high capacity capable of handling terabytes of storage. The databases are relational, which means that you are simply going to be running queries such as SELECT statements to read fields data or INSERT statements to write fields data.

Cloud SQL supports many clients such as CloudTrail, App Engine, and G Suite Scripts. It also supports other applications and tools that you might be used to, like SQL Workbench, Toad, and other external applications using standard MySQL drivers.

Let's create Cloud SQL Instance in GCP Console:

Choose MySQL, ensure 5.7 version is selected

Enter Instance ID whatever you want

Set Root password whatever you want

In Show configuration options: Select Machine type and storage; select machine type, storage type and enter storage capacity according to your need

That’s it, now hit Create.

When all done, your instance will get a public IP.

Upon clicking on the Instance ID, you’ll get SQL instance overviews like CPU Utilization, Public IP, Configurations and all.

In the left navigation area, go to Connections option and in the Connectivity section, add an authorized network with 0.0.0.0/0

Below connections, goto Users and Create a user account with name and password.

Below Users, goto Database and create DB, just by entering the name.

Now we are done with Google Cloud SQL setup. It’s time to install Helm for installation of WordPress.


Helm

Helm is just package manager for Kubernetes for deploying and managing complex applications easily and flexibly. It’s what NPM is for Node, Hombrew for Mac etc.

For a better explanation, you can visit my last blog there I’ve explained about Helm in a detailed way or you can also visit Helm Official Docs.

To install and setup Helm( v2.14.1) in your cluster:

Again if you want a better explanation of each command above, go to my previous blog from the above link.

Yay! we are done with Helm installation, now let’s move forward to install WordPress.

WordPress

WordPress is open source software you can use to create a beautiful website, blog, or app-based on PHP and MYSQL. WordPress was released on May 27, 2003, by its founders, Matt Mullenweg and Mike Little, as a fork of b2/cafelog. The software is released under the GPLv2 (or later) license. WordPress is used by more than 60 million websites, including 33.6% of the top 10 million websites as of April 2019, it is one of the most popular content management system (CMS) solutions in use. It has also been used for other application domains such as pervasive display systems (PDS).

Since we are using our own MySQL we need to clone the GitHub repo of Helm, make some changes in the configuration file like DB username, password, IP of MySQL etc and then deploy it via Helm command.

Clone the Helm Charts repo, either in cloud-shell or your local-system

Once in there you’ll find there a values.yaml which is a configuration file of WordPress.

Now you need to make some essential changes in this file which are as follows (you can use your own value, I’m using just mine)

  • wordpressUsername: admin [line 41]
  • wordpressPassword: my-password [line 47]
  • wordpressEmail: rohandash1998@gmail.com [line 52]
  • host: [IP_Address of Cloud SQL Instance] [line 151]
  • user: wp-user [line 154] (values you enter while setting up cloud SQL)
  • password: “my-password” [line 157]
  • database: wp_db [line 160]
  • port: 3306 [line 163]
  • In the MariaDB section,enabled: false [line 172]
  • In the service section, type: ClusterIP [line 214]
  • port: 6002 [line 217]
  • In the ingress section, enabled: false [line 283]

That’s it, now we are ready to launch WordPress.

Run the following commands:

After successful installation, run

See there is no ingress till now because we disabled ingress in values.yaml now will create our own ingress.

To get ingress, we need to install nginx-ingress-controller, we will do it via Helm.

Now make an ingress file and paste the following code.

Run kubectl -n wordpress apply -f ingress.yaml to deploy ingress.

Now we are done with all major task.

Now a small task has left; mapping of a subdomain with load balancer IP. For this, I’m assuming you already have a domain, we just need to create A Record in DNS of our domain manager with value our ingress IP and host what we mentioned in our ingress.yaml .

This mapping would some time to get attach with load balancer IP.

Once done, open your host or subdomain, in my case it’s dev.rohankalhans.xyz

You will get a landing page.

For login just enter dev.rohankalhans.xyz/wp-admin a login page will appear. Enter the username and password that you have given in values.yaml of WordPress.

After successful login, you get your beautiful WordPress dashboard.

Ensuring data is stored in MySQL database

Open your Google Cloud Shell and enter these commands:

So yeah, that’s how you set up WordPress on Kubernetes via Helm using Google Cloud SQL.

And all done.

Google Kubernetes Engine

Google Cloud SQL

Helm

Helm Charts

Kubernetes

WordPress

Read my other blogs on Cloud

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬.

To join our community Slack team chat 🗣️ read our weekly Faun topics 🗞️, and connect with the community 📣 click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Rohan Singh

Written by

DevOps Engineer Intern @ Techolution | Living in ☁️ since Sept 2017 | https://rohankalhans.xyz

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade