App Modernization with AKS – Part 2, pull image

Post View : 3851

After we have successfully built the image and deployed to the ACR, we will need to setup our AKS to pull the correct image from ACR.

Creating/Setup your Azure resources

If you haven’t created your AKS, you can run the following command on PowerShell:

# set this to the name of your Azure Container Registry.
$ACR = 'YourContainerRegistry'

# Create an AKS cluster with ACR integration
New-AzAksCluster -Name myAKSCluster -ResourceGroupName myResourceGroup -GenerateSshKey -AcrNameToAttach $ACR

Please ensure your ACR name, resource group and your AKS Cluster.

If you already have an AKS running on your Azure resource group, and you would like to attach the ACR to your existing AKS. You may try the following command:

Set-AzAksCluster -Name myAKSCluster -ResourceGroupName myResourceGroup -AcrNameToAttach <acr-name>

Remember to change the name of your ACR.

After you have created both ACR and AKS, connected both of them together, we are ready to go for deploying our application to the AKS now.

Prepare Image and Deployment

We can import our container image to the ACR by running the following command:

Import-AzContainerRegistryImage -RegistryName <acr-name> -ResourceGroupName myResourceGroup -SourceRegistryUri docker.io -SourceImage library/nginx:latest

Ensure the ACR name, resource group and source image is correct.

  • docker.io: You should replace it with your own image source URI.
  • library/nginx: In this example we are using nginx as the runtime environment.

After imported the image, we are ready to deploy the image. First of all, you need to config the AKS credential:

Import-AzAksCredential -ResourceGroupName myResourceGroup -Name myAKSCluster

Create a file called acr-nginx.yaml:

apiVersion: apps/v1
kind: Deployment
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: <acr-name>.azurecr.io/nginx:v1
        ports:
        - containerPort: 80

This file is for the AKS to following the configuration of your environment, remember to change your ACR name in the yaml file.

In addition, in this yaml file we are using port 80 as example, if your service is using different port like port 8080, 8443 remember to update the port number.

After saving the file, we need to run the following command for applying the config:

kubectl apply -f acr-nginx.yaml

If you wish to view the deployment, run the following command:

kubectl get pods

You should be able to view your running pods:

NAME                                 READY   STATUS    RESTARTS   AGE
nginx0-deployment-669dfc4d4b-x74kr   1/1     Running   0          20s
nginx0-deployment-669dfc4d4b-xdpd6   1/1     Running   0          20s
Chris Wan
Website | + posts

Microsoft Certified Trainer (MCT)
Application Architect, SOS Group Limited

Leave a comment

SOS Group Limited © 2022. All Rights Reserved.