Programming Chunks

Explore Python And Data Science with me!!

Home ยป The Ultimate guide for deploying Flask backend to Aks-Part1

The Ultimate guide for deploying Flask backend to Aks-Part1

So, how do you start deploying Flask backend to Aks? Lately I am doing my shopify which has a backend in flask and I wanted to deploy that to cloud to get rid of local server run. So, I devised this deployment strategy to get this work done.

Creating The Resources In Azure for for deploying Flask backend to Aks

The above is the resources you may need to set up your python backend on the azure cloud for deploying Flask backend to Aks.

All in all, you need a Virtual machine to host the CICD pipeline tool (Jenkins), a k8cluster to which the code would be deployed, a resource group to combine all these resources together, a subscription on azure and a disk storage for storing artifacts.

You can either use terraform to create these resources or use the azure UI to create the above mentioned resources.

For a walkthrough of the azure platform, you can refer the below mentioned video for deploying Flask backend to Aks.


Basically, the networking in azure comprises of creating a virtual network, and these virtual network comprises of small segments of subnets, which can be again either private or public. Now resources attached to the public subnets are accessible through the internet and those attached with the private subnets can access the Internet with NAT (Network Address Translation) gateway that is present in the public subnet.

Apart from that there are routing tables which directs the traffic from source to target, there are as well network security groups. It restricts inbound and outbound network traffic based on the IP address, port and protocol.

Basically, here what we will be doing to create a virtual private network, and attach a virtual machine to it with a internet facing subnet. This VM would be used for setting up the CICD tool- Jenkins.

Apart from that, we have to add a container registry, and a Kubernetes service cluster as a resource to the same resource group.

Configuring Jenkins On the VM

The next step is to configure Jenkins on the VM. This is probably the simplest stuff in the entire process. You can get the commands for installing Jenkins on a virtual machine from internet. In my it was Ubuntu on which I set up my Jenkins server. Here is a link to setting up Jenkins on VM. I am not going into deep into this because all the set ups are available in the microsoft official website.

The sole of this post is to define a guideline to deploying backend python services to the cloud. You can open the port 8080 in the inbound security rules and take the public IP of the VM and access Jenkins UI on that port.

Connecting Jenkins on VM to Kubernetes

Next is to configure Jenkins running on VM to Kubernetes cluster which is running in a different location but belonging to the same resource group.

Login to the Jenkins server and go to the Manage Jenkins tab.

First thing first, we have to create a few bunch of credentials in Jenkins.

  • Set up credentials for GitHub with username/password
  • Set up container registry credentials for connecting to azure registry

Then install some important plugins:

  • Kubernetes Plugin
  • Kubernetes CLI plugin
  • Github Plugin
  • Docker Plugin
  • Docker pipeline Plugin
  • Azure Container Registry Tasks Plugin

After creating the credentials, it time to set up Jenkins on the cloud so that Kubernetes cluster can talk to Jenkins server. Go to the azure Kubernetes cluster and get the API server address and put it in the Kubernetes URL. For now disable https check, we can reconfigure it later. As a credentials give the config secret that you get from serviceaccount configuration. Test the connection. It should succeed. The credentials should be appropriate for this connection to succeed.

Connecting Docker to Jenkins

After creating the credentials, and all, it’s time to connect the Jenkins to the docker so that pipeline can run the docker command. This is by far the most difficult thing that took hours for me to achieve.

Provided all the required plugin are installed, we have to do couple of things.

  • set up the docker host uri
  • expose docker port
  • configure docker agent template
  • connect method configuration

Getting the docker host URI was a really confusing task. So logically, we are running Jenkins on which docker is supposed to run on the VM which is essentially a Ubuntu machine. So there are many ways to get the host IP address, I used “ip add” command to get the IP address.

For the port we have to do some changes in the dockerd configuration file. Here are the command you have to execute once you ssh into the VM. (We are sshing to the VM because our Jenkins and hence docker also is installed in it)

sudo nano /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H tcp:// -H unix:///var/run/docker.sock
sudo systemctl daemon-reload
sudo service docker restart
curl http://localhost:4243/version

After having that done, we have to configure the connection method.

For this, we have to use a open source image which is jenkins/agent and attach docker container to it.The Jenkins remote agent code will be copied into the container and then run using the Java that’s installed in the container.

After doing all of these configuration, we can run docker commands in the Jenkins pipeline.

Creating The Pipeline script of Jenkins For Deployment

Now comes the part where we create the Jenkins pipeline that will actually deploy the image to Kubernetes. I will give the stage you have to run through.

Creating Dockerfile for deployment

Next creating a dockerfile that will actually build the image to be deploying using deployment files on the cluster through Jenkins CICD pipeline. Try to build the docker file in two stages, one is builder and other is final stage.

  • Set the working directory
  • Install the system dependencies
  • Copy the requirements file from the local directory to the remote working directory in the container
  • set all the environment variables and expose the container port
  • Run the gunicorn command to run the flask app

You can use these hints to form your own docker file which will basically, copy the local directory, establish a connection to database and run the server for flask

Creating k8 deployment and services with helm installation

The last step is to create the deployment files, which also includes creation of the service. For this you have to first install the azure cli in the Jenkins server which is running on the VM.

The format of the deployment mainly takes care of the matching labels. For this you have to be able to run kubectl command on the Jenkins server, For this you may want to create a secret id and client id in azure portal to get the app registered and use the credential to login to the Kubernetes cluster running on a different location on the Jenkins server.

Lastly, create the service as a load balancer type to access the service outside the cluster.

You can also use helm charts to achieve this, and all of these can be put at this step in CICD pipeline in the deploy stage for automation. If you want to know more helm chart you can refer this article.

Leave a Reply

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

Back to top