How to Install MySQL using GitHub Actions on Azure Kubernetes Service

Çiğdem Kadakoğlu
3 min readNov 3, 2023

MySQL-AKS-GitHub

Hi everyone, 🌼

I’ll show you how to install MySQL using GitHub actions on Azure Kubernetes Service in this article.

We need Kubernetes namespace, resource quota, storage class, persistent volume claim, deployment, and service yaml files.

First, let’s create the storage class K8S yaml file. We need this because MySQL data in “/var/lib/mysql” directory should be mounted with Azure file.

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: mysql-sc-azurefile
provisioner: file.csi.azure.com
allowVolumeExpansion: true
mountOptions:
- file_mode=0777
- mfsymlinks
- uid=999
- dir_mode=0777
- gid=999
- actimeo=30
- cache=strict
- nobrl
parameters:
skuName: Standard_LRS

Second, the persistent volume claim K8S yaml file will be used to specify how much space we will request from the storage class we created.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
storageClassName: mysql-sc-azurefile
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi

Third, let’s create deployment K8S yaml file. We’ll use “mysql:8.0” as a Docker image.

apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:8.0
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: 'test12345'
resources:
requests:
cpu: 500m
memory: 1Gi
limits:
cpu: 1
memory: 2Gi
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim

Next, the namespace and resource quota K8S yaml files created.

apiVersion: v1
kind: Namespace
metadata:
name: mysql-ns-rq
apiVersion: v1
kind: ResourceQuota
metadata:
name: mysql-ns-rq
spec:
hard:
requests.cpu: "1000m"
requests.memory: 2Gi
limits.cpu: "2000m"
limits.memory: 4Gi

Finally, we can create service K8S yaml file. In general, we use “ClusterIP” for MySQL service because it shouldn’t be accessed from outside the Kubernetes Cluster.

apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
type: ClusterIP
selector:
app: mysql

Now let’s create the GitHub repository and add these files.

The following step is to create GitHub actions.

First, we’ll connect to Azure from GitHub side.

- name: Checkout
uses: actions/checkout@v2

- name: 'Az CLI login'
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

- name: Azure Kubernetes set context
uses: Azure/aks-set-context@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
resource-group: ${{ secrets.AZURE_RESOURCE_GROUP }}
cluster-name: ${{ secrets.AZURE_CLUSTER_NAME }}

These are the secrets:

Here we can create a namespace and specify a resource quota for this namespace if you want. It’s up to you!

- name: Creating mysql-ns Namespace
uses: Azure/k8s-deploy@v1
with:
manifests: |
namespace.yml
action: deploy

- name: Creating Resource Quota for mysql-ns Namespace
uses: Azure/k8s-deploy@v1
with:
namespace: mysql-ns
manifests: |
resourcequota.yml
action: deploy

Now let’s create storage class with “storageclass.yml”.

- name: Creating Storage Class with Azure File to Azure Kubernetes Service
uses: Azure/k8s-deploy@v1
with:
namespace: mysql-ns
manifests: |
storageclass.yml
action: deploy

Next, we can install MySQL as below.

- name: Creating MySQL to Azure Kubernetes Service
uses: Azure/k8s-deploy@v1
with:
namespace: mysql-ns
manifests: |
persistentvolumeclaim.yml
deployment.yml
service.yml
action: deploy

It’s time to “run workflow”!

You can find the files on GitHub here:

Hope you find my article helpful. I wish everyone an awesome day!

--

--

Çiğdem Kadakoğlu

Senior Cloud&DevOps Engineer | Instructor | Docker Captain | Azure Solutions Architect | CK{A|AD}-AZ{104|204|305|400|800|801|900}-DP900-AI{900|102}-PL900🐧🐳🌼☕