NOTE: This tutorial assumes you know how to connect to a Kubernetes cluster.
Create secrets to securely store MySQL credentials
- Guide on how to create base 64 encoded values:
- Create a new file called:
mysql-secret.yamland paste the value below.
NOTE: You must first capture the value in base 64 by following the guide in step 1.
---
apiVersion: v1
kind: Secret
metadata:
name: mysqldb-secrets
type: Opaque
data:
ROOT_PASSWORD: c3VwZXItc2VjcmV0LXBhc3N3b3JkLWZvci1zcWw= - Execute the below command to create secrets:
kubectl apply -f mysql-secret.yamlOutput:
secret/mysqldb-secrets created- To see if the secret is created, execute the below command:
kubectl get secretNAME TYPE DATA AGE
default-token-jqq69 kubernetes.io/service-account-token 3 6h20m
echo-tls kubernetes.io/tls 2 5h19m
mysqldb-secrets Opaque 1 42s- To see the description of the secret, execute the below command:
kubectl describe secret mysqldb-secretsName: mysqldb-secrets
Namespace: default
Labels: <none>
Annotations: <none>
Type: Opaque
Data
====
ROOT_PASSWORD: 29 bytesPersistant volume and MySQL deployment
- Create a persistent volume YAML file called:
mysql-pvc.yamland paste the following values:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pvc
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/mysql-data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: do-block-storage- Create a new deployment YAML file called: mysql-deployment.yaml and paste the following values:
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
clusterIP: None
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysqldb-secrets
key: ROOT_PASSWORD
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc-claimExecute the below command to create persistent volume:
kubectl apply -f mysql-pvc.yamlOutput:
persistentvolume/mysql-pvc createdpersistentvolumeclaim/mysql-pvc-claim createdExecute the below command to deploy MySQL pod:
kubectl apply -f mysql-deployment.yamlOutput:
service/mysql createdExposing MySQL as a Service
- Create a file called
mysql-service.yamland paste the following values:
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
selector:
app: mysql
ports:
- protocol: TCP
port: 3306
targetPort: 3306- Execute the below command to create a service for MySQL:
kubectl apply -f mysql-service.yamlOutput:
service/mysql-service created- To confirm if the service is created successfully, execute the below command:
kubectl get svcOutput:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
echo1 ClusterIP 10.245.179.199 <none> 80/TCP 6h4m
echo2 ClusterIP 10.245.58.44 <none> 80/TCP 6h2m
kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 6h33m
mysql ClusterIP None <none> 3306/TCP 4m57s
mysql-service ClusterIP 10.245.159.76 <none> 3306/TCP 36s

