Kubernetes save mysql database's data inside a volume

8/4/2019

I'm new to kubernetes (using minikube) and i want to deploy an springboot app which uses mysql to store data.

I'm running my app inside a pod with 2 containers (one for my app and one for mysql), it works fine and as expected, my data are lost once i restard the pods (with a scale --replicas=0; scale --replicas=1 for exemple).

I'm using PersistentVolumeClaim, but still the data aren't stored, i'm for sure missing something important.

Here's my configuration file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: esse-deployment-1
  labels:
    app: esse-1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: esse-1
  template:
    metadata:
      labels:
        app: esse-1
    spec:
      containers:
      - image: mysql:5.7
        name: esse-datasource
        ports:
        - containerPort: 3306
        env: 
        - name: MYSQL_ROOT_PASSWORD
          value: root 
        volumeMounts:
        - name: mysql-persistent-storage-esse-1
          mountPath: /home/esse-1/data/mysql
      - image: esse-application
        name: esse-app
        imagePullPolicy: Never
        ports:
        - containerPort: 8080
        env:
        - name: ESSE_DATABASE_USERNAME
          value: root
        - name: ESSE_DATABASE_PASSWORD
          value: root
        - name: ESSE_APPLICATION_CONTEXT
          value: /esse-1
      volumes:
        - name: mysql-persistent-storage-esse-1
          persistentVolumeClaim:
            claimName: mysql-persistent-volume-claim-esse-1

---

apiVersion: v1
kind: Service
metadata:
  name: esse-service-1
  labels:
    app: esse-1
spec:
  selector:
    app: esse-1
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
  type: NodePort

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-persistent-volume-claim-esse-1
  labels:
    app: esse-1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
-- Mssm
kubernetes
minikube
mysql
persistent-volumes
spring-boot

1 Answer

8/4/2019

You need to mount the persistent volume to the directory where mysql is actually writing the database data to (adjust mountPath for the container). This is /var/lib/mysql in this case.

-- Thomas
Source: StackOverflow