Kubernetes MySQL POD with Persistent EBS Volume in EKS

Kubernetes employs a client-server architecture, with a master managing nodes that host containerized applications. Deploying MySQL on Kubernetes requires persistent storage like Amazon EBS for data integrity. Using StatefulSets and Persistent Volume Claims (PVCs) ensures data persistence across pod lifecycles. YAML configurations for PersistentVolume (PV), PVC, and MySQL deployment outline resource definitions crucial for persistent data storage and database operation. Following best practices, including selecting the right storage classes and regular updates, enhances performance and security for MySQL on Kubernetes.

How Kubernetes works

Kubernetes is based on a client-server model, it implements a layered architecture with a master server controlling several nodes ( worker nodes ) on which containers are hosted. On each node, there are a variable number of containers ( aka POD ) that run your services ( deployments or workloads )

Read more here on creating a POD with Persistent volume

Setting Up MySQL on Kubernetes with Persistent Storage

Running MySQL databases on Kubernetes offers numerous benefits, including easy scalability, portability, and efficient resource utilization. To ensure data integrity and persistence in this dynamic environment, leveraging persistent storage solutions like Amazon Elastic Block Store (EBS) volumes is crucial. Let’s dive into the process of deploying MySQL on Kubernetes while ensuring data persistence through persistent volumes.

    Understanding StatefulSets and Persistent Volumes

    StatefulSets play a critical role in managing stateful applications like databases in Kubernetes. They provide guarantees for ordering and uniqueness of pods, ensuring that each pod maintains a persistent identity across deployments and rescheduling. Alongside StatefulSets, persistent volumes offer independent storage resources that can outlive the lifecycle of attached pods, ensuring data durability and availability.

    Persistent volumes are storage resources in Kubernetes that operate independently of attached pods. These resources can be provisioned statically at configuration or dynamically through Storage Classes. When you use persistent volumes, you create a PersistentVolumeClaim that operates like a pod. You can attach pods to this claim to allow them to use the persistent storage you created.

    Configuring PersistentVolume (PV) and PersistentVolumeClaim (PVC)

    In our YAML configurations, we define a PersistentVolume (PV) to represent the EBS volume where MySQL data will be stored. It’s essential to specify attributes such as access modes, capacity, and storage class to match the requirements of our workload. Additionally, creating a PersistentVolumeClaim (PVC) allows pods to dynamically request storage resources from the PV, enabling flexible and efficient allocation of storage.

    YAML file ( pv.yaml ) to create PV, edit the volume ID with the EBS Volume ID which you created and the _PV_ with the unique identifier name

    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: _PV_
    spec:
      accessModes:
      - ReadWriteOnce
      awsElasticBlockStore:
        fsType: xfs
        volumeID: aws://us-east-1a/vol-xxxxxxxxx
      capacity:
        storage: 10Gi
      persistentVolumeReclaimPolicy: Retain
      storageClassName: gp2-retain
      volumeMode: Filesystem

    YAML file ( pvc.yaml ) to create PVC, change the volumeName with the PV name you used to create using the above script, and mention the PVC name

    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      labels:
        app: asvignesh
      name: _PVC_
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 10Gi
      storageClassName: gp2-retain
      volumeMode: Filesystem
      volumeName: _PV_

    Deployment Strategy for MySQL

    Our MySQL deployment YAML defines the necessary components for running MySQL in a Kubernetes cluster. By specifying environment variables like the MySQL root password and configuring volume mounts to map the persistent storage from the PVC, we ensure that MySQL data persists across pod restarts and failures. Additionally, leveraging Kubernetes Services enables seamless communication with the MySQL database through a stable network endpoint.

    YAML file ( mysql.yaml ) for the deployment of MySQL, give the app name which is suitable for your deployment, and use the PVC name which you used in the earlier command

    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: mysql
      labels:
        app: asvignesh
    spec:
      ports:
        - port: 3306
          targetPort: 3306
      selector:
        app: asvignesh
        tier: mysql
      clusterIP: None
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mysql
      labels:
        app: asvignesh
    spec:
      selector:
        matchLabels:
          app: asvignesh
          tier: mysql
      strategy:
        type: Recreate
      template:
        metadata:
          labels:
            app: asvignesh
            tier: mysql
        spec:
          containers:
          - image: mysql:5.6
            name: mysql
            env:
            - name: MYSQL_ROOT_PASSWORD
              value: password
            ports:
            - containerPort: 3306
              name: mysql
            volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
          volumes:
          - name: mysql-persistent-storage
            persistentVolumeClaim:
              claimName: _PVC_

    To create the PV, PVC and Deployment, save the above code or get it from the GIST link mentioned below and run kubectl command

    kubectl apply -f pv.yaml
    kubectl apply -f pvc.yaml
    kubectl apply -f mysql.yaml
    

    Best Practices and Considerations

    While deploying MySQL on Kubernetes, it’s essential to consider several best practices:

    • Choose appropriate storage classes and access modes based on performance and availability requirements.
    • Implement backup and disaster recovery strategies to mitigate data loss risks.
    • Monitor resource utilization and performance metrics to optimize MySQL performance in a Kubernetes environment.
    • Regularly update Kubernetes and MySQL versions to leverage new features and security enhancements.

    Conclusion

    Deploying MySQL databases on Kubernetes with persistent storage offers a robust and scalable solution for managing data-intensive workloads in modern cloud-native environments. By leveraging StatefulSets and persistent volumes, we ensure data persistence and availability, enabling reliable and efficient operation of MySQL databases in Kubernetes clusters.

    Feel free to reach out if you have any questions or need further assistance in deploying MySQL on Kubernetes!


    Also published on Medium.

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Discover more from

    Subscribe now to keep reading and get access to the full archive.

    Continue reading