This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Tech Community.
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:
- Deploy Java Spring Boot Application on AKS cluster with MySQL - Flexible Server within a VNet
- Deploy WordPress on AKS cluster with MySQL - Flexible Server
Step #1: Create a MySQL flexible server
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:
The subnet specified will be delegated for use only with your MySQL flexible server. Be sure that the subnet doesn’t include any other resources.
Note: If you prefer to use public access networking instead, be sure that you’ve created firewall rules to allow either all Azure services or specific services, such as the AKS cluster.
You can also create databases for your application by using the CLI command: az mysql flexible-server db create
Step #2: Prepare your application
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.
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:
Now, build the application image and push it to this registry using Docker CLI (docker build, docker tag, and docker push), ACR tasks, or developer tools such as the Maven Jib plugin for containerized Java applications.
Step #4: Create an AKS cluster
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:
Step #5: Deploy the application to the cluster
To deploy the application to the AKS cluster, you first need to create a Kubernetes manifest file that defines a desired state for the cluster, such as what container images to run.
Key things to define in the Kubernetes manifest YAML file include:
- Container image name: replace it with your own in the format [registryname].azurecr.io/[image-name]:[tag]
- Environment variables for MySQL flexible server host URL, database name, admin username and password.
- A Service resource to access the application in the cluster. A service of the type “LoadBalancer” will create an external load balancer providing an externally accessible IP address to the application
You should now be able to launch the application by opening a web browser and navigating to the Kubernetes service’s external IP.
We hope this five-step guide simplifies the deployment process and helps you quickly get started with building your applications on AKS and Azure Database for MySQL – Flexible Server.
If you’re a new developer or Azure user seeking a quick, affordable way to develop/deploy apps on Azure, create an Azure free account today. With an Azure free account, you can now try Azure Database for MySQL - Flexible Server for free for 12 months!
If you have any questions, feedback, or suggestions on what else you’d like us to write about, please leave a comment below or email us at AskAzureDBforMySQL@service.microsoft.com. Thank you!