Deploy stateful applicaiton

Deploy MySQL stateful application

  • Stateful applications, like databases, need to maintain data across pod restarts.

  • Using pvc’s, Trident ensures data persistence for these applications.

  • Deploying MySQL as a sample stateful application gives a practical example of how Trident integrates with OpenShift to manage data for stateful applications, ensuring data is not lost when pods are moved or restarted.

Setup the MySQL Project

Before we create the MySQL application, we will creat a mysql project and store the application’s username and password in a Secret.

Create the mysql namespace

oc create ns mysql

We’ll use the mysql project as our default project

oc project mysql

Create the mysql secret

password is the password but can be chagned in the mysql-secret.yml file

oc apply -f mysql-secret.yml

Verify the secret was created.

oc get secrets mysql-password
NAME                       TYPE                                  DATA   AGE
mysql-password             opaque                                1      1h34m

Create a pvc for the MySQL application

oc apply -f mysql-pvc.yml

Verify the pvc’s are created by the MySQL application.

oc get persistentvolumeclaims
NAME          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-volume  Bound    pvc-676d059c-1480-4e36-963e-2249efc70331   10Gi       RWO            trident-csi    4h4m

MySQL Application Deployment

Next we will deploy the MySQL application on the ROSA cluster.

Open mysql-deployment.yml and review the details –metadata, replicas, and storageclass name.

For simplicity in this lab, we are only going to create one (1) replica set.

Execute the following command.

Ignore any warnings about PodSecurity
oc apply -f mysql-deployment.yml

Verify the application deployment. It will take a minute for the container to start.

oc get pods
NAME                        READY   STATUS    RESTARTS   AGE
mysql-fsx-7db4f45b8-mmfzv   1/1     Running   0          40s

Create a service for the MySQL application

  • A OpenShift service acts as an internal load balancer. It provides a stable endpoint through which other pods within the cluster can access the MySQL application, regardless of the individual states of the MySQL pods.

  • By specifying a service for MySQL, you provide a consistent internal address for the database, ensuring seamless communication even as pods are scaled or restarted.

oc apply -f mysql-service.yml

Verify the service.

oc get service
NAME    TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
mysql   ClusterIP   None         <none>        3306/TCP   4h3m

Create MySQL client

  • The MySQL client is used to access the MySQL application using the service we created.

  • This provides a consistent entry point into the database.

Review the content of mysql-client.yml and then deploy the MySQL client using the following command.

oc apply -f mysql-client.yml
Warning: would violate PodSecurity "restricted:v1.24": allowPrivilegeEscalation != false (container "mysql-container" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "mysql-container" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "mysql-container" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "mysql-container" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
pod/mysql-client created

Verify the pod status.

oc get pods
NAME                         READY   STATUS    RESTARTS   AGE
mysql-client                 1/1     Running   0          21s

Create a sample database

Log in to the MySQL client pod.

oc rsh mysql-client

Within the mysql-client pod, connect to the MySQL server.

mysql -u root --password=password -h mysql.mysql.svc.cluster.local

Once connected, we will create a new database.

From the MySQL [(none)]> prompt enter the following:

CREATE DATABASE erp;
CREATE TABLE erp.Persons ( ID int, FirstName varchar(255),Lastname varchar(255));
INSERT INTO erp.Persons (ID, FirstName, LastName) values (1234 , "John" , "Doe");
commit;
select * from erp.Persons;
mysql> CREATE DATABASE erp;
Query OK, 1 row affected (0.01 sec)

mysql> CREATE TABLE erp.Persons ( ID int, FirstName varchar(255),Lastname varchar(255));
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO erp.Persons (ID, FirstName, LastName) values (1234 , "John" , "Doe");
Query OK, 1 row affected (0.01 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from erp.Persons;
---------------------------
| ID   | FirstName | Lastname |
---------------------------
| 1234 | John      | Doe      |
---------------------------
1 row in set (0.00 sec)

Type exit to exit the mysql server and exit again to exit the pod. You should now be back at the bastion prompt

Creating Snapshots

  • Snapshots are point-in-time copies of your data, crucial for backup and disaster recovery.

  • Here, you’ll learn how to use Trident with FSx for ONTAP to create snapshots for backup, and how to restore your application data from these snapshots.

  • This is vital for protecting your application against data loss.

Scale down MySQL for an consitent snapshot

oc scale deployment mysql-fsx --replicas 0

Create the volume snapshot class and snapshot

oc apply -f volume-snapshot-class.yml

Create a snapshot of the exising MySQl data

oc apply -f volume-snapshot.yml

Use the following to find the name of the snapshot.

oc get volumesnapshots

Scale up MySQL for an consitent snapshot

oc scale deployment mysql-fsx --replicas 1

Data Recovery

  • This illustrates the use of snapshots in real-world scenarios through the deletion and restoration of the database.

  • This demonstrates the quick and efficient data recovery capabilities of Trident and FSx for ONTAP in managing and protecting OpenShift stateful application data.

Delete the erp database.

To delete the database erp after creating a snapshot follow these steps.

Log back in to the mysql-cleint

oc rsh mysql-client

Login to the MYSQL database.

mysql -u root --password=password -h mysql.mysql.svc.cluster.local

Delete the erp` database at the MySQL [(none)]> prompt

DROP DATABASE erp;

After executing the DROP command, the database "erp" will be deleted, and you should see a message like:

Query OK, 1 row affected

Verify the database has been deleted.

SHOW DATABASES;

Exit back out to the bastion prompt.

Restore the snapshot

  • Restoring a snapshot to a new pvc creates a new volume that mirrors the data state captured in the snapshot.

  • This process enables data recovery or access as it existed at the snapshot’s creation, without altering the original volume.

Create a new pvc from the snapshot.

The name of the new pvc is mysql-volume-clone
oc apply -f mysql-pvc-clone.yml

Update the MySQL application

We need to to update the mysql application to point to the new pvc.

Redeploy the application. This will recreate the pod so it points to the cloned pvc.

oc apply -f mysql-deployment-clone.yml

Verify the new pod is running. This may take a minute.

oc get pods
NAME                         READY   STATUS    RESTARTS   AGE
mysql-client                 1/1     Running   0          10m
mysql-fsx-59ff57c888-jcppr   1/1     Running   0          69s

Validate Database Restoration

  • Validation confirms that the restored data is complete and accurate, maintaining the integrity of the database after a recovery process.

  • Validation helps in identifying any issues or gaps in the restoration process, allowing for timely corrections

We can now check that our data has been restored.

oc rsh mysql-client
mysql -u root --password=password -h mysql.mysql.svc.cluster.local

Show Databases

SHOW DATABASES;
--------------------
| Database           |
--------------------
| information_schema |
| erp                |
| mysql              |
| performance_schema |
| sys                |
--------------------

Show database data

select * from erp.Persons;
---------------------------
| ID   | FirstName | Lastname |
---------------------------
| 1234 | John      | Doe      |
---------------------------

Congrats. You have completed the lab!