0

I have created a db called db1 and it's stored in /var/lib/mysql on my physical machine and created a db2 in kubenetes and its stored in a pv I want to link db1 so that I access it through the persistent volume.
This is the service file:

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: my-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

this is the pv and pvc files:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

this is the database created locally : enter image description here this is the database created in kubernetes: enter image description here

  • What is your scenario you want to cover? – Manuel Apr 24 '21 at 19:13
  • running an old mysql database in kubernetes ,the way I did it was by copying the sql file commands and running them inside the mysql pod if you have a different way or I'm wrong at something please feel free to help me thank you. – Benabbou F Zouhir Apr 25 '21 at 10:51
  • Depends on how many times you have to do it, when and how big is your data. The mysql image itself for example allows you to execute sql scripts on start. This could be an option. Another way is to create an application based migration, like flyway or other db migration processes. If you just have to do it once, do a one-time process and backup the pv. If you like, we can also chat and workout a solution and post it here later on. – Manuel Apr 26 '21 at 07:29
  • @ManuelPolacek yes I would like that very much how do I contact you? – Benabbou F Zouhir Apr 26 '21 at 14:49
  • We can chat inside here https://chat.stackoverflow.com/rooms/231626/how-to-link-a-mysql-database-to-a-persistent-volume-on-kubernetes – Manuel Apr 26 '21 at 20:58

0 Answers0