Hi All,
I’ve been continuing my learning journey into the world of Kubernetes with Azure. In this post I would like to explain how I went about deploying a MySQL container with Azure File storage as a mount point. before you follow along you need to ensure that you have provisioned an AKS (Azure Kubernetes Instance) and that on your developer machine you have installed the AZ CLI and the kubectl tools.
Once you have installed the above tools and created your AKS instance in Azure, we now need to fetch the cluster credentials so that we can issue kubectl commands against it.
from a Windows command prompt issue the following:
1 |
az aks get-credentials --resource-group <RESOURCE GROUP NAME > --name <AKS CLUSTER NAME> |
be sure to replace <RESOURCE GROUP NAME> and <AKS CLUSTER NAME> with your values and without the <>.
YAML Files
To create my YAML files I am using Visual studio code with the YAML extension to provide code highlighting, I’ve created 4 files called:
- mysql-clusterip-service.yaml
- mysql-deployment.yaml
- persistent-volume-claim.yaml
- user-management-configmap.yaml
let me show you the code for each file in turn, followed by a brief explanation as to what the code is doing.
mysql-clusterip-service.yaml
Code
1 2 3 4 5 6 7 8 9 10 |
apiVersion: v1 kind: Service metadata: name: mysql spec: selector: app: mysql ports: - port: 3306 clusterIP: None # this means we are going to use Pod IP |
Explanation:
This Kubernetes Service manifest defines a Service named “mysql” that selects pods with the label app: mysql and forwards traffic on port 3306 to those pods. However, instead of assigning a cluster IP, it allows direct access to the individual pod IPs, making it suitable for stateful services like a MySQL database.
mysql-deployment
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
apiVersion: apps/v1 kind: Deployment metadata: name: mysql spec: replicas: 1 selector: # Pod Selector Labels matchLabels: app: mysql strategy: # Stragey for deployment type: Recreate template: # Pod Template metadata: labels: app: mysql spec: # Pod Specification containers: - name: mysql image: mysql:5.6 ports: - containerPort: 3306 name: mysql volumeMounts: # Container level resource - name: mysql-persistent-storage mountPath: /var/lib/mysql - name: usermanagement-dbcreation-script mountPath: /docker-entrypoint-initdb.d volumes: # Pod spec level resource - name: mysql-persistent-storage persistentVolumeClaim: claimName: azure-managed-disk-pvc - name: usermanagement-dbcreation-script configMap: name: usermanagement-dbcreation-script |
Explanation
This YAML code defines a Kubernetes Deployment for running a MySQL container. I’ve specified for only one replica MySQL container should be deployed, and I’ve provided configuration for mounting volumes and labels for selecting and managing the Pods. This MySQL container runs version 5.6 and is configured to use persistent storage and configuration from ConfigMaps.
persistent-volume-claim
Code
1 2 3 4 5 6 7 8 9 10 11 |
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: azure-managed-disk-pvc spec: accessModes: - ReadWriteOnce storageClassName: managed-premium resources: requests: storage: 5Gi |
Explanation:
This YAML code establishes a PersistentVolumeClaim (PVC) named “azure-managed-disk-pvc” with a 5-gigabyte storage request and read-write access for a single node. The PVC employs the “managed-premium” storage class, enabling other Kubernetes resources like pods to request and utilize the allocated storage.”
user-management-configmap
Code:
1 2 3 4 5 6 7 8 |
apiVersion: v1 kind: ConfigMap metadata: name: usermanagement-dbcreation-script data: mysql_usermgmt.sql: |- DROP DATABASE IF EXISTS webappdb; CREATE DATABASE webappdb; |
Explanation:
This YAML code defines a ConfigMap named “usermanagement-dbcreation-script” stores a SQL database creation script under the key “mysql_usermgmt.sql.” The script can be mounted into containers as a volume, allowing applications to access and use the SQL script for database initialization or configuration.
Deploy
to deploy the above yaml files, I issued the following command:
1 |
kubectl apply -f . |
the above command will deploy all the above files in the current working directory that I was working in.