Introduction
In my recent three articles, I developed a REST API with Azure Functions using SQL database and containerized Azure Functions Apps using Docker Desktop and created a CI/CD pipeline for my containerized Azure Functions App. In this article, I will create CI/CD pipeline for the Azure Kubernetes cluster because I want to do an automatic build and release process. If any developer wants to make a change in the code, then my pipelines will run automatically based on the branch name trigger. I already have pushed this application source code to Azure Repo. This is a public repo and anyone can access and clone the code. I will use the Azure Container Registry service for docker image hosting and I will use Azure Kubernetes service for container deployments.
Prerequisites
You are required to have intermediate-level knowledge of Azure Container Registry and Azure Kubernetes services and Azure DevOps.
Scheme
- Create Azure Resource Group
- Create Azure Container Registry
- Create Azure Kubernetes Cluster
- Create a Pipeline For Deployment to Kubernetes
Let's get started,
Log in to the Azure portal or log in through Azure CLI. I will show you both ways. Open your command prompt and use the following command in order to login to Azure. Make sure you already have installed the Azure CLI on your local.
az login
After logging in, you can see your all active subscriptions in the output and you need to set the subscription for this current context to do so use the following command. I have multiple subscriptions and I will use my Azure pass sponsorship for this context.
az account set --subscription "Azure Pass - Sponsorship"
Step 1 - Create an Azure Resource Group
As you know, we already logged in using CLI. Now we will create a resource group for our docker images and Kubernetes cluster. We will keep our all resources in this resource group that we are creating. Use the following command in order to create the resource group. If you don't have Azure CLI installed on your local you can follow the Azure Portal steps as shown below.
az group create --name "azurefunction-rg" --location "centralus"
az group list --output table
Step 2 - Create Azure Container Registry
We have created a resource group. Now for every new resource, we will add to this resource group. Use the following command to create the Azure container registry for hosting docker images. This is a paid service, but you can also use a free hosting service like Docker Hub.
(Azure Container Registry Name : azurefuncAcr)
az acr create --resource-group azurefunction-rg --name azurefuncAcr --sku Basic
As shown above, you can see that we have successfully created the Azure container registry.
Step 3 - Create Azure Kubernetes Cluster
Click on create a resource and choose Kubernetes Service.
Select your subscription and resource group. Enter your cluster name and other options leave it to default, and then simply click on Review and Create.
We successfully created our Azure Kubernetes cluster.
Step 4 - Create a Pipeline For Deployment to Kubernetes
Login to your DevOps accounts and create a new project with the name Azure Function App.
Open this newly created project and add source code to this project repo. You can also use the existing source code that I mention on top of this article. I already have the source code in this project.
Next, click on the Pipeline tab and then choose the pipelines. This pipeline will build the source code and then create the docker image and will push to the Azure container registry that we created in the last step and create a new deployment to the Azure Kubernetes cluster. Click on create pipeline and choose the source code option. As I mentioned earlier that, I am using Azure Repos for my source code. I will select the Azure repo git option.
Select your repository and then branch. In my case, I will choose Azure Function App.
Next, select the Deploy to Azure Kubernetes Service (build and push the image to Azure Container Registry; Deploy to Azure Kubernetes Service) option from the Configure your pipeline page.
Next, select your active subscription from pop up and hit continue. In my case, I have two subscriptions and I select the (Azure Pass - Sponsorship). Enter your Azure account credentials and click sign in.
Next, select your Cluster name from the dropdown and choose the Namespace (existing or new), and select the container registry and enter the image name that you want to use. The service Port option leaves it with a default value. Finally, click on validate and configure.
Next, you will see an azure-pipelines.yml file that is a predefined template for building and pushing images to the Azure container registry and deploying them to the Kubernetes cluster. Just update the branch trigger name main to master or any branch name that you want to use for this build pipeline. Now we are good to go and we can save this pipeline as-is. Click on save and run.
- # Deploy to Azure Kubernetes Service
- # Build and push the image to Azure Container Registry; Deploy to Azure Kubernetes Service
- # https://docs.microsoft.com/azure/devops/pipelines/languages/docker
- trigger:
- - master
- resources:
- - repo: self
- variables:
- # Container registry service connection established during pipeline creation
- dockerRegistryServiceConnection: '65de1c2f-d191-4f20-b6cb-15201e44d415'
- imageRepository: 'azurefunctionapp'
- containerRegistry: 'learningimagescontainerregistry.azurecr.io'
- dockerfilePath: '**/Dockerfile'
- tag: '$(Build.BuildId)'
- imagePullSecret: 'learningimagescontainerregistry1592bca3-auth'
- # Agent VM image name
- vmImageName: 'ubuntu-latest'
- stages:
- - stage: Build
- displayName: Build stage
- jobs:
- - job: Build
- displayName: Build
- pool:
- vmImage: $(vmImageName)
- steps:
- - task: Docker@2
- displayName: Build and push an image to container registry
- inputs:
- command: buildAndPush
- repository: $(imageRepository)
- dockerfile: $(dockerfilePath)
- containerRegistry: $(dockerRegistryServiceConnection)
- tags: |
- $(tag)
- - upload: manifests
- artifact: manifests
- - stage: Deploy
- displayName: Deploy stage
- dependsOn: Build
- jobs:
- - deployment: Deploy
- displayName: Deploy
- pool:
- vmImage: $(vmImageName)
- environment: 'AzureFunctionApp-1436.default'
- strategy:
- runOnce:
- deploy:
- steps:
- - task: KubernetesManifest@0
- displayName: Create imagePullSecret
- inputs:
- action: createSecret
- secretName: $(imagePullSecret)
- dockerRegistryEndpoint: $(dockerRegistryServiceConnection)
- - task: KubernetesManifest@0
- displayName: Deploy to Kubernetes cluster
- inputs:
- action: deploy
- manifests: |
- $(Pipeline.Workspace)/manifests/deployment.yml
- $(Pipeline.Workspace)/manifests/service.yml
- imagePullSecrets: |
- $(imagePullSecret)
- containers: |
- $(containerRegistry)/$(imageRepository):$(tag)
Here you will see in the output popup the three files will be added to our repository (azure-pipeline, deployment.yml, service.yml) choose the commit directly to the master branch option and hit Save and Run.
After Save and Run, you will see two stages. In the first stage, our code will be built and the docker image will be created and pushed to the Azure container registry. In the second stage, it will be deployed to the Kubernetes cluster.
The process of building an image and pushing it to the Azure container registry is in progress.
The stage of building an image and pushing it to the Azure container registry is done.
The process of deploying to the Kubernetes cluster is in progress.
The stage of deploying to the Kubernetes cluster is done.
Open your Kubernetes cluster and from the left panel click on Services and ingresses. Here you will see a new service named azurefuncapp is running under the default namespace with its External IP address that we will use for the public.
Grab the External IP address of your service and open it in any browser. Amazing, the Azure Kubernetes cluster is up and running in the Azure portal. So, this was the process of building a CI/CD pipeline for Azure Kubernetes Cluster.