Cloud Container Service Engine (CCSE)

Containerized Web Access to MySQL (Local PV) Practice

2025-07-11 06:12:35

This section introduces how to deploy a stateless workload, connect to a stateful database, and use persistent storage in actual scenarios. In this tutorial, we use Local PV as the persistent storage medium. In production use, Ceph storage is recommended.

l  Creating Persistent Storage Class

In the CCSE interface, click on the buttons shown in the figure above in sequence to access the storage class creation interface. Then choose Local storage, as shown below:

Creating PV:

图形用户界面, 应用程序 描述已自动生成

In the CCSE interface, click on the buttons shown in the figure above in sequence to access the PV creation interface. Then choose the PV created in step 1, as shown below:

图形用户界面, 网站 描述已自动生成

Choose the node and directory for the LocalPV. Here, the node for LocalPV is the k8s node where you want to persist the container data, and the directory for LocalPV is the specified directory on the selected node. (Note: This directory should exist on the selected node. If it does not, you need to manually create the directory on that node.)

Creating MySQL Stateful Service and Using PV:

In the CCSE interface, click on the buttons shown in the figure above in sequence to access the PV creation interface. Then create MySQL as shown below:

 

Upon creating a new PVC, pay close attention to the notes indicated in the image below:

图形用户界面 中度可信度描述已自动生成

 

Next, proceed with setting the parameters for the MySQL container. Enter the MySQL container name, and select the MySQL image and version number. Pay close attention to the mount point name and the container path.

图形用户界面, 应用程序 描述已自动生成

 

Set the resource parameters and environment variables of the container. For the MySQL container to run properly, we must set the environment variable MYSQL_ROOT_PASSWORD and set the root user password. Additionally, if we need to create a database upon the startup of the MySQL container, we can use the environment variable MYSQL_DATABASE.

Finally, to configure the internal cluster access mode for the MySQL container, we choose the type as ClusterIP.

 

l  Create a stateless web workload and connect it to the database.

1. Web Workload Configuration

The configuration files used by our web workload are as follows:

spring:

  jpa:

    show-sql: false

    open-in-view: true

  datasource:

    driver-class-name: "com.mysql.cj.jdbc.Driver"

    jdbc-url: "jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false"

    username: "${MYSQL_USERNAME}"

password: "${MYSQL_PASSWORD}"

 

In the above configuration, we set the MySQL host IP, port, username, and password required by the application through reading the environment variables.

 

2. Creating a Stateless Workload

This section only covers how to set the environment variables for the stateless workload. Other parameter configurations are similar to those used for MySQL:

 

l  Viewing MySQL container (optional)

Enter the k8s cluster and view through command the MySQL container just created.

Execute the command to enter this MySQL container.

Execute the MySQL command to connect to MySQL client. Use the parameters as follows: the parameter after -h is the name of the MySQL workload created in step 3.1, -u specifies the user as the root user, and -p specifies the root user password. The specified password should be the value of the environment variable MYSQL_ROOT_PASSWORD set when creating the MySQL container in 3.1.

View the database in MySQL, and you can see the database created in step 3.1 through the environment variable MYSQL_DATABASE.

文本 描述已自动生成

l  Verifying the Persistent Storage of the MySQL Container (optional)

Create a database containerized web access to MySQL (LocalPV). Execute the exit command twice consecutively to exit the MySQL client and MySQL container respectively.

For verification, we can redeploy the MySQL container on the CCSE interface and then check if the data still exists, as shown in the figure below:


6Qv1Vi9.M8z8