Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 5a7b295

Browse files
fawazsiddiqiFawaz Siddiqi
and
Fawaz Siddiqi
authored
Update Code Pattern to make it usable with new versions of Kubernetes (#58)
* Code pattern update * Code pattern updated * Code pattern updated * Updated readme build status added but need to be debugged * Readme added and updated Need to add build status * Update README.md * Update README.md Co-authored-by: Fawaz Siddiqi <[email protected]>
1 parent 634ab72 commit 5a7b295

8 files changed

+269
-81
lines changed

README.md

+134-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
[![Build Status](https://travis-ci.org/IBM/Scalable-WordPress-deployment-on-Kubernetes.svg?branch=master)](https://travis-ci.org/IBM/Scalable-WordPress-deployment-on-Kubernetes)
2-
3-
*Read this in other languages: [한국어](README-ko.md) / [日本語](README-jp.md).*
4-
51
# Scalable WordPress deployment on Kubernetes Cluster
62

73
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.
@@ -17,8 +13,6 @@ In addition to deployment on Kubernetes, we will also show how you can scale the
1713
- [MySQL (5.6)](https://hub.docker.com/_/mysql/)
1814
- [Kubernetes Clusters](https://cloud.ibm.com/docs/containers/cs_ov.html#cs_ov)
1915
- [IBM Cloud Compose for MySQL](https://cloud.ibm.com/catalog/services/compose-for-mysql)
20-
- [IBM Cloud DevOps Toolchain Service](https://cloud.ibm.com/catalog/services/continuous-delivery)
21-
- [IBM Cloud Container Service](https://cloud.ibm.com/catalog?taxonomyNavigation=apps&category=containers)
2216

2317
## Prerequisite
2418

@@ -33,25 +27,77 @@ This scenario provides instructions for the following tasks:
3327
- Create and deploy the WordPress frontend with one or more pods.
3428
- Create and deploy the MySQL database (either in a container or using IBM Cloud MySQL as backend).
3529

36-
## Deploy to IBM Cloud
37-
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](##steps)
30+
## Deply to IBM Cloud
31+
32+
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](##Methods-to-Deploy)
33+
34+
[![Deploy to IBM Cloud](https://cloud.ibm.com/devops/setup/deploy/button.png)](https://cloud.ibm.com/devops/setup/deploy?repository=https://github.com/IBM/Scalable-WordPress-deployment-on-Kubernetes&branch=master)
35+
36+
# Methods to Deploy
37+
38+
- [Using The Kustomization File](#Using-The-Kustomization-File)
39+
- [Manually deploying each file](#Manually-deploying-each-deployment)
40+
- [Using Compose for MySQL as Backend](#Using-IBM-Cloud-Compose-for-MySQL-as-backend)
41+
42+
# Using The Kustomization File
43+
44+
`kustomize` lets you customize raw, template-free YAML
45+
files for multiple purposes, leaving the original YAML
46+
untouched and usable as is.
47+
48+
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).
49+
50+
How does our `kustomization.yaml` file looks like:
51+
52+
```yaml
53+
secretGenerator: #generates secrets within the cluster
54+
- name: mysql-pass
55+
files:
56+
- password.txt
57+
resources: #runs the .yaml files which are written below
58+
- local-volumes.yaml
59+
- mysql-deployment.yaml
60+
- wordpress-deployment.yaml
61+
```
62+
#### To run the kustomization file
63+
64+
```bash
65+
kubectl apply -k ./
66+
```
67+
68+
#### Output
69+
70+
```bash
71+
secret/mysql-pass-c2f8979ct6 created
72+
service/wordpress-mysql created
73+
service/wordpress created
74+
deployment.apps/wordpress-mysql created
75+
deployment.apps/wordpress created
76+
persistentvolume/local-volume-1 created
77+
persistentvolume/local-volume-2 created
78+
persistentvolumeclaim/mysql-pv-claim created
79+
persistentvolumeclaim/wp-pv-claim created
80+
```
81+
![Kubernetes Status Page](images/kube_kust.png)
3882

39-
[![Create Toolchain](https://cloud.ibm.com/devops/setup/deploy/button.png)](https://cloud.ibm.com/devops/getting-started)
83+
[To read more about kustomization please click here](https://github.com/kubernetes-sigs/kustomize)
4084

41-
Please follow the [Toolchain instructions](https://github.com/IBM/container-journey-template/blob/master/Toolchain_Instructions_new.md) to complete your toolchain and pipeline.
85+
Now please move on to [Accessing the External Link](#Accessing-the-external-wordpress-link).
4286

43-
## Steps
87+
#### To remove the deployment
88+
89+
```bash
90+
kubectl delete -k ./
91+
```
92+
93+
# Manually deploying each deployment
4494
1. [Setup MySQL Secrets](#1-setup-mysql-secrets)
4595
2. [Create local persistent volumes](#2-create-local-persistent-volumes)
4696
3. [Create Services and Deployments for WordPress and MySQL](#3-create-services-and-deployments-for-wordpress-and-mysql)
47-
- 3.1 [Using MySQL in container](#31-using-mysql-in-container)
48-
- 3.2 [Using IBM Cloud MySQL](#32-using-ibm-cloud-mysql-as-backend)
49-
4. [Accessing the external WordPress link](#4-accessing-the-external-wordpress-link)
97+
4. [Accessing the external WordPress link](#Accessing-the-external-wordpress-link)
5098
5. [Using WordPress](#5-using-wordpress)
5199

52-
# 1. Setup MySQL Secrets
53-
54-
> *Quickstart option:* In this repository, run `bash scripts/quickstart.sh`.
100+
### 1. Setup MySQL Secrets
55101

56102
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).
57103

@@ -62,7 +108,7 @@ We need to make sure `password.txt` does not have any trailing newline. Use the
62108
tr -d '\n' <password.txt >.strippedpassword.txt && mv .strippedpassword.txt password.txt
63109
```
64110

65-
# 2. Create Local Persistent Volumes
111+
### 2. Create Local Persistent Volumes
66112
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.
67113

68114
#### For "lite" IBM Cloud Kubernetes Service
@@ -73,11 +119,9 @@ kubectl create -f local-volumes.yaml
73119
#### For paid IBM Cloud Kubernetes Service OR Minikube
74120
Persistent volumes are created dynamically for you when the MySQL and Wordpress applications are deployed. No action is needed.
75121

76-
# 3. Create Services and deployments for WordPress and MySQL
77-
78-
### 3.1 Using MySQL in container
122+
### 3. Create Services and deployments for WordPress and MySQL
79123

80-
> *Note:* If you want to use IBMCloud Compose-MySql as your backend, please go to [Using Bluemix MySQL as backend](#32-using-bluemix-mysql-as-backend).
124+
> *Note:* If you want to use IBM Cloud Compose for MySql as your backend, please go to [Using IBM Cloud MySQL as backend](#Using-IBM-Cloud-MySQL-as-backend).
81125
82126
Install persistent volume on your cluster's local storage. Then, create the secret and services for MySQL and WordPress.
83127

@@ -102,38 +146,57 @@ wordpress-3772071710-58mmd 1/1 Running 0 17s
102146
wordpress-mysql-2569670970-bd07b 1/1 Running 0 1m
103147
```
104148

105-
Now please move on to [Accessing the External Link](#4-accessing-the-external-wordpress-link).
149+
Now please move on to [Accessing the External Link](#Accessing-the-external-wordpress-link).
106150

107-
### 3.2 Using IBM Cloud MySQL as backend
151+
# Using IBM Cloud Compose for MySQL as backend
152+
153+
### Create Local Persistent Volumes
154+
To save your data beyond the lifecycle of a Kubernetes pod, you will want to create persistent volumes for your Wordpress applications to attach to.
155+
156+
#### For "lite" IBM Cloud Kubernetes Service
157+
Create the local persistent volumes manually by running
158+
```bash
159+
kubectl create -f local-volumes-compose.yaml
160+
```
161+
#### For paid IBM Cloud Kubernetes Service OR Minikube
162+
Persistent volumes are created dynamically for you when the MySQL and Wordpress applications are deployed. No action is needed.
108163

109164
Provision Compose for MySQL in IBM Cloud via https://cloud.ibm.com/catalog/services/compose-for-mysql
110165

111-
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
166+
Go to Service credentials and view your credentials (or add one if you don't see one created already). Your MySQL hostname, port, user, and password are under your credential url and it should look like this
167+
168+
```script
169+
db_type": "mysql",
170+
"uri_cli_1": "mysql -u <<USERNAME>> -p<<PASSWORD>> --host sl-us-south-1-portal.47.dblayer.com --port 22817 --ssl-mode=REQUIRED",
171+
```
172+
173+
**Alternatively** you could also go to the manage tab and under "Connection Strings" you will be able to find the username, password, hostname as well as the port as shown below
112174

113-
![mysql](images/mysql.png)
175+
![mysqlmanage](images/mysql_manage.png)
114176

115-
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.
177+
Go to your `wordpress-deployment-compose.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.
116178

117179
And the environment variables should look like this
118180

119181
```yaml
120182
spec:
121183
containers:
122-
- image: wordpress:4.7.3-apache
123-
name: wordpress
184+
- image: wordpress:latest
185+
name: wordpress-c
124186
env:
125187
- name: WORDPRESS_DB_HOST
126-
value: sl-us-dal-9-portal.7.dblayer.com:22412
188+
value: <<ENTER YOUR DB HOST>>:<<ENTER DB PORT>>
127189
- name: WORDPRESS_DB_USER
128-
value: admin
190+
value: <<ENTER YOUR DB USERNAME>>
129191
- name: WORDPRESS_DB_PASSWORD
130-
value: XMRXTOXTDWOOPXEE
192+
value: <<ENTER YOUR DB PASSWORD FROM COMPOSE>>
131193
```
132194
133-
After you modified the `wordpress-deployment.yaml`, run the following commands to deploy WordPress.
195+
196+
After you modified the `wordpress-deployment-compose.yaml`, run the following commands to deploy WordPress.
134197

135198
```bash
136-
kubectl create -f wordpress-deployment.yaml
199+
kubectl create -f wordpress-deployment-compose.yaml
137200
```
138201

139202
When all your pods are running, run the following commands to check your pod names.
@@ -148,8 +211,37 @@ This should return a list of pods from the kubernetes cluster.
148211
NAME READY STATUS RESTARTS AGE
149212
wordpress-3772071710-58mmd 1/1 Running 0 17s
150213
```
214+
#### To access the service
215+
216+
You can obtain your cluster's IP address using
217+
218+
```bash
219+
$ ibmcloud ks workers --cluster <your_cluster_name>
220+
OK
221+
ID Public IP Private IP Machine Type State Status
222+
kube-hou02-pa817264f1244245d38c4de72fffd527ca-w1 169.47.220.142 10.10.10.57 free normal Ready
223+
```
224+
225+
You will also need to run the following command to get your NodePort number.
226+
227+
```bash
228+
$ kubectl get svc wordpress-c
229+
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
230+
wordpress-c NodePort 172.21.179.176 <none> 80:30126/TCP 21s
231+
```
151232

152-
# 4. Accessing the external WordPress link
233+
Congratulations. Now you can use the link **http://[IP of worker node]:[service port number]** to access your WordPress site.
234+
235+
236+
> **Note:** For the above example, the link would be http://169.47.220.142:30126
237+
238+
### To delete your deployment
239+
240+
````bash
241+
kubectl delete -f wordpress-deployment-compose.yaml
242+
kubectl delete -f local-volumes-compose.yaml
243+
````
244+
# Accessing the external WordPress link
153245

154246
> If you have a paid cluster, you can use LoadBalancer instead of NodePort by running
155247
>
@@ -162,7 +254,7 @@ wordpress-3772071710-58mmd 1/1 Running 0 17s
162254
You can obtain your cluster's IP address using
163255

164256
```bash
165-
$ bx cs workers <your_cluster_name>
257+
$ ibmcloud ks workers --cluster <your_cluster_name>
166258
OK
167259
ID Public IP Private IP Machine Type State Status
168260
kube-hou02-pa817264f1244245d38c4de72fffd527ca-w1 169.47.220.142 10.10.10.57 free normal Ready
@@ -176,7 +268,7 @@ NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
176268
wordpress 10.10.10.57 <nodes> 80:30180/TCP 2m
177269
```
178270

179-
Congratulation. Now you can use the link **http://[IP]:[port number]** to access your WordPress site.
271+
Congratulations. Now you can use the link **http://[IP of worker node]:[service port number]** to access your WordPress site.
180272

181273

182274
> **Note:** For the above example, the link would be http://169.47.220.142:30180
@@ -196,11 +288,16 @@ NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
196288
wordpress 1 1 1 1 23h
197289
wordpress-mysql 1 1 1 1 23h
198290
```
291+
# To Scale Your Application
199292

200293
Now, you can run the following commands to scale up for WordPress frontend.
201294
```bash
202295
$ kubectl scale deployments/wordpress --replicas=2
203296
deployment "wordpress" scaled
297+
```
298+
299+
#### Check your added replica
300+
```bash
204301
$ kubectl get deployments
205302
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
206303
wordpress 2 2 2 2 23h

images/kube_kust.png

152 KB
Loading

images/mysql_manage.png

172 KB
Loading

kustomization.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
secretGenerator:
2+
- name: mysql-pass
3+
files:
4+
- password.txt
5+
resources:
6+
- local-volumes.yaml
7+
- mysql-deployment.yaml
8+
- wordpress-deployment.yaml

local-volumes-compose.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
apiVersion: v1
3+
kind: PersistentVolume
4+
metadata:
5+
name: local-volume-3
6+
labels:
7+
type: local
8+
spec:
9+
capacity:
10+
storage: 20Gi
11+
accessModes:
12+
- ReadWriteOnce
13+
hostPath:
14+
path: /tmp/data/lv-3
15+
persistentVolumeReclaimPolicy: Recycle

mysql-deployment.yaml

+22-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
apiVersion: v1
32
kind: Service
43
metadata:
@@ -16,7 +15,7 @@ spec:
1615
apiVersion: v1
1716
kind: PersistentVolumeClaim
1817
metadata:
19-
name: mysql-lv-claim
18+
name: mysql-pv-claim
2019
labels:
2120
app: wordpress
2221
spec:
@@ -26,13 +25,17 @@ spec:
2625
requests:
2726
storage: 20Gi
2827
---
29-
apiVersion: extensions/v1beta1
28+
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
3029
kind: Deployment
3130
metadata:
3231
name: wordpress-mysql
3332
labels:
3433
app: wordpress
3534
spec:
35+
selector:
36+
matchLabels:
37+
app: wordpress
38+
tier: mysql
3639
strategy:
3740
type: Recreate
3841
template:
@@ -42,21 +45,21 @@ spec:
4245
tier: mysql
4346
spec:
4447
containers:
45-
- image: mysql:5.6
48+
- image: mysql:5.6
49+
name: mysql
50+
env:
51+
- name: MYSQL_ROOT_PASSWORD
52+
valueFrom:
53+
secretKeyRef:
54+
name: mysql-pass
55+
key: password.txt
56+
ports:
57+
- containerPort: 3306
4658
name: mysql
47-
env:
48-
- name: MYSQL_ROOT_PASSWORD
49-
valueFrom:
50-
secretKeyRef:
51-
name: mysql-pass
52-
key: password.txt
53-
ports:
54-
- containerPort: 3306
55-
name: mysql
56-
volumeMounts:
57-
- name: mysql-local-storage
58-
mountPath: /var/lib/mysql
59+
volumeMounts:
60+
- name: mysql-persistent-storage
61+
mountPath: /var/lib/mysql
5962
volumes:
60-
- name: mysql-local-storage
61-
persistentVolumeClaim:
62-
claimName: mysql-lv-claim
63+
- name: mysql-persistent-storage
64+
persistentVolumeClaim:
65+
claimName: mysql-pv-claim

0 commit comments

Comments
 (0)