Deploying your containerized app to Azure Kubernetes Service with Jenkins CI/CD Pipeline and GitHub Webhook - Part 5
This is the last part of the Jenkins CI/CD Pipeline series, where we will be deploying our containerized app to Azure Kubernetes Service. In case you havent gone through the previous tutorials of this series, I highly recommend you review them before proceeding with this one for better continuity and understanding.
Create a Jenkins Linux VM HERE.
Install Docker Engine on the Azure VM that is running Jenkins HERE.
Manually Deploying your containerized Application to an Azure Web App HERE.
Automatically Deploying your containerized Application to an Azure Web App HERE.
Here are quick steps that we shall follow here for this tutorial:
Prepare your GitHub repository with the Application Code.
Deploy a sample NodeJS Application to an AKS cluster.
Create a basic Jenkins project.
Set up credentials for Jenkins to interact with ACR.
Create a Jenkins build job and GitHub webhook for automated builds.
Test the CI/CD Jenkins pipeline to update the application in AKS based on GitHub code commits.
Prerequisites
Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
Jenkins - Install Jenkins on a Linux VM
Azure CLI: Install Azure CLI (version 2.0.67 or higher) on the Jenkins server.
Sample Application Code can be found at my Github Repository HERE
Create your Fully Automated CI/CD Jenkins Pipeline
GitHub Repository with Sample Application Code
Create a new GitHub repository with your application code. Sample Application Code can be found at my Github Repository HERE
GitHub Webhook
We will need to create a webhook for the GitHub repository that should be able to remotely trigger your builds jobs in Jenkins everytime the application changes are commited and pushed to the above application code repository.
We will define the Jenkins Pipeline with the Pipeline script from SCM method as shown below
This should now allow the GitHub repository webhook to remote trigger the Build Jobs for the above pipeline in Jenkins, whenever you commit changes to your application code hosted on GitHub.
Deploy a sample NodeJS Application to an AKS cluster
Let us login to the Azure Portal >> Search >> Azure Kubernetes Servies >> Create Kubernetes Cluster with configurations as shown below
Click Add node pool >> Give the pool a name (nplinux OR npwindows) with User Mode and Node Size as D2s_v3 >> Add >> Review and Create
Connecting to the AKS Cluster
Let us now launch the Azure Cloud Shell. The Cloud Shell has the kubectl pre-installed.
az aks get-credentials --resource-group aks_RG267 --name aks_demo267
This command downloads credentials and configures the Kubernetes CLI to use them. To verify our connection with the AKS Cluster, let us run the following
kubectl get nodes
Execute the below command to get the kubeconfig info, we will need to copy the entire content of the file to txt file that we will use to create a Jenkins Credentials with a secret file and name it "aks_secret.txt"
cat ~/.kube/config
How to verify integration between ACR and AKS Cluster?
az role assignment list --scope /subscriptions//resourceGroups//providers/Microsoft.ContainerRegistry/registries/ -o table
az role assignment list --scope /subscriptions//resourceGroups/jenkins267/providers/Microsoft.ContainerRegistry/registries/acr267 -o table
Make sure that the output of the above command is not empty. If empty run the following command. For AKS to pull the Container images from the ACR, you will need to integrate your ACR with the AKS cluster using the az aks update command with the attach acr parameter and a valid value for acr-name or acr-resource-id. This should configure necessary permissions for AKS to access the ACR
az aks update --resource-group --name --attach-acr
OR
az aks update --resource-group --name --attach-acr
az aks update --resource-group aks_RG267 --name aks_demo267 --attach-acr acr267
Create a Secret File credential in Jenkins
We will now create a Secret File credential in Jenkins with the secret text file "aks_secret.txt" that we had create earlier. This will allow Jenkins to connect to the AKS Cluster and update the Image Version for the deployment that is already up and running.
Deploy a sample NodeJS Application to the AKS cluster
To deploy the application, you use a manifest file to create all the objects required to run the AKS Store application. A Kubernetes manifest file defines a cluster's desired state, such as which container images to run. The manifest includes the following Kubernetes deployments and services.
In the Cloud Shell, let us donwload the sample repository files
git clone https://github.com/mfkhan267/jenkins_on_azure2024.git
cd deployments
kubectl apply -f svc-lb.yml
kubectl apply -f deploy-complete.yml
Test the initial base Application
Check for a public IP address for the web-deploy application in the load balancer service. Monitor progress using the kubectl get service command with the --watch argument.
kubectl get service ps-lb
Once the EXTERNAL-IP address (public IP) is available for the load balancer service, open a web browser to the external IP address of your service to see the your sample NodeJS app is up and running.
Commit and Push your application code changes to the Application repository on GitHub
Commit and push your changes to your application code on GitHub and the GitHub webhook should trigger the Jenkins Pipeline Build automatically. The Jenkins Pipeline will automatically Build, Push and Deploy your containerized Application to the Azure Kubernetes Service Cluster as part of your complete CI/CD Job. The GitHub webhook should now trigger the Jenkins Pipeline Job and the Build Job should run automatically.
Wait until the Jenkins Pipeline build job is over. A graphic below the Build History heading indicates that the job is being executed or completed. Go to the job build and console output to see the results.
Congratulations! You have successfully Deployed your containerized app to your Azure Web App. Your Azure Web App should now be running your newly generated docker image with your App Code
Testing your Web App that should now be running your newly generated docker image with your App Code
Below is Application v13 running as a Deployment on the AKS Cluster (your build version may differ)
The Application should now be running the new Build v14 as the recently modified deployment on the AKS Cluster (your build version may differ)
That's all folks. Hope you enjoyed the Jenkins series with Docker, Azure Container Registry, Azure WebApp and Azure Kubernetes Service. Kindly share with the community. Until I see you next time. Cheers !
Next steps
Subscribe to my newsletter
Read articles from ferozekhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by