How to ensure data persistence for Simple WordPress Website with Persistent MySQL Storage in Kubernetes?

PoorvashaPoorvasha
2 min read

Scenario: You're running a simple WordPress website in a Kubernetes cluster. The website consists of a WordPress application and a MySQL database. You need to ensure that the MySQL database data is persistent, so even if the MySQL pod is rescheduled or replaced, the data remains intact.

  1. Create a Storage Class:

    • Example: You create a StorageClass called standard-storage for your website's data.

    • YAML Definition:

        apiVersion: storage.k8s.io/v1
        kind: StorageClass
        metadata:
          name: standard-storage
        provisioner: example.com/standard-provisioner
      
    • Explanation: You define a StorageClass to specify the type of storage you want to use for your data.

  2. Create Persistent Volume Claims:

    • Example: You create a PersistentVolumeClaim for MySQL to request storage from the standard-storage class.

    • YAML Definition:

        apiVersion: v1
        kind: PersistentVolumeClaim
        metadata:
          name: mysql-pvc
        spec:
          accessModes:
            - ReadWriteOnce
          storageClassName: standard-storage
          resources:
            requests:
              storage: 10Gi
      
    • Explanation: You create a claim to request 10GB of storage from the standard-storage class for the MySQL database.

  3. Attach PVCs to Pods:

    • Example: In your MySQL Deployment, you attach the mysql-pvc to the MySQL pod as a volume.

    • YAML Definition (in Deployment):

        spec:
          volumes:
          - name: mysql-data
            persistentVolumeClaim:
              claimName: mysql-pvc
          containers:
          - name: mysql
            image: mysql:latest
            volumeMounts:
            - mountPath: /var/lib/mysql
              name: mysql-data
      
    • Explanation: You configure the MySQL pod to use the mysql-pvc for data storage.

  4. Use the Storage:

    • Example: Your WordPress website writes and reads data to/from the mounted storage provided by the mysql-pvc.

    • Explanation: The WordPress application communicates with the MySQL database through the PVC, ensuring data persistence even if the MySQL pod is rescheduled.

This scenario illustrates how you can ensure that the data for your MySQL database remains persistent in a simple WordPress website deployment in a Kubernetes cluster. The PVC abstracts the storage details, allowing you to focus on your application's functionality while Kubernetes handles the storage management.

0
Subscribe to my newsletter

Read articles from Poorvasha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Poorvasha
Poorvasha

Cloud & DevOps Engineer | Kubernetes | AWS | Ansible | GIT | Gitlab | Docker | Python