Recently, Kubernetes and containerized applications have been increasingly popular for their efficiency, scalability, and ease of deployment. Azure provides a fully managed Kubernetes service (Azure Kubernetes Service, or AKS) that helps you quickly and easily define, deploy, debug, and upgrade even the most complex containerized applications.
If you want to use a MySQL database in your Azure Kubernetes application, there are three options:
Use Azure Database for MySQL – Take advantage of this fully managed MySQL service on Azure. This option enhances developer productivity by reducing the operational overhead of managing the MySQL server. While Azure Database for MySQL is available in two deployment modes – Flexible Server and Single Server, it is recommended to choose Flexible Server deployment option for all new developments.
Run it on Kubernetes – Provision a MySQL database in a container running on a Kubernetes pod. This option has the benefits that Kubernetes provides in terms of automation, but it demands developer efforts to keep the database running together. In addition, given the transient nature of Kubernetes pods, there is a higher likelihood of failovers and restarts which impacts application availability and business continuity.
Use MySQL on Azure VM – Azure VMs are considered an infrastructure as a service offering This option requires that the user take responsibility for managing and maintaining the MySQL server.
In this blog post, we’ll look more closely at the first option, discussing how you can deploy simple applications on AKS that integrate Azure Database for MySQL - Flexible Server on the backend in five simple and easy-to-follow steps!
Create a MySQL flexible server.
Prepare your application.
Create an Azure container registry and push the application to the registry as a Docker image.
Create an AKS cluster.
Deploy the application to the cluster and then test.
Note: This blog post provides a brief summary of the process. For detailed tutorials with sample applications and commands, see the following articles:
Along with choosing the right Compute, storage, backup, and high availability options, it’s important to decide the suitable networking option while creating the MySQL flexible server. Based on how you’d like AKS to reach the MySQL flexible server, you can choose either of the following options:
Public Access (allowed IP addresses)
Private Access (VNet integration)
Unless your scenario involves only small projects or demos, it’s highly recommended to create your server with private access, which will help secure access to your server via VNet Integration.
You can quickly create a flexible server in a VNet using the Azure CLI by running the following command:
Update your application code with the following changes:
To have the application use MySQL flexible server, modify the corresponding configuration file or application properties file to add code indicating the host server URL, database name, username, and password, which will be read from the environment variables (as defined in and passed down from the Kubernetes manifest file).
Create a DockerFile to build the application docker image.
For reference on how to prepare your application code for AKS and MySQL Flexible Server, see “Code the application” sections in these tutorials: Java SpringBoot | WordPress .
Step #3: Create an Azure container registry and push image to the registry
You can either build and deploy the docker image to Docker hub or use Azure Container Registry, a private container registry that allows you to securely build and deploy your applications and custom code.
To quickly create an Azure container registry, use the az acr create command, as shown below:
Next, create an AKS cluster in the VNet and attach the Azure container registry account to the cluster.
Before you create an AKS cluster, you first need to create a subnet for the cluster within the same VNet as the MySQL flexible server. The next thing to consider is whether to use kubenet networking or Azure CNI networking for the cluster. Based on your application and infrastructure requirements, this comparison chart can help you decide.
If you’re using Azure CNI, create the AKS cluster using the az aks create command, as shown below: