In this post I’ll show you how to Deploy your first Service to Azure Container Services (AKS).

Prerequisites:

Create a resource group:

Firt create a Resource Group. Be aware that at the time of writing AKS is not available in all Azure regions.

1az group create -l westeurope -n aks

Create an Azure Container Registry:

Probably you’ll want a private registry to upload your docker images, so let’s create an Azure Container Registry instance.

1az acr create -g aks -n myregistry --sku Basic --admin-enabled

Login to the Azure Container Registry:

You can login to the Container Registry using the following Azure CLI command:

1az acr login -n myregistry

Or you can get the Key and login with docker:

1az acr credential show -n myregistry
2docker login myregistry.azurecr.io -u myregistry -p [PASSWORD FROM PREVIOUS COMMAND]

Push an image to the Azure Container Registry:

In this step we are going to pull an image from docker hub, and then upload it to the Container Registry created in step 2. Feel free to use your own docker image with a working web application.

1docker pull yaros1av/hello-core
2docker tag yaros1av/hello-core myregistry.azurecr.io/hello-core:1.0
3docker push myregistry.azurecr.io/hello-core:1.0

Create AKS cluster

Now create the AKS cluster:

1az aks create -g aks --name myAKSCluster --generate-ssh-keys

Install kubectl

To manage the cluster you’ll need to install kubectl so run the following command:

1az aks install-cli

Get cluster credentials

Get the cluster credentials and check the connectivity so you can start working with the AKS cluster.

1az aks get-credentials -g aks -n myAKSCluster
2kubectl get nodes

Create a Secret to hold the registry credentials.

You have to add the Azure Container Registry credentials to your AKS service in order to be able to pull the images. This is acomplished by creating a secret:

1kubectl create secret docker-registry [SECRET NAME] --docker-server=myregistry.azurecr.io --docker-username=myregistry --docker-password=[THE REGISTRY PASWORD FROM STEP 3] --docker-email=[EMAIL ADDRESS]

Check for the secret:

1kubectl describe secret

Create a kubernetes deployment definition.

Create a file [your deployment].yml with the following contents (replace the secret name):

 1apiVersion: apps/v1beta1
 2kind: Deployment
 3metadata:
 4  name: my-api
 5spec:
 6  replicas: 1
 7  template:
 8    metadata:
 9      labels:
10        app: my-api
11    spec:
12      containers:
13      - name: my-api
14        image: myregistry.azurecr.io/hello-core:1.0
15        ports:
16        - containerPort: 80
17      imagePullSecrets:
18      - name: [SECRET NAME]
19---
20apiVersion: v1
21kind: Service
22metadata:
23  name: my-api
24spec:
25  type: LoadBalancer
26  ports:
27  - port: 80
28  selector:
29    app: my-api

As you can see the file references the image pushed to the Container Service with the secret created in previous steps.

Deploy your service.

Run the following comnmand to deploy your service to AKS:

1kubectl create -f [your deployment].yml

Get the puplic IP for your service

To try your service you’ll need to get the public IP (It can take a while).

1kubectl.exe get service/my-api -w

Once you get the IP go ahead and test the service.

Update your deployment to different version of the image

If you need to update your service to use another version of the dokcer image run the following command:

1kubectl set image deployment/my-api [image name]=myregistry.azurecr.io/[image name]:[new version]

Add an autoscale rule

To add an autoscale rule to your service, run the following command:

1kubectl autoscale deployment my-api --min=2 --max=5 --cpu-percent=80

Hope it helps!