Deploying a MySQL Stateful Application

Objective

Take the setting up of a Statefulset for an example. Here is a presentation of how to use the mirroring deployment, mysql:5.6, to set up a stateful MySQL app as the Wordpress website’s backend. It will show you how to use Statefulset. The MySQL initial password for this example will be created and saved as Secret. For presenting, here will only demonstrate processes. For relevant parameters and fields’ detailed explanation, please refer to the Secret and StatefulSets

Prerequisites

Estimated Time

  • About 10 minutes

Hands-on Lab

Deploy MySQL

Step 1: Create the Password

MySQL’s Enviromental variable MYSQL_ROOT_PASSWORD, namely the root user’s password, is private informsation. It’s inappropriate to show the password in steps. Therefore, here we use the password creation to replace the environmental variable. The created password will be keyed in as the environmental variable when setting up the MySQL container group.

1.1. Log in KubeSphere as the project-regular. Select Secret in the Configuration Center → Secrets, then click Create.

Deploying a MySQL Application - 图1

1.2. Fill in the password’s basic information,then click Next.

  • Name: The environmenal variables in the MySQL container can have customized names, such as mysql-secret.

  • Nickname: Nickname can be a mix of characters for you to differenciate resources, such as MySQL Secret.

  • Information Description: Simply introduce the password, such as MySQL Initial password.

    1.3. Fill in the following information into the secret setting page. Then click Create.

  • Type: Select default(Opaque).

  • Data: Fill in MYSQL_ROOT_PASSWORD and 123456for the data key-value pair.

Deploying a MySQL Application - 图2

Step 2: Create a StatefulSet

Navigate to Workload → StatefulSets, then click Create StatefulSet.

Deploying a MySQL Application - 图3

Step 3: Fill in Basic Information

Fill in the following information and then click Next.

  • Name:(Necessary) A simple name can help with user brpwsing and researching, such as wordpress-mysql.
  • Nickname: (Optional) Chinese can help with better resource differentiation, such as MySQL Database.
  • Information description: Simply introduce the workload for users’ understanding.

Step 4: Container Group Template

4.1. Click Add Container to fill in the container group seeing. The name is customizable. Fill in the mirror with mysql:5.6 (specific mirror edition number is needed). There is no limitation for CPU and storage. They will be used as the default reqest value when creating the project.

Deploying a MySQL Application - 图4

4.2 Set up the Service Setting and the Environmental Variable. Leave others unchanged. Then click Save.

  • Port: It can be named as Port. Select TCP protocol. Fill in 3306 at MySQL’s container port.
  • Environmental Variables: Check the box and click Reference Configuration Center. Key in MYSQL_ROOT_PASSWORDfor name and select the secret set in the first step mysql-secret and MYSQL_ROOT_PASSWORD.

Deploying a MySQL Application - 图5

4.3. Click Save and then click Next.

Step 5: Add Storage Volume Template

Complete the container group template then click Next. Lick Add Storage Volume Template in the template. Stateful data should be saved in persistent storage volume. Thus, you need to add storage volume to realize the data persistency. Please refer to the storage volume information as follows.

  • Volume Name: mysql-pvc
  • Storage Type: Select existing storage type, such as Local.
  • Capacity: Set 10 Gi by default and set access mode as ReadWriteOnce by default.
  • Mount Path: Find the storage volume’s mount path in the container. Select Read and Write and set pasth as /var/lib/mysql.

Click Save when you’re done. Then click Next.

Deploying a MySQL Application - 图6

Step 6. Service Configuration

If you need to reveal the MySQL application to other applications and servers, you need to create the service. Complete the parameter setting by refering the picture below. Then click Next.

  • Service Name: mysql-service (Attention: The Service Name will be associated with Wordpress so use this name when you add environmental variables.)
  • Conversation affinity: None by default
  • Ports: The name is customizable. Select TCP protocol. Fill 3306 for both of the MySQL service port and the target port. The first port the service port that needs to be exposed. The second port (target port) is the container port.

Note: If there is a requirement for conversation affinity, you can select “ClientIP” in the drop-down box or set the value of service.spec.sessionAffinity as “ClientIP” (“None” by default) in the code mode. This configuration can forward access request from the same IP address to the same rear end Pod.

Deploying a MySQL Application - 图7

Step 7: Tag Setting

Keep the tag as default setting app: wordpress-mysql. You can use the specific container group adjust the next node selector to the expected node. Do not set it for now. Click Create.

Inspect the MySQL Application

You can see the MySQL StatefulSet displays “updating” since this process requires a series of operations, such as pulling a Docker image creating a container, and initializing the database. It will show ContainerCreating. Normally, it will change to “running” at around 1 min. Click this you can access to the StateSet page including the Resource Status, Version Control, Monitoring, Environmental Variable and Events.

Resource Status

Deploying a MySQL Application - 图8

Monitoring Data

Deploying a MySQL Application - 图9

Events List

Deploying a MySQL Application - 图10

So far, MySQL Stateful application has been created successfully, it will be served as the backend database of the WordPress application.

It’s recommended to follow with Quick Start - Wordpress Deployment Guide to deploy the blog website, then you will be able to access the web service.