Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mounting ConfigMap deleted old data from MySQL my.cnf file

I am a newbee in Kubernetes/Openshift.

I am trying to update MySQL configuration using configmap. I have the below yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: slave-replcmap
data:
  my.conf: |
    [mysqld]
    server-id=2
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-slave
spec:
  selector:
    matchLabels:
      app: mysql-slave
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql-slave
    spec:
      volumes:
        - name: slave-mysql-persistent-storage
          persistentVolumeClaim:
            claimName: slave-nfs-claim1
        - name: slave-replcmap-vol
          configMap:
            name: slave-replcmap
        - name: slave-mysqlinitconf-vol
          configMap:
            name: slave-mysqlinitcmap
      containers:
      - image: mysql:5.7
        name: mysql-slave
        env:
        - name: MYSQL_SERVER_CONTAINER
          value: mysql
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_ROOT_PASSWORD
        - name: MYSQL_DATABASE
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_DATABASE
        - name: MYSQL_USER
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_USER
        - name: MYSQL_PASSWORD
          valueFrom:
              secretKeyRef:
                name: mysql-secret
                key: MYSQL_PASSWORD
        ports:
        - containerPort: 3306
          name: mysql-slave
        volumeMounts:
        - name: slave-mysql-persistent-storage
          mountPath: /var/lib/mysql
        - name: slave-mysqlinitconf-vol
          mountPath: /docker-entrypoint-initdb.d
        - name: slave-replcmap-vol
          mountPath: /etc/mysql/my.cnf
          subPath: my.conf

Its updating the config file no issues in that.

But the issue is its deleting the existing content from my.cnf file and adding configmap data. I need to append this configmap data to my.cnf file without deleting the existing data.

Please let me know how i have to modify the yml file for to achieve that.

Thanks in advance.

like image 779
Abdul Avatar asked Nov 17 '25 11:11

Abdul


1 Answers

Your current configuration mount volume directly to my.cnf file - mountPath: /etc/mysql/my.cnf. When you are doing that you are replacing it. subPath property is used to reference the file by key. Similar example can be found here.

Ive deployed this image on my local env. As default my.cnf inside have only some commented text and:

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

It means that all configuration files from both folders are included in main configuration.

Instead of mounting main config you might mount this config to one of the folders. For example:

...
          mountPath: /etc/mysql/conf.d/new.conf
          subPath: my.conf

Ohter ways how to pass ConfigMaps are describe here.

I would also recommended you to check this MySQL HELM Chart about configuration files in MySQL.

like image 109
PjoterS Avatar answered Nov 19 '25 09:11

PjoterS