Search code examples
mysqlkuberneteskubernetes-pvc

how does PVC on Kubernetes work with MYSQL?


I am an computer enginering student. In my freetime I am working an a K8s project.

I was wondering how PVC works, I understand the basic concept. But here is my question. So this is the code for a basic wordpress with mysql application.

I was wondering if I want to make multiple instaces of this application, do I need to change the mountpath? Or is the mountpath the location within the container.

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

Solution

  • if you really want to run different instance you would really love to create different data directory for them. it's never good idea to use deployment in this scenario. it's better if you use statefulset to manage database. statefulset will automatically generate different pvc for different pods. you can check that with kubectl get pvc. here, in statefulset volumeClaimTemplates is the template for pvc.

    pvc name will be like that <pvc_template_name>-<statefulset_name>-<podnumber>.

    eg: mysql-pv-claim-wordpress-mysql-0

    
    apiVersion: v1
    kind: Service
    metadata:
      name: wordpress-mysql
      labels:
        app: wordpress
    spec:
      ports:
        - port: 3306
      selector:
        app: wordpress
        tier: mysql
      clusterIP: None
    
    ---
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: wordpress-mysql
      labels:
        app: wordpress
    spec:
      serviceName: wordpress-mysql
      replicas: 1
      selector:
        matchLabels:
          app: wordpress
          tier: mysql
    
      template:
        metadata:
          labels:
            app: wordpress
            tier: mysql
        spec:
          containers:
          - image: mysql:5.6
            name: mysql
            env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password
            ports:
            - containerPort: 3306
              name: mysql
            volumeMounts:
            - name: mysql-pv-claim
              mountPath: /var/lib/mysql
      volumeClaimTemplates:
      - metadata:
          name: mysql-pv-claim
        spec:
          accessModes: [ "ReadWriteOnce" ]
          storageClassName: standard
          resources:
            requests:
              storage: 20Gi