Deploy Python App to On-premise IIS Server and Kubernetes Cluster by Azure Devops Pipeline

We will deploy python app to on-premise Window IIS server and Kubernetes cluster by Azure Devops Pipeline. We will use Nginx load balancer in front of IIS and K8S. Nginx will handle incoming requests and ssl certificate. Nginx is installed in Kubernetes cluster master node. You can install with “apt update” and “apt install nginx” commands in Linux server. Even for production environment you can install Nginx for public face load balancer to same server with Kubernetes cluster master node.

If this is your first sign in Azure Portal to create Azure cloud resources then follow below steps to create your Azure Devops environment. Otherwise, you can skip to creating pipelines step. Go to Azure Portal home page and click on All Services and then Azure Devops organizations to create first devops organization and project. On next screen Click on “My Azure Devops Organizations” to forward to next step.

 

Choose “Default Directory” under your profile and click on “Create new organization” button to create your first and new organization. Then click on “Continue” on next screen. Choose your projects location to host and click on “Continue”.

 

Write a project name as you desired and choose “Private” to make your repository private access. If you want it public access repository then you can of course choose “Public”.

 

Now go to “Files” under Repos in left panel menu in your project dashboard and click on “Initialize” to create your repository for this project.

 

You can find all files in below GitHub repository. I wrote linux bash script steps into one file name “kubernetesbashscript.sh”. You need to separate it to pipeline steps accordingly. It is up to you. You can also use script in one step as well.

Python Application and Pipeline Script Codes:

https://github.com/semiharsan/pythonapp-to-iis-k8s

 

We have used Azure Devops Pipeline cloud version to deploy python application to on-premise IIS server and Kubernetes cluster for this blog post. So, we need to define on-premise IIS and Kubernetes cluster master node servers as Deployment Group to Azure Devops so that Azure Devops upload files into these servers and run scripts.

Deployment Group server means Azure Devops will install an azure devops agent into this server and run scripts using this agent. If you want, you can use your server directly as Azure Devops Hosted Agent Server as well. This means you can save your repository files directly in your server instead of Azure Devops cloud storage account. This is the most specific difference between Azure Hosted Agent and Deployment Group.

Go to Deployment Group under Pipelines menu on left panel and click on “Add a deployment group” button to add your servers as deployment group. Write a desired name and description for this deployment group and click on “Create”.

 

Choose “Windows” as target type and tick on “Use a personal access token” so that you don’t need to do anything for authentication between your server and azure devops. Pipeline will automatically create a token and give you powershell script along with access token. You just need to click on “Copy script to the clipboard” button and paste the script to your server powershell session.

 

Connect with rdp or powershell session to your IIS server and paste the script that you copied into the powershell session to install azure devops agent.

 

We will do same steps for Kubernetes cluster master node but we need to make a small change in agent script so that we can install it with root user. Even if you will install with another user, no problem to do this changeset for script file. Click on Add a deployment group again in Deployment groups menu. This time choose “Linux” as target type. Tick on “Use a personal access token” and click to Copy clipboard button.

 

Paste the code into an editor to separate downloading azure agent files and installing script. We will download agent files to be able to change “config.sh” file before we install agent with root user. After you downloaded agent files, open “config.sh” with nano or vi editor to comment “exit 1” line as you can see in next screen shot.

 

Now you can paste rest of the script from your editor to install agent with root user.

 

And Kubernetes master node agent is online along with IIS server agent. Now we can deploy anything onto these servers via Azure Devops pipeline by using deployment group task. Before to create pipelines we need to install python to IIS server. Run below script to install python. If you install python manually please remember to change pipeline variables for python install path. You can find script in below GitHub url.

https://raw.githubusercontent.com/semiharsan/pythonapp-to-iis-k8s/main/InstallPythonToWindows.ps1

After python installation is finished, run below commands as well to install fast-cgi for IIS and its python dependency. If you don’t have IIS role in your server please install it via Windows Server Management tool.

Pip install wfastcgi

Install-WindowsFeature -name web-cgi

 

 

IIS is ready to serve for python applications. Let us go to Pipeline and create our pipeline task steps to copy repository files into pipeline containers. Please apply same things as I showed in red boxes in below screen shots. Of course, you can change names as you desired. Publish Artifact step settings are default and you can only change artifact name, that’s why I took only one screen shot for Publish Artifact steps.

 

Enable trigger and add only Python application files folder to deploy application automatically after a new code checked in. Click on “Save and Queue” button to create repository container after you have finished.

 

It is time to create release pipelines to deploy python application to on-premise IIS and Kubernetes cluster. Go to Releases and click on “Create New Release”. Then write a name for your release pipeline and click on Add button to add artifact.

 

You need to write whole variables as you see in screen shot. If you change variables names, please remember to change same variable names in powershell and bash scripts as well. You need to add Docker username and password as secret from Library to Variable Group as we used Docker Hub to save our docker image. Remember to change target in Deployment Group Job steps.

 

We have used Nginx as public face to welcome incoming requests and redirect them to IIS and Kubernetes Pods. You can see in below screen shots that nginx redirect first to Kubernetes Service and second to IIS server.

deploy python app

 

Congrats guys. We have used Azure Devops cloud pipeline and deployed python application on IIS server along with Kubernetes Cluster. You can check out our blog posts about deploying python app on Azure AKS or AWS EKS.

<< 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 >>