Deploy Python App to Azure AKS Cluster by Azure Devops Pipeline

We will deploy python app to Azure AKS cluster by Azure Devops Pipeline. Go to your Collection files repository to upload files into repository. If this is your first repository then you need to click on Initialize and write a first file name to check in and create the repository. I have used on-premise Azure Devops pipeline in this article but if you want to use Azure Devops Pipeline in your azure cloud then you can apply same steps for that as well. There is no difference.

You can find my demo app, docker file and Kubernetes yaml files in below github url. Please remember to change file names as you desired and change in deployment shell script as well. We will use cloudflare for domain names management that’s why cert-request.yaml file is according to cloudflare. If you use another dns provider then you can find how to create cert request according to your own dns provider in cert manager official web page. You can find issuer in below cert manager url.

Python Application Codes:

https://github.com/semiharsan/pythondemoapp

Cert Manager Issuers:

https://cert-manager.io/docs/configuration/acme/dns01/

 

Let us create build pipeline to create artifacts means file container to use in release pipeline. We will create 2 different containers because we don’t want to upload yaml files into docker container that we will use in release pipeline. Please apply same steps as you see in below screenshots for your pipeline 😊

 

Let us enable continues integration to trigger automatically build pipeline if we change somethings in our app codes. We made sure only app codes changes will trigger the pipeline as we defined in “Path specification”. So build pipeline will run after checked in to create new container with updated files.

 

Now let us define service connection to give permission on-premise azure devops pipeline to be able to access to azure resources. You have to do same thing for your azure cloud devops as well. We already have done this step in another article which you can find its url at the end of this article. If you want to learn how to do it please have a look at that article.

 

We will use shell script in our release pipeline to build docker images and deploy it to Kubernetes cluster, so we need a linux agent to be able to run bash commands. If you will use Azure cloud devops pipeline then you just need to choose a linux hosted agent from Agent Pool.

If you will use on-premise azure devops pipeline then you need to install agent on a linux machine and az cli command tool. We already have installed agent in our Kubernetes cluster master node machine in our deploy aks cluster with terraform article which you can find its url at the and of this article.

You can install az cli command tool in your linux server that you have installed azure devops agent with below commands

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

az –version

 

We will need service principal information for azure login command and cloud flare api key,so create required secret variables in Library to use in pipeline step.

Azureuser : Your service principal account id

Azurepassword : Your service principal secret key that you created in app registration

Azuretenantid : Your azure tenant id

Cloudflare_api_key : Your cloud flare account api key

Cloudflare_email : Your cloud flare login email address.

 

Now go to releases and create a new release pipeline with your desired name. Add build pipeline as artifact and link azure login and cloudflare secret variable groups to this pipeline. Then go to pipeline variables to define docker image and kubernetes yaml variables to use in bash script. You can change names as you desired.

 

You need to choose a linux agent to be able to run bash commands. If you use Azure devops you can choose build in linux hosted agents. In our case we will chose our linux agent that we have installed on our Kubernetes master node linux server. You can find how to do it in the article url at the end of this article.

 

Let us add bash script steps from Add menu. I have separated 2 parts. One is only to build image and push it to Azure Acr Repository. Other one is only to deploy this image on Azure AKS cluster. Because we don’t want to upload yaml files into the image. That’s why we have created 2 different container with build pipeline and defined working folders accordingly for bash script steps.

https://raw.githubusercontent.com/semiharsan/devops/main/azuredevopspipelinepythonappbashscript

 

Pipeline is done and deployed python app to Azure AKS cluster. Come on let us test it. You just need to get ingress app public ip address and change your hostname domain record accordingly in your domain management. Or you can just add host record with below command to test it.

Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value ‘

20.162.145.136   pythondemo.arsit.tech

‘ -force -passthru

 

Let us enable continues deployment for release pipeline. If you change your code in your app repository then build pipeline will create a new updated artifact and then release pipeline will deploy it again with zero downtime as my bash script is written to do so. It will increase scale set 1 more and update pods with new image and delete old pods.

 

Let us change hello.py file and see what is going to happen 😊

deploy python app

 

Congrats guys !!! We have just deployed python application on Azure AKS cluster without knowing any python coding skills. You can visit our related articles about this.

 

<< GO TO SETUP AZURE AKS CLUSTER WITH TERRAFORM CODE >>

<< GO TO SETUP AZURE DEVOPS 2022 ON A WINDOWS 2022 SERVER VIRTUAL MACHINE >>

<< GO TO CONNECT ON-PREMISE AZURE DEVOPS TO AZURE CLOUD >>

<< GO TO HOW TO DEPLOY PYTHON APP MAIN POST >>