AKS + Azure Database for MySQL – Flexible Server: Deploy applications in 5 easy steps!

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:

  1. 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.
  2. 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.
  3. 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!

  1. Create a MySQL flexible server.
  2. Prepare your application.
  3. Create an Azure container registry and push the application to the registry as a Docker image.
  4. Create an AKS cluster.
  5. 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:

 

Step #1: Create a MySQL flexible server

 

First, create a MySQL flexible server with the desired configuration by using the Azure Portal, the Azure CLI, an ARM template, or other Azure tools.

 

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:

 

 

 

az mysql flexible-server create \ --name <server-name> \ --resource-group <resource-group-name> \ --location <azure-region> \ --admin-user <your-admin-username> \ --admin-password <your-admin-password> \ --vnet <vnet-name> \ --subnet <mysql-subnet-name>

 

 

 

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.

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:

 

 

 

az acr create --resource-group <resource-group-name> \ --location <azure-region> \ --name <registryname> \ --sku <Basic/Classic/Premium/Standard>

 

 

 

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:

 

 

 

az aks create \ --resource-group <resource-group-name> \ --name <AKS-cluster-name> \ --network-plugin azure \ --service-cidr 10.0.0.0/16 \ --dns-service-ip 10.0.0.10 \ --docker-bridge-address 172.17.0.1/16 \ --vnet-subnet-id <AKS-subnet-ID> \ --attach-acr <registryname> \ --dns-name-prefix <AKS-DNS-prefix> \ --generate-ssh-keys

 

 

 

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

 

After the YAML file is ready, deploy it with kubectl apply or with the Kubernetes resource view via the Azure portal.

 

You should now be able to launch the application by opening a web browser and navigating to the Kubernetes service’s external IP.

 

Summary

 

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!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.