Compose and Deploy Wordpress

WordPress Introduction

WordPress is a free and open-source content management system written in PHP, allowing users to build their own websites. A complete Wordpress application includes the following Kubernetes objects with MySQL serving as the backend database.

WordPress

Objective

This tutorial demonstrates how to create an application (WordPress as an example) in KubeSphere and access it outside the cluster.

Prerequisites

An account project-regular is needed with the role operator assigned in one of your projects (the user has been invited to the project). For more information, see Create Workspace, Project, Account and Role.

Estimated Time

About 15 minutes.

Hands-on Lab

Task 1: Create Secrets

Create a MySQL Secret

The environment variable WORDPRESS_DB_PASSWORD is the password to connect to the database in WordPress. In this step, you need to create a ConfigMap to store the environment variable that will be used in MySQL pod template.

  1. Log in KubeSphere console using the account project-regular. Go to the detailed page of demo-project and navigate to Configurations. In Secrets, click Create on the right.

create-secret

  1. Enter the basic information (e.g. name it mysql-secret) and click Next. In the next page, select Default for Type and click Add Data to add a key-value pair. Input the Key (MYSQL_ROOT_PASSWORD) and Value (123456) as below and click in the bottom-right corner to confirm. When you finish, click Create to continue.

key-value

Create a WordPress Secret

Follow the same steps above to create a WordPress secret wordpress-secret with the key WORDPRESS_DB_PASSWORD and value 123456. Secrets created display in the list as below:

wordpress-secrets

Task 2: Create a Volume

  1. Go to Volumes under Storage and click Create.

create-volume

  1. Enter the basic information of the volume (e.g. name it wordpress-pvc) and click Next.
  2. In Volume Settings, you need to choose an available Storage Class, and set Access Mode and Volume Capacity. You can use the default value directly as shown below. Click Next to continue.

volume-settings

  1. For Advanced Settings, you do not need to add extra information for this task and click Create to finish.

Task 3: Create an Application

Add MySQL backend component

  1. Navigate to Applications under Application Workloads, select Composing App and click Create Composing Application.

Compose and Deploy Wordpress - 图7

  1. Enter the basic information (e.g. input wordpress for Application Name) and click Next.

basic-info

  1. In Components, click Add Service to set a component in the app.

add-service

  1. Define a service type for the component. Select Stateful Service here.
  2. Enter the name for the stateful service (e.g. mysql) and click Next.

mysql-name

  1. In Container Image, click Add Container Image.

container-image

  1. Enter mysql:5.6 in the search box, press Enter and click Use Default Ports. After that, do not click in the bottom-right corner as the setting is not finished yet.

Compose and Deploy Wordpress - 图12

Note

In Advanced Settings, make sure the memory limit is no less than 1000 Mi or MySQL may fail to start due to a lack of memory.

  1. Scroll down to Environment Variables and click Use ConfigMap or Secret. Input the name MYSQL_ROOT_PASSWORD and choose the resource mysql-secret and the key MYSQL_ROOT_PASSWORD created in the previous step. Click after you finish and Next to continue.

environment-var

  1. Select Add Volume Template in Mount Volumes. Input the value of Volume Name (mysql) and Mount Path (mode: ReadAndWrite, path: /var/lib/mysql) as below:

volume-template

Click after you finish and click Next to continue.

  1. In Advanced Settings, you can click Add directly or select other options based on your needs.

advanced-setting

  1. At this point, the MySQL component has beed added as shown below:

mysql-done

Add WordPress frontend component

  1. Click Add Service again and select Stateless Service this time. Enter the name wordpress and click Next.

Compose and Deploy Wordpress - 图17

  1. Similar to the step above, click Add Container Image, enter wordpress:4.8-apache in the search box, press Enter and click Use Default Ports.

Compose and Deploy Wordpress - 图18

  1. Scroll down to Environment Variables and click Use ConfigMap or Secret. Two environment variables need to be added here. Enter the values according to the screenshot below.
  • For WORDPRESS_DB_PASSWORD, choose wordpress-secret and WORDPRESS_DB_PASSWORD created in Task 1.
  • Click Add Environment Variable, and enter WORDPRESS_DB_HOST and mysql for the key and value.

Warning

For the second environment variable added here, the value must be exactly the same as the name you set for MySQL in step 5. Otherwise, Wordpress cannot connect to the corresponding database of MySQL.

environment-varss

Click to save it and Next to continue.

  1. In Mount Volumes, click Add Volume and select Choose an existing volume.

Compose and Deploy Wordpress - 图20

choose-existing

  1. Select wordpress-pvc created in the previous step, set the mode as ReadAndWrite, and input /var/www/html as its mount path. Click to save it and Next to continue.

Compose and Deploy Wordpress - 图22

  1. In Advanced Settings, you can click Add directly or select other options based on your needs.

Compose and Deploy Wordpress - 图23

  1. The frontend component is also set now. Click Next to continue.

two-components-done

  1. You can set route rules (Ingress) here or click Create directly.

Compose and Deploy Wordpress - 图25

  1. The app will display in the list below after you create it.

Compose and Deploy Wordpress - 图26

Task 4: Verify the Resources

In Workloads, check the status of wordpress-v1 and mysql-v1 in Deployments and StatefulSets respectively. If they are running as shown in the image below, it means WordPress has been created successfully.

wordpress-deployment

wordpress-statefulset

Task 5: Access WordPress through NodePort

  1. To access the service outside the cluster, navigate to Services first. Click the three dots on the right of wordpress and select Edit Internet Access.

edit-internet-access

  1. Select NodePort for Access Method and click OK.

access-method

  1. Click the service and you can see the port exposed.

nodeport-number

  1. Access this application via {Node IP}:{NodePort} and you can see an image as below:

wordpress

Note

Make sure the port is opened in your security groups before you access the service.