adam b
adam b

Reputation: 33

Execute mysql command with kubernetes poststart

I'm trying to get logs from mysql container. I have to set the variable :

"SET GLOBAL general_log= 1"

I'm trying to set the variable by using kubernetes poststart but it's not working . Here is my config :

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        lifecycle:
          postStart:
            exec:
              command: ["/bin/bash", "-c","/etc/init.d/mysql start", "mysql -p${MYSQL_ROOT_PASSWORD} <<< 'SET GLOBAL general_log= 1;'" ]
 
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql  #The path used to mount the hostPath share inside the pod.
   
  - name: log-mysql
    image: alpine
    args: [/bin/sh, -c, "tail -n+1 -f /tmp/logs/mysql-0.log"] 
    volumeMounts:
    - name: mysql-persistent-storage
      mountPath: /tmp/logs
  
  volumes:                    
  - name: mysql-persistent-storage
    persistentVolumeClaim:
      claimName: mysql-pvclaim
  
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql

I don't have any error when I apply this config but the value of the general_log variable stay OFF and doesn't turn ON.

PS: when I connect to my pod and manually type

mysql -p${MYSQL_ROOT_PASSWORD} <<< 'SET GLOBAL general_log= 1;'

the value of the variable turn ON. I don't understand why it's not working using Poststart.

Can I have some help ? Thanks

Upvotes: 1

Views: 661

Answers (1)

Thomas
Thomas

Reputation: 12029

The command is excuted too early, right after the container is created and before mysqld is ready to accept commands.

You can use a different method to achieve your goal:

  • proivde a container command and use "--general-log=1"
  • create a custom custom.cnf file with the settings and volume mount it to /etc/mysql/conf.d

Upvotes: 1

Related Questions