When you're using Azure Container Registry (ACR) with Azure Kubernetes Service (AKS), an authentication mechanism needs to be established.
To allow an AKS cluster to interact with ACR, an Azure Active Directory managed identity is used.
Create Resource Group
Make sure you are login to Azure portal first.
az login
You need to create a resource group first.
az group create --name myResourceGroup --location southcentralus
Create AKS cluster with 2 worker nodes
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 2 --enable-addons monitoring --generate-ssh-keys
az aks show --name myAKSCluster --resource-group myResourceGroup
The above command should display Cluster exists in Azure portal
Create Azure Container Registry
Run the below command to create your own private container registry using Azure Container Registry (ACR).
az acr create --resource-group myResourceGroup --name myacrrepo31 --sku Standard --location southcentralus
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
For Deploying Docker images from ACR into AKS Cluster
The following command allows you to authorize an existing ACR in your subscription and configures the appropriate ACRPull role for the managed identity.
az aks update -n myAKSCluster -g myResourceGroup --attach-acr myacrrepo31
To Detach ACR from AKS use below command
az aks update -n myAKSCluster -g myResourceGroup --detach-acr myacrrepo31
Hashicorp's Terraform is an open-source tool for provisioning and managing cloud infrastructure. Terraform can provision resources on any cloud platform.
Terraform allows you to create infrastructure in configuration files(tf files) that describe the topology of cloud resources. These resources include virtual machines, storage accounts, and networking interfaces. The Terraform CLI provides a simple mechanism to deploy and version the configuration files to Azure.
Watch the steps in YouTube:
Advantages of using Terraform:
Reduce manual human errors while deploying and managing infrastructure.
Deploys the same template multiple times to create identical development, test, and production environments.
Reduces the cost of development and test environments by creating them on-demand.
How to Authenticate with Azure?
Terraform can authenticate with Azure in many ways, in this example we will use Azure CLI to authenticate with Azure and then we will create resources using Terraform.
The above command will open the browser and will ask your Microsoft account details. Once you logged in, you can see the account info by executing below command:
az account list
Now create a directory to store Terraform files.
mkdir tf-acr
cd tf-acr
Let's create a terraform file to use azure provider. To configure Terraform to use the Default Subscription defined in the Azure CLI, use the below cod.
We will learn how to automate Docker builds using Jenkins. We will use
Python based application. I have already created a repo with source code
+ Dockerfile. We will see how to create Docker image and upload into Azure Container Registry (ACR) successfully.
Pre-requisites:
1. Jenkins is up and running
2. Docker installed on Jenkins instance. Click here to for integrating Docker and Jenkins
3. Docker and Docker pipelines plug-in are installed
4. Create credentials entry for Jenkins for connecting to ACR
5. Repo created in ACR, Click here to know how to do that.
6. port 8096 is opened up in firewall rules.
Step # 1 - Create a pipeline in Jenkins
Name as myACRDockerPipelineJob
Step # 2 - Copy the pipeline code from below
Make sure you change red highlighted values below:
Your a should be updated and repo should be updated. pipeline { agent any
environment { //once you create ACR in Azure cloud, use that here registryName = "myakacrregistry/my-python-app" //- update your credentials ID after creating credentials for connecting to ACR registryCredential = 'ACR' dockerImage = '' registryUrl = 'myakacrregistry.azurecr.io' }