[MUSIC]
>> Hi. In this episode,
we'll go through another step by step tutorial,
this time to deploy an application on AKS integrating
with MySQL Flexible Server, isolated within VNets.
But before we jump into the tutorial,
let's understand the different ways in which you can
integrate Kubernetes applications with MySQL.
While this is true in general for all of Kubernetes,
I'll only be referencing to AKS in this episode.
There are typically three options you can choose from.
One is have your MySQL server
hosted on-premises or in a VM somewhere.
Now, in this case, the developer has to put in a lot of
efforts to manage the MySQL server.
The second option is to have MySQL running in
a container alongside your application in the Kubernetes pod.
Now, this is actually quite beneficial in the sense that
the developer doesn't need to leave
the Kubernetes plane to manage
the application as well as the database.
But again, it's a lot of efforts for
the developer to manage the MySQL server,
and because Kubernetes pods are transient in nature,
there is a high chance of restarts and failures that will happen,
which will affect the application availability.
The third option is to have MySQL
hosted on a managed database service on the Cloud,
like Azure Database for MySQL Flexible Server.
This reduces the overhead on developers heavily,
and it is quite easy to connect from
Kubernetes application to Azure Database for MySQL.
Now, one of our teams in Azure has actually gone a
step ahead and created something called Azure Service Operator,
which gives you the best of both worlds.
Just like you would provision Azure Database for
MySQL Flexible Server using Azure CLI Portal,
PowerShell, ARM templates,
Azure Service Operator is another way
to provision and manage flexible server.
But the great part is,
you can do that within the Kubernetes control plane itself.
All you need to do is define
the database configurations in a YAML file,
and it is as simple as doing a kubectl apply on the cluster,
and your database will be provisioned for you.
If you want to make any changes with the database,
you don't have to leave Kubernetes at all.
All you have to do is update
the Kubernetes YAML file and deploy it again.
In this episode, we'll only look at developing
applications with Azure Kubernetes service,
connecting with Azure Database for
MySQL Flexible Server in five simple, easy to follow steps.
For Azure Service Operator,
there is a link in the description below.
It takes you to a blog which gives you a detailed step by
step tutorial on how to
use ASO to provision Azure Database for MySQL.
Let's get started with the first step,
creating a MySQL Flexible Server with VNet integration.
We'll create a resource group, a VNet, a subnet, and the
flexible server with a database within that subnet.
Let's use the Azure CLI this time.
Let's go to Azure Portal and open the Cloud Shell,
and let's create a resource group in the Southcentral US region.
The next step is to create a VNet and a subnet.
Now, this subnet will be delegated
completely for MySQL Flexible Server only.
Let's make sure that we don't use this subnet for
any other resources because then there will be a conflict.
Once we have this VNet and subnet created,
the next step is to go ahead and create
a flexible server in that subnet.
All you have to do is an easy MySQL Flexible Server,
create past the parameters and we should be good to go.
Now, during the create,
they will ask you if you want to create a new private DNS zone,
so choose yes for this, I'll explain why later.
You're going to create a standard B1MS flexible server here.
Now that it is created, let's also create a demo database
for our application to connect to.
That concludes step 1 for us.
Step 2 is about preparing your application.
In my case, I'm going to be using a Java Spring Boot application.
The sample application is provided in the link below.
Just go to the GitHub site, either clone it or download it,
and go to the respective folder.
Since we are going to be using Maven for building our project,
go to the pom.xml file.
We'll be using the Jib Maven plugin to
actually build the image and send it
to the Azure Container Registry.
MySQL AKS demo registry will be the name
of the container registry we will create in the next step.
Make sure that these three properties are
up to date in the pom.xml file.
The next thing that you need to make sure is up to date is
the dependency on MySQL connector for Java.
The last one, and the most important one, is
the plugin itself for the Jib Maven.
Now let us look at the source code.
First, the application properties file
that you will put in the resources section of the source code.
Here, you'll have the URL, username,
and password for the MySQL Flexible Server.
You will be passing all these values
from the Kubernetes manifest file later on.
The other file that we'll look at is schema.sql file,
where we will put the entire database schema.
For my application,
it's a simple one table that I need with four columns.
It's just one table present in the schema,
but you will have more in your application.
Let's also look at the Java code themselves.
DemoApplication.java file, which has
the main function where the Spring Boot application will run from.
The todo.java file,
which will have the todo class to hold each of the records.
TodoRepository has the repository which Spring Data JDBC manages.
A TodoController to manage the storing and retrieving of
records when you do a post or get HTTP request.
Then finally the Dockerfile,
which will build the application image for you.
This image will be built on top of OpenJDK 8,
and the entry point to
this application will be the DemoApplication class.
Now that you have the code ready,
let's create an Azure container registry
and push the image to that registry.
Build and push the image.
To create an Azure container registry,
it's just an az acr create.
Let's use a basic SKU for now.
Once the ACR is created, go to your terminal.
Go to the springboot-mysql-aks folder.
Let's log in to ACR.
Let's compile the project and build it using jib.
The Jib Maven plugin will actually build
the project and also push the image to ACR.
Now that the build is successful,
let's go to Azure Portal and check out if it is reflecting that.
Let's go to the demoregistry ACR.
Go to Repositories section, and you can verify
that the image has been successfully built and deployed.
Now let's create an AKS cluster.
I'm going to create an AKS cluster in the same VNet.
I'll just be creating a different subnet for it.
You can create it in a separate VNet altogether,
then later you can peer the VNets together,
or we can use virtual network links.
I'll show you how to create a virtual network link as well.
Back in the Azure Cloud Shell,
let's create a new subnet for the AKS cluster.
Once that's created, let's actually store
that subnet ID to be used for cluster creation.
The cluster creation is going to be really simple.
Just put the name resource group,
and choose the right network plugin.
You can either choose Kubenet or you can choose
Azure CNI based on your infrastructure needs.
Then the service CIDR needs to be mentioned.
This address space must be different from
the virtual network address spaces that you have used so far.
The DNS service IP should be one of them.
The Docker bridge address should be
entirely different from both of these,
and you can then support the subnet ID and
attach the MySQL AKS demo registry as well.
Once this is completed,
let's go ahead and deploy the application to the cluster.
For this, let's use the Azure Portal itself
instead of using a kubectl commands.
Now, I've created my AKS in the same VNet as my flexible server.
But if you have created it in a different VNet,
go to the private DNS zone to set up a virtual network link.
We'll go to the MySQL private DNS zone.
Go to the Virtual Network Links section. Add a link.
It could be like mysql-kube, any name can be added here.
If you know the resource ID of
the virtual network, then that's great.
Otherwise, you can just pick it up from the dropdown here.
Check the "Enable Auto Registration" and click on "Okay".
This way, you can actually connect from
your Kubernetes cluster in the different VNet to the MySQL server.
You can also do VNet peering alternatively.
Now, in the AKS cluster,
let's go ahead and add our deployment and service.
Let's add with YAML, and paste the YAML code here.
We'll first create a deployment.
You can name the deployment however you wish,
but what is important is the containers in the specifications.
You will have to provide
the image that is hosted on the Azure container registry.
Also the database host, database username.
I think my username was Microsoft.
Then the database password,
and the database name as well.
The next thing is we will create a load balancer service for
the AKS cluster to be accessible over the Internet.
Let's click on "Add" and the deployment
as well as the servers will be created for you.
Let's give it a couple of minutes for
the cluster to be in a ready state.
You could probably refresh it at that time.
Once the deployment is ready,
go to "Services" section and make a note of the external IP.
What we'll do is we'll use a call command
to post one of the records.
Let's post one record of data, a simple congratulations,
you've deployed the application correctly command.
Let's see if that goes through.
We can retrieve the data from the browser as well.
Just click on the external IP and you
can see that you've successfully retrieved the data.
That ends our demo.
If you want to clean up the resources after your tutorial,
just go to the resource group
and delete the whole resource group,
or delete the resources individually.
That concludes our third tutorial on application development.
We learned how to deploy applications
using AKS and MySQL Flexible Server.
The resources for this episode are in the description below.
I really hope you found this episode useful.