Kubernetes Views 5

Running MySQL on Kubernetes

Running MySQL on Kubernetes image 1

Running MySQL as a Pod


Basic setup


[Kubernetes] Building a Kubernetes cluster

[Kubernetes] Building NFS dynamic provisioning

Before deploying MySQL


Before you deploy MySQL, deploy the PVC and the ConfigMap.

  • pvc.yaml — Write the pvc.yaml file as below, deploy it, then check that a PV was created.
  • kubectl apply -f pvc.yaml kubectl get pv -A
  • apiVersion: v1 kind: PersistentVolumeClaim metadata: name: test-mysql-pv-claim namespace: test spec: storageClassName: nfs-client accessModes: - ReadWriteOnce resources: requests: storage: 5Gi
  • configmap.yaml — Deploy this the same way.
  • apiVersion: v1 kind: ConfigMap metadata: name: mysql-config namespace: bangbaesa data: custom.cnf: |+ [mysqld] ft_min_word_len=2 character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci lower_case_table_names=1 innodb_ft_min_token_size=2 log_output=FILE general_log general_log_file=/var/log/mysql/mariadb.log log_error=/var/log/mysql/mariadb.err log_warnings=9 slow_query_log slow_query_log_file=/var/log/mysql/mariadb-slow.log long_query_time=1.0

Deploying MySQL

After the PVC and ConfigMap have been deployed, write deployment.yaml and deploy it.

YAML
 apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
 kind: Deployment
 metadata:
   name: mysql
   namespace: test
 spec:
   selector:
     matchLabels:
       app: mysql
   strategy:
     type: Recreate
   template:
     metadata:
       labels:
         app: mysql
     spec:
       containers:
       - name: mysql
         image: mariadb:10.5.8
         env:
           # Use secret in real usage
         - name: MYSQL_ROOT_PASSWORD
           value: test
         ports:
         - containerPort: 3306
           name: mysql
         volumeMounts:
         - name: mysql-persistent-storage
           mountPath: /var/lib/mysql
         - name: time-zone
           mountPath: /etc/localtime
         - name: config-volume
           mountPath: /etc/mysql/mariadb.conf.d/custom.cnf
           subPath: custom.cnf
         - name: mysqllog
           mountPath: /var/log/mysql
       - name: mysql-err-log
         image: busybox
         args: [/bin/sh, -c, 'tail -n+1 -f /var/log/mysql/mariadb.err']
         volumeMounts:
         - name: mysqllog
           mountPath: /var/log/mysql
       - name: mysql-slow-log
         image: busybox
         args: [/bin/sh, -c, 'tail -n+1 -f /var/log/mysql/mariadb-slow.log']
         volumeMounts:
         - name: mysqllog
           mountPath: /var/log/mysql
       - name: mysql-gen-log
         image: busybox
         args: [/bin/sh, -c, 'tail -n+1 -f /var/log/mysql/mariadb.log']
         volumeMounts:
         - name: mysqllog
           mountPath: /var/log/mysql
       volumes:
       - name: mysql-persistent-storage
         persistentVolumeClaim:
           claimName: test-mysql-pv-claim
       - name: time-zone
         hostPath:
           path: /usr/share/zoneinfo/Asia/Seoul
       - name: config-volume
         configMap:
           name: mysql-config
           optional: true
       - name: mysqllog
         emptyDir: {}
 ---
 apiVersion: v1
 kind: Service
 metadata:
         name: mysql-in
         namespace: test
 spec:
         selector:
                 app: mysql
         ports:
                 - protocol: TCP
                   port: 3306
                   targetPort: 3306

If you look at the file above, the PVC and ConfigMap volumes are mounted. If those two are not mounted, it will not run, so check them carefully and then deploy.

 kubectl apply -f deployment.yaml
 kubectl logs -n test mysql

Also check the logs to confirm that it is running normally.

Reactions

Comments 0

A nickname and a password are required. The same password edits or deletes the comment later. Comments appear after a review.

Comments appear after a review. The password is used again to edit or delete.