IBM/Scalable-WordPress-deployment-on-Kubernetes

Name: Scalable-WordPress-deployment-on-Kubernetes

Owner: International Business Machines

Description: This code showcases the full power of Kubernetes clusters and shows how can we deploy the world's most popular website framework on top of world's most popular container orchestration platform.

Created: 2017-03-13 23:10:22.0

Updated: 2018-05-21 13:05:29.0

Pushed: 2018-05-08 18:31:06.0

Homepage: https://developer.ibm.com/code/patterns/scalable-wordpress-on-kubernetes

Size: 7702

Language: Shell

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build Status

Read this in other languages: ???.

Scalable WordPress deployment on Kubernetes Cluster

This journey showcases the full power of Kubernetes clusters and shows how can we deploy the world's most popular website framework on top of world's most popular container orchestration platform. We provide a full roadmap for hosting WordPress on a Kubernetes Cluster. Each component runs in a separate container or group of containers.

WordPress represents a typical multi-tier app and each component will have its own container(s). The WordPress containers will be the frontend tier and the MySQL container will be the database/backend tier for WordPress.

In addition to deployment on Kubernetes, we will also show how you can scale the front WordPress tier, as well as how you can use MySQL as a service from IBM Cloud to be used by WordPress frontend.

kube-wordpress

Included Components
Prerequisite

Create a Kubernetes cluster with either Minikube for local testing, with IBM IBM Cloud Container Service, or IBM Cloud Private to deploy in cloud. The code here is regularly tested against Kubernetes Cluster from IBM Cloud Container Service using Travis.

Objectives

This scenario provides instructions for the following tasks:

Deploy to IBM Cloud

If you want to deploy the WordPress directly to IBM Cloud, click on Deploy to IBM Cloud button below to create an IBM Cloud DevOps service toolchain and pipeline for deploying the WordPress sample, else jump to Steps

Create Toolchain

Please follow the Toolchain instructions to complete your toolchain and pipeline.

Steps
  1. Setup MySQL Secrets
  2. Create local persistent volumes
  3. Create Services and Deployments for WordPress and MySQL
  4. 3.1 Using MySQL in container
  5. 3.2 Using Bluemix MySQL
  6. Accessing the external WordPress link
  7. Using WordPress

1. Setup MySQL Secrets

Quickstart option: In this repository, run bash scripts/quickstart.sh.

Create a new file called password.txt in the same directory and put your desired MySQL password inside password.txt (Could be any string with ASCII characters).

We need to make sure password.txt does not have any trailing newline. Use the following command to remove possible newlines.

d '\n' <password.txt >.strippedpassword.txt && mv .strippedpassword.txt password.txt

2. Create Local Persistent Volumes

To save your data beyond the lifecycle of a Kubernetes pod, you will want to create persistent volumes for your MySQL and Wordpress applications to attach to.

For “lite” IBM Bluemix Container Service

Create the local persistent volumes manually by running

ctl create -f local-volumes.yaml
For paid IBM Bluemix Container Service OR Minikube

Persistent volumes are created dynamically for you when the MySQL and Wordpress applications are deployed. No action is needed.

3. Create Services and deployments for WordPress and MySQL

3.1 Using MySQL in container

Note: If you want to use Bluemix Compose-MySql as your backend, please go to Using Bluemix MySQL as backend.

Install persistent volume on your cluster's local storage. Then, create the secret and services for MySQL and WordPress.

ctl create secret generic mysql-pass --from-file=password.txt
ctl create -f mysql-deployment.yaml
ctl create -f wordpress-deployment.yaml

When all your pods are running, run the following commands to check your pod names.

ctl get pods

This should return a list of pods from the kubernetes cluster.

                               READY     STATUS    RESTARTS   AGE
press-3772071710-58mmd         1/1       Running   0          17s
press-mysql-2569670970-bd07b   1/1       Running   0          1m

Now please move on to Accessing the External Link.

3.2 Using Bluemix MySQL as backend

Provision Compose for MySQL in Bluemix via https://console.ng.bluemix.net/catalog/services/compose-for-mysql

Go to Service credentials and view your credentials. Your MySQL hostname, port, user, and password are under your credential uri and it should look like this

mysql

Modify your wordpress-deployment.yaml file, change WORDPRESS_DB_HOST's value to your MySQL hostname and port (i.e. value: <hostname>:<port>), WORDPRESS_DB_USER's value to your MySQL user, and WORDPRESS_DB_PASSWORD's value to your MySQL password.

And the environment variables should look like this

spec:
  containers:
  - image: wordpress:4.7.3-apache
    name: wordpress
    env:
    - name: WORDPRESS_DB_HOST
      value: sl-us-dal-9-portal.7.dblayer.com:22412
    - name: WORDPRESS_DB_USER
      value: admin
    - name: WORDPRESS_DB_PASSWORD
      value: XMRXTOXTDWOOPXEE

After you modified the wordpress-deployment.yaml, run the following commands to deploy WordPress.

ctl create -f wordpress-deployment.yaml

When all your pods are running, run the following commands to check your pod names.

ctl get pods

This should return a list of pods from the kubernetes cluster.

                               READY     STATUS    RESTARTS   AGE
press-3772071710-58mmd         1/1       Running   0          17s

4. Accessing the external WordPress link

If you have a paid cluster, you can use LoadBalancer instead of NodePort by running

kubectl edit services wordpress

Under spec, change type: NodePort to type: LoadBalancer

Note: Make sure you have service "wordpress" edited shown after editing the yaml file because that means the yaml file is successfully edited without any typo or connection errors.

You can obtain your cluster's IP address using

 cs workers <your_cluster_name>

                                               Public IP        Private IP     Machine Type   State    Status   
-hou02-pa817264f1244245d38c4de72fffd527ca-w1   169.47.220.142   10.10.10.57    free           normal   Ready

You will also need to run the following command to get your NodePort number.

bectl get svc wordpress
        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
press   10.10.10.57   <nodes>       80:30180/TCP   2m

Congratulation. Now you can use the link http://[IP]:[port number] to access your WordPress site.

Note: For the above example, the link would be http://169.47.220.142:30180

You can check the status of your deployment on Kubernetes UI. Run kubectl proxy and go to URL 'http://127.0.0.1:8001/ui' to check when the WordPress container becomes ready.

Kubernetes Status Page

Note: It can take up to 5 minutes for the pods to be fully functioning.

(Optional) If you have more resources in your cluster, and you want to scale up your WordPress website, you can run the following commands to check your current deployments.

bectl get deployments
              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
press         1         1         1            1           23h
press-mysql   1         1         1            1           23h

Now, you can run the following commands to scale up for WordPress frontend.

bectl scale deployments/wordpress --replicas=2
oyment "wordpress" scaled
bectl get deployments
              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
press         2         2         2            2           23h
press-mysql   1         1         1            1           23h

As you can see, we now have 2 pods that are running the WordPress frontend.

Note: If you are a free tier user, we recommend you only scale up to 10 pods since free tier users have limited resources.

5. Using WordPress

Now that WordPress is running you can register as a new user and install WordPress.

wordpress home Page

After installing WordPress, you can post new comments.

wordpress comment Page

Troubleshooting

If you accidentally created a password with newlines and you can not authorize your MySQL service, you can delete your current secret using

ctl delete secret mysql-pass

If you want to delete your services, deployments, and persistent volume claim, you can run

ctl delete deployment,service,pvc -l app=wordpress

If you want to delete your persistent volume, you can run the following commands

ctl delete -f local-volumes.yaml

If WordPress is taking a long time, you can debug it by inspecting the logs

ctl get pods # Get the name of the wordpress pod
ctl logs [wordpress pod name]

References

License

Apache 2.0


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.