Deploy Java Microservices to Azure Kubernetes with Azure DevOps (CI+CD )
- Anand Nerurkar
- Sep 22, 2024
- 3 min read
Pre-Requisite:
Azure Account
Docker
Kubernetes file
ACR Set Up
Azure Kubernetes Cluster
kubectl
Azure DevOps Dashboard setup
Steps
====
Create Azure Kubernetes Cluster
login to azure portal
az login
Create a resource group
az group create --name myResourceGroup --location southcentralus
Create a AKS Cluster with 2 worker node
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 2
Display cluster details
az aks show --name myAKSCluster --resource-group myResourceGroup
Create a Azure Container Registry (ACR)
az acr create --resource-group myResourceGroup --name myacrrepo4321 --sku Standard --location southcentralus
Connect to Cluster
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster --overwrite-existing
Verify connect to cluster
kubectl get nodes
kubectl get deployments --all-namespaces=true
Please make sure that you have access to AzureDevOps pipeline
docker file is created
Make sure that AKS has pull access from ACR
When you're using Azure Container Registry (ACR) with Azure Kubernetes Service (AKS), an authentication mechanism needs to be established.
You can set up the AKS to ACR integration in a few simple commands with the Azure CLI or Azure PowerShell. This integration assigns the AcrPull role to the managed identity associated to the AKS Cluster.
For Deploying Docker images from ACR into AKS Cluster
az aks update -n myAKSCluster -g myResourceGroup --attach-acr myacrrepo4321
modify deployment files with updated image tag
Set Up DevOps Pipeline
1.set up build pipeline
2. set up release pipeline
Set up build pipeline
Step 1 - How to create a Azure Build Pipeline
1. Login into your Azure DevOps dashboard
2. Click on Pipelines.
3. Click on New Pipeline
4. Click on use the classic editor
Enter your repo name and branch name where you have stored your source code along with Dockerfile:
Click on Continue. Now choose the template by typing Docker, Select Docker container and Apply.
Now pipeline is created with two tasks already. We need to more tasks:
Let's add Maven build task for building the JAR file.
Click on + icon and type Maven
And then enter maven goal as package
Let's modify Build an image task.
Select Push an image task
Add a task for Copying YAML file, enter the Kubernetes deployment YAML file -
aks-deploy-from-acr.yaml
Add Publish artifact task
Now click Save + Queue and run to start Building the pipeline
Once the build is completed, you should be able to see the Docker images under
Services --> Repositories
Release Pipeline
How to Create Release pipeline for deploying Docker containers into AKS Cluster
Go to Pipelines --> Click on Releases --> New Release pipeline
Click on Stage 1 and choose a template by selecting
Deploy to a Kubernetes cluster and click on Apply
Change the stage name to Deploy to AKS
Now click on Add an artifact
Select the Build pipeline and click on the latest version
Now click on Deploy to AKS stage
Add Replace token task
Click on + to add task, type token and choose replace token task.
Now click on replace token task and Clik on root directory, click on ... dots
select the drop directory from below:
and enter Target file as aks-deploy-from-acr.yaml
Click on kubectl apply
Now Click on New to enter AKS cluster connection info
Choose the Azure subscription and enter Microsoft user credentials.
Select AKS cluster from the drop down, choose default namespace
Choose command as apply and select the yaml file from the dropdown from Configuration file
Now click on Save,
Click on Create a release
and then click Create to run the deployment
Click on Stage to see the logs
Now you will see the following tasks are in green to confirm Deployment was successful.
Let's check if deployment created any pods
kubectl get deployments
kubectl get pods
kubectl get svc
Now try to access spring boot application running inside AKS cluster by using external IP and port number
If you see any errors after deploying the pods, you can check the pod logs.
kubectl logs <pod_name>
Go to the browser enter http://external IP
Comments