How to Deploy MySQL on Kubernetes
Prepare the environment
Following this tutorial, you need to have Minikube installed on your Ubuntu Linux.
You can verify whether the Minikube has been successfully up and running by the following command:
$ minikube status
Output:
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
Create Secret for MySQL
Kubernetes uses Secret
to store and manage sensitive information such as passwords, ssh keys and OAuth tokens. In this tutorial, we use base64
encoded to store 'MYSQL_ROOT_PASSWORD'. For example:
$ echo -n 'admin' | base64
Output:
YWRtaW4=
Create a mysql-secret.yaml
file for MySQL that will be mapped as an environment variable as follows:
apiVersion: v1
kind: Secret
metadata:
name: mysql-pass
type: Opaque
data:
password: YWRtaW4=
Apply the manifest:
$ kubectl create -f mysql-secret.yaml
secret/mysql-pass created
Verify that the Secret
has just been created successfully:
$ kubectl get secrets
NAME TYPE DATA AGE
default-token-l7t7b kubernetes.io/service-account-token 3 4h24m
mysql-pass Opaque 1 1m
Deploy MySQL
Create the mysql-pod.yaml
file to deploy MySQL pod on Kubernetes cluster:
apiVersion: v1
kind: Pod
metadata:
name: k8s-mysql
labels:
name: lbl-k8s-mysql
spec:
containers:
- name: mysql
image: mysql:latest
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- name: mysql
containerPort: 3306
protocol: TCP
volumeMounts:
- name: k8s-mysql-storage
mountPath: /var/lib/mysql
volumes:
- name: k8s-mysql-storage
emptyDir: {}
Apply the manifest file:
$ kubectl create -f mysql-pod.yaml
pod/k8s-mysql created
Verify that the pod is running:
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
k8s-mysql 1/1 Running 0 30s
Now, we can connect to the k8s-mysql
pod:
$ kubectl exec k8s-mysql -it -- bash
root@k8s-mysql:/# echo $MYSQL_ROOT_PASSWORD
admin
root@k8s-mysql:/# mysql --user=root --password=$MYSQL_ROOT_PASSWORD
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.22 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
Kubernetes use Service
to expose pods to other pods or external systems. We will use the following manifest file mysql-service.yaml
to make the k8s-mysql
pod be reachable:
apiVersion: v1
kind: Service
metadata:
name: mysql-service
labels:
name: lbl-k8s-mysql
spec:
ports:
- port: 3306
selector:
name: lbl-k8s-mysql
type: ClusterIP
Apply the manifest to create the service:
$ kubectl create -f mysql-service.yaml
service/mysql-service created
Verify that the service has been successfully created:
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 5h4m
mysql-service ClusterIP 10.110.22.182 3306/TCP 30s