Hands on DevSecOps Coaching that is provided on AWS and Azure Cloud platforms. Contact Coach AK at devops.coaching@gmail.com for more info. You can also reach out to Coach AK at +1(469) 733-5248
We are going to learn how to deploy Springboot Microservices Docker container into Azure Kubernetes Cluster(AKS) using Helm and Azure pipelines.
Sample springboot App Code:
I have created a sample Springboot App setup in GitHub. Click here to access code base in GitHub.
Watch steps in YouTube channel:
What is Helm?
Helm is a package manager for Kubernetes. Helm is the K8s equivalent of yum or apt. It accomplishes the same goals as Linux system package managers like APT or YUM: managing the installation of applications and dependencies behind the scenes and hiding the complexity from the user.
Helm Charts
Helm uses a packaging format called Charts.A Helm Chart is a collection of files that describe a set of Kubernetes resources.Helm Charts helps you define, install, and upgrade even the most complex Kubernetes application. Charts are easy to create, version, share, and publish.
Implementation steps:
Create a resource group, AKS cluster and Azure container registry
Provide pull access for AKS to pull image from ACR
Create a namespace for helm deployment
Create a helm chart for spring boot app
Create a build pipeline to automate docker image
Customize pipeline with helm package tasks
Create a release pipeline
Customize pipeline with helm upgrade tasks
Run the pipeline to deploy springboot app into AKS
If you see any errors after deploying the pods, you can check the pod logs.
kubectl describe pod <pod_name> -n helm-deployment
Go to the browser enter http://localhost:8080
You should see below web page.
Clean up Resources
Let us see how to clean up the resources that were created. We can use az group delete command to remove the resource group, AKS cluster, and all related resources.
We can monitor AKS cluster using many ways. We will monitor AKS cluster using Azure Log Analytics workspace which collects log data and metrics from AKS cluster and stores them inside workspace. We will use Grafana for visualizing the data from Log Analytics workspace.
Azure Log Analytics workspace is a logical storage unit in Azure where all log data generated by Azure Monitors are stored. Log Analytics workspace collects log data from various Azure resources such as Azure Virtual machine or Azure Kubernetes Service. You can think of the workspace as a folder where all your monitoring data is stored and managed.
What is Grafana?
Grafana is an open source visualization and analytics tool.
It allows you to query, visualize, alert on, and explore your metrics no matter where they are stored.
Pre-requisites:
Azure subscription, click here if you don't have one.
You can also monitor existing AKS cluster as well, but make sure monitoring is enabled. once enabled, you can associate default workspace to AKS cluster or create a new workspace, associate it to cluster.
But we will try creating a new AKS cluster and monitor.
Create AKS Cluster with Monitoring Enabled
Make sure you are login to Azure portal first.
az login
enter your Microsoft credentials.
Create a resource group first
az group create --name myResourceGroup --location southcentralus
Create Log Analytics workspace
az monitor log-analytics workspace create --resource-group myResourceGroup \ --workspace-name my-loganalytics-workspace \ --query id \ -o tsv
Output of the above command will display log analytics Id which is needed for next command while creating AKS cluster. we will be associating Log Analytics Workspace ID with AKS Cluster during creation.
Create AKS cluster with 2 worker nodes with Monitoring Enabled
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 2 --enable-addons monitoring --workspace-resource-id /subscriptions/XXXXX/resourceGroups/myResourceGroup/providers/Microsoft.OperationalInsights/workspaces/aks-loganalytics-workspace
Verify all the resources are created in Azure Portal
Click on resource group name, you will see AKS cluster, log analytics workspace
Display Details of Cluster
az aks show --name myAKSCluster --resource-group myResourceGroup
The above command will display AKS Cluster details.
Connect to the cluster
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster --overwrite-existing
To verify the connection to your cluster, use the kubectl get command to return a list of the cluster nodes.
kubectl get nodes
Permission needed (SPN) to pull metrics Data from Azure Log Analytics
We need to create Service principal(SPN) and assign Log Analytics Reader Role on the AKS Cluster ResourceGroup. Execute below command to first get resource group ID where AKS cluster is running.
First Let's get Resource Group ID
az group show --name myResourceGroup --query id --output tsv
output of the above command will be resource group ID.
Create a Service Principal and Assign Role
az ad sp create-for-rbac --role="Log Analytics Reader" --scopes="/subscriptions/xxxxxx-xxxx-xxxx-xxxxx/resourceGroups/myResourceGroup"
Creating 'Log Analytics Reader' role assignment under scope '/subscriptions/XXX/resourceGroups/myResourceGroup'
Now Open localhost:3000 in browser to access Grafana
UserName: admin
Password: <from the above command>
Create Data Source in Grafana
Go to Grafana, configuration and click Data Sources. Click on Add Data Source and search for Azure Monitor.
Enter tenant id, app id and secret information. click on Load subscriptions. Click on Save and Test.
Create Dashboard in Grafana
In Grafana, we can create various kinds of dashboards as per our needs.
How to Create Azure Monitor For Containers Dashboard?
Click '+' button on left panel and select ‘Import’.
Enter 10956 dashboard id under Grafana.com Dashboard.
Click ‘Load’.
Click ‘Import’.
This will show monitoring dashboard for all cluster nodes
How to Create POD Metrics for AKS Dashboard?
Click '+' button on left panel and select ‘Import’.
Enter 14891 dashboard id under Grafana.com Dashboard.
Click ‘Load’.
This will show monitoring dashboard for all pods, you can also select namespaces as well.
Clean up Resources
Let's see how to clean up the resources that were created. We can use the az group delete command to remove the resource group, AKS cluster, and all related resources.
az group delete --name myResourceGroup --yes --no-wait
Click here for Script for Creating AKS cluster with Monitoring Enabled.