You are looking at the documentation of a prior release. To read the documentation of the latest release, please visit here.

New to KubeDB? Please start here.

Using Custom Configuration File

KubeDB supports providing custom configuration for MySQL. This tutorial will show you how to use KubeDB to run a MySQL database with custom configuration.

Before You Begin

  • At first, you need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using kind.

  • Now, install KubeDB cli on your workstation and KubeDB operator in your cluster following the steps here.

  • To keep things isolated, this tutorial uses a separate namespace called demo throughout this tutorial.

    $ kubectl create ns demo
    namespace/demo created
    $ kubectl get ns demo
    demo    Active  5s

Note: YAML files used in this tutorial are stored in docs/examples/mysql folder in GitHub repository kubedb/docs.


MySQL allows to configure database via configuration file. The default configuration for MySQL can be found in /etc/mysql/my.cnf file. When MySQL starts, it will look for custom configuration file in /etc/mysql/conf.d directory. If configuration file exist, MySQL instance will use combined startup setting from both /etc/mysql/my.cnf and *.cnf files in /etc/mysql/conf.d directory. This custom configuration will overwrite the existing default one. To know more about configuring MySQL see here.

At first, you have to create a config file with .cnf extension with your desired configuration. Then you have to put this file into a volume. You have to specify this volume in spec.configSecret section while creating MySQL crd. KubeDB will mount this volume into /etc/mysql/conf.d directory of the database pod.

In this tutorial, we will configure max_connections and read_buffer_size via a custom config file. We will use configMap as volume source.

Custom Configuration

At first, let’s create my-config.cnf file setting max_connections and read_buffer_size parameters.

cat <<EOF > my-config.cnf
max_connections = 200
read_buffer_size = 1048576

$ cat my-config.cnf
max_connections = 200
read_buffer_size = 1048576

Here, read_buffer_size is set to 1MB in bytes.

Now, create a configMap with this configuration file.

$ kubectl create configmap -n demo my-configuration --from-file=./my-config.cnf
configmap/my-configuration created

Verify the config map has the configuration file.

$ kubectl get configmap -n demo my-configuration -o yaml
apiVersion: v1
  my-config.cnf: |
    max_connections = 200
    read_buffer_size = 1048576    
kind: ConfigMap
  name: my-configuration
  namespace: demo

Now, create MySQL crd specifying spec.configSecret field.

$ kubectl apply -f created

Below is the YAML for the MySQL crd we just created.

kind: MySQL
  name: custom-mysql
  namespace: demo
  version: "8.0.21"
    name: my-configuration
    storageClassName: "standard"
    - ReadWriteOnce
        storage: 1Gi

Now, wait a few minutes. KubeDB operator will create necessary PVC, statefulset, services, secret etc. If everything goes well, we will see that a pod with the name custom-mysql-0 has been created.

Check that the statefulset’s pod is running

$ kubectl get pod -n demo
custom-mysql-0   1/1       Running   0          44s

Check the pod’s log to see if the database is ready

$ kubectl logs -f -n demo custom-mysql-0
2020-08-26T10:45:27.892022Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-08-26T10:45:28.353024Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-08-26T10:45:28.465037Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060
2020-08-26T10:45:28.573208Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-08-26T10:45:28.573533Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2020-08-26T10:45:28.610887Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

Once we see [Note] /usr/sbin/mysqld: ready for connections. in the log, the database is ready.

Now, we will check if the database has started with the custom configuration we have provided.

First, deploy phpMyAdmin to connect with the MySQL database we have just created.

 $ kubectl create -f
deployment.extensions/myadmin created
service/myadmin created

Then, open your browser and go to the following URL: http://{node-ip}:{myadmin-svc-nodeport}. For kind cluster, you can get this URL by running the following command:

$ kubectl get svc -n demo myadmin -o json | jq '.spec.ports[].nodePort'

$ kubectl get node -o json | jq '.items[].status.addresses[].address'

# expected url will be:

Now, let’s connect to the database from the phpMyAdmin dashboard using the database pod IP and MySQL user password.

$ kubectl get pods custom-mysql-0 -n demo -o yaml | grep IP

$ kubectl get secrets -n demo custom-mysql-auth -o jsonpath='{.data.\user}' | base64 -d

$ kubectl get secrets -n demo custom-mysql-auth -o jsonpath='{.data.\password}' | base64 -d

Once, you have connected to the database with phpMyAdmin go to Variables tab and search for max_connections and read_buffer_size. Here are some screenshot showing those configured variables. max_connections


Cleaning up

To cleanup the Kubernetes resources created by this tutorial, run:

kubectl patch -n demo my/custom-mysql -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"
kubectl delete -n demo my/custom-mysql

kubectl delete deployment -n demo myadmin
kubectl delete service -n demo myadmin

kubectl delete ns demo

If you would like to uninstall KubeDB operator, please follow the steps here.

Next Steps