Mounting ConfigMap deleted old data from MySQL my.cnf file

11/20/2019

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.

-- Abdul
kubernetes
openshift

3 Answers

2/27/2020

Luckily, the default /etc/mysql/my.cnf includes the following folder: /etc/mysql/conf.d/. So if you place your custom configuration in files in that folder, you are good.

You can achieve this with a ConfigMap as you propose.

Start by creating a ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-custom-config
data:
  my.custom.conf: |
    [mysqld]
    server-id=2

As you can see from the example, this ConfigMap contains a single file named my.custom.conf.

Next, map this file to a file under /etc/mysql/conf.d/:

spec:
  containers:
  - image: mysql:5.7
    name: mysql
    ...
    volumeMounts:
    - name: mysql-custom-config
      mountPath: /etc/mysql/conf.d/custom.my.cnf
      subPath: custom.my.cnf #should be the name used in the ConfigMap
  volumes:
  - name: mysql-custom-config
    configMap:
      name: mysql-custom-config
-- Sam
Source: StackOverflow

11/21/2019

i believe the content of existing my.cnf file is fixed .Thus you can add the existing content of my.cnf file to your configmap .

-- shubham_asati
Source: StackOverflow

12/3/2019

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.

-- PjoterS
Source: StackOverflow