Install Mysql 8 on kubernetes

By Amin

Table of Contents

Agenda

This reference will cover how do we can install MySql server 8 on Kubernetes. This guideline will take care of storage, configuration, and deployment.

Storage

On-disk files in a Container are ephemeral, which presents some problems for non-trivial applications when running in Containers. We should mount this disk contents to outside of containers, and this storage can be your physical machine, or in the cloud world it will be your storage-class.

In Kubernetes world, this storage is define as a K8s Storgae topics.

How we can define a storage

There are two approaches to define storages.

Define a PersistentVolumeClaim object

Each Kubernetes cluster will have own storage-class, and it is easy to figure out.

$ kubectl get storageclass

,and the result will be

NAME                         PROVISIONER                 RECLAIMPOLICY   VOLUMEBINDINGMODE
do-block-storage (default)   dobs.csi.digitalocean.com   Delete          Immediate      

,and then we can define a persistent-volume

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-volume
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
  storageClassName: do-block-storage

Define a VolumeClaimTemplates inside of StatefulSet object

There is a way to define storage inside of StatefulSet object.

 volumeClaimTemplates:
    - metadata:
        name: '[name-of-storage]'
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: '[size-of-storage]'
        storageClassName: '[storage-class-name]'

so, it will be easy to define a storage in a one YAML file. This is full source of StatefulSet object which has a storage inside.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: database
          image: mysql:8.0
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mysql-data
              mountPath: /var/lib/mysql
              subPath: mysql
  volumeClaimTemplates:
    - metadata:
        name: mysql-data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 20Gi
        storageClassName: do-block-storage
  serviceName: mysql

Find out what we did

$ kubectl get PersistentVolumeClaim --all-namespaces

and the result will be

NAME                 STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS       AGE
mysql-data-mysql-0   Bound    pvc-a5dc827c-d33b-4c50-982e-d3d959ac12f2   5Gi        RWO            do-block-storage   65d

Configuration & Secrets

The configuration sometime is painful, but it will be so easy on Kubernetes cluster. A ConfigMap is an API object used to store non-confidential data in key-value pairs in Kubernetes Pods.

Define ConfigMap

ConfigMaps are suitable

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  labels:
    app: mysql
data:
  ## default_auth is the name of config. This will be referred from volume mount definition
  mysql_database: "default"
  mysql_user: "admin"
  ## We can define a bunch of different users here
  another_user: "another"
  default_auth: |
    [mysqld]
    default_authentication_plugin= mysql_native_password