How to integrate Azure Container registry With Azure Kubernetes Services in three easy steps?

In any large-scale implementation of AKS (Azure Kubernetes Services), we need to use an image repository to store container images securely. So whenever you want to deploy the images on the Kubernetes cluster you will deploy the images stored in the image repository. In this article, we will learn how to integrate the Azure-based image repository called Azure Container Registry(ACR) with Azure Kubernetes Services(AKS) in the most simple manner.

https://www.youtube.com/watch?v=w4Q_Wt-MrkI

Step 1: Create Azure Container Registry and Azure Kubernetes Services

First, we will create ACR via Azure CLI. There are three options to use while creating the AKS instance:

While creating the AKS we will provide the –attach-acr parameter.

  • Option 1: If a new instance of AKS needs to be created then we will use the –attach-acr parameter while creating the AKS instance
  • Option 2: If AKS and ACR is in different subscriptions then we need to use resource Id in the –attach-acr parameter in this format: /subscriptions/resourceGroups/myContainerRegistryResourceGroup/providers/Microsoft.ContainerRegistry/registries/<containerRegistryName>
  • Option 3: If you have an existing AKS instance then we need to use az aks update with –attach-acr switch.
#login to Azure
az login

# Set variables
acrname=myacr07
rgname=aksdemo
aksclustername=aksdemo

# Create ACR instance
az acr create -n $acrname -g $rgname --sku basic

# Create an AKS cluster with ACR integration
az aks create -n $aksclustername -g rgname --generate-ssh-keys --attach-acr acrname

#If ACR is in the same subscription then use above format otherwise you need to use this format
az aks create -n $aksclustername -g $rgname --generate-ssh-keys --attach-acr /subscriptions/<subscription-id>/resourceGroups/myContainerRegistryResourceGroup/providers/Microsoft.ContainerRegistry/registries/<containerRegistryName>

#What if ACR is already created and now you want to do the integration then use this command
az aks update -n $aksclustername -g $rgname --attach-acr $acrname

Step 2: Import the image from docker.io

Now we have created ACR and AKS let’s import an image from docker.io to ACR and then deploy it to AKS.

az acr import  -n $acrname --source docker.io/library/nginx:latest --image nginx:v1

Step 3: Deploy the image into AKS and verify it.

In order to connect to AKS, we need to get the cluster credentials first, and then we can use the deployment YAML file to push the deployment to AKS. In the Deployment YAML file we will specify the registry name:

metadata:
  name: nginx0-deployment
  labels:
    app: nginx0-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx0
  template:
    metadata:
      labels:
        app: nginx0
    spec:
      containers:
      - name: nginx
        image: myacr07.azurecr.io/nginx:v1
        ports:
        - containerPort: 80

This will deploy the NGINX image to the AKS:

#Get AKS credentials
az aks get-credentials -g $rgname -n $aksclustername

#Apply the deployment file
kubectl apply -f  deploynginx.yaml

Hope you enjoyed reading it.

Leave a Reply

Your email address will not be published. Required fields are marked *