Getting started with Azure Kubernetes Services - Step by Step - 2

Other posts in this series:  Getting started with Kubernetes

Test locally using docker-compose

Clone the application using:

git clone https://github.com/Azure-Samples/azure-voting-app-redis.git

Change directory:

cd azure-voting-app-redis

Start the application locally using:

docker-compose up -d

When you run http://localhost:8080, you should be able to see a page. Run the following to view all running containers:

docker ps -a

Setup an Azure Container Registry

To setup an Azure Container Registry, also known as acr, you can issue the following command:

az acr create --resource-group <RESOURCE_GROUP_NAME> --name <CONTAINER_REGISTRY_NAME> --sku Basic

If you already have an acr, you can view the list using:

az acr list
[
  {
    "adminUserEnabled": false,
...
    "loginServer": "<ACRNAME>.azurecr.io",
...
    "type": "Microsoft.ContainerRegistry/registries"
  }
]

Note down the loginServer details.

Push an Image to acr

Now, to push an image, you must login to the acr:

az acr login --name <ACR_NAME_ONLY>

You can use the docker tag command like so:

docker tag azure-vote-front <ACR_NAME>/azure-vote-front:v1

All set, let's push (it might take a while for the first time):

docker push <ACR_NAME>/azure-vote-front:v1

You have a private registry now with the images that you can use to build your containers in the AKS cluster. Do ensure to give ONLY the name and not the full address like ACR_NAME.azurecr.io.

az acr repository list --name <ACR_NAME_ONLY>

To view tags for a repository, you can use:

az acr repository show-tags --name <ACR_NAME_ONLY> --repository <REPOSITORY_NAME>

Create a Service Principal

To create a service principal, you can issue the following command:

az ad sp create-for-rbac --skip-assignment

You will get an output like so:

{
  "appId": "###",
  "displayName": "###",
  "name": "###",
  "password": "###",
  "tenant": "###"
}

Fetch the ID

az acr show --resource-group <RESOURCE_GROUP_NAME> --name <ACR_NAME_ONLY> --query "id"

To grant the correct access for the AKS cluster to pull images stored in ACR, assign the AcrPull role using the az role assignment create command.

az role assignment create --assignee <APP_ID> --scope <ID_RETRIEVED_FROM_PREVIOUS_STEP> --role acrpull

Create a Kubernetes Cluster

If you are starting afresh, you can use the following to create a cluster:

az aks create \
    --resource-group myResourceGroup \
    --name myAKSCluster \
    --node-count 2 \
    --service-principal <appId> \
    --client-secret <password> \
    --generate-ssh-keys

If you have already created an AKS cluster, you can simply update the credentials using:

az aks update-credentials \
    --resource-group myResourceGroup \
    --name myAKSCluster \
    --reset-service-principal \
    --service-principal <appId> \
    --client-secret <password>

Update loginServer information and apply configuration

Update the loginServer information in azure-vote-all-in-one-redis.yaml

image: <loginServer>.azurecr.io/azure-vote-front:v1

And now, to Apply your configuration run the following:

kubectl apply -f azure-vote-all-in-one-redis.yaml

If all goes well, you will have a couple of pods running.

kubectl get pods                                                                                                                      
NAME                                READY   STATUS    RESTARTS   AGE
azure-vote-back-847fc9bcb9-lphbm    1/1     Running   0          7m5s
azure-vote-front-675fb4bffc-xtjk4   1/1     Running   0          7m4s

To view the services, you can run:


kubectl get services
kls                                                     
NAME               TYPE           CLUSTER-IP    EXTERNAL-IP      PORT(S)        AGE
azure-vote-back    ClusterIP      10.0.47.228   <none>           6379/TCP       8m
azure-vote-front   LoadBalancer   10.0.16.192   52.163.218.201   80:30480/TCP   8m

Hit the server with the External-IP and you should be good!

Update the cluster with latest code

For a simple updation do as follows:

$ vi azure-vote/azure-vote/config_file.cfg
# UI Configurations
TITLE = 'Azure Voting App - v2'
VOTE1VALUE = 'Cats'
VOTE2VALUE = 'Dogs'
SHOWHOST = 'false'

I have just added - v2 in the 2nd line. Let's test the code locally by building the image, then push the code with a different tag manually.

docker-compose build
docker tag azure-vote-front <ACR_NAME_ONLY>.azurecr.io/azure-vote-front:v2
docker push <ACR_NAME_ONLY>.azurecr.io/azure-vote-front:v2

This time, the push will be super-fast.

Script this task if you like

Repeating the same tasks again is not fun. How can we do it better? Let me show you an extremely simplistic script. Create a file called deploy.sh and give it appropriate permissions chmod 755 deploy.sh. After that, copy/paste the following and execute:

#!/bin/bash

echo 1. Building Docker Image
echo ========================
docker-compose build
echo
echo 2. Find the commit id
echo =====================
COMMIT="$(git rev-parse --short HEAD)"
echo $COMMIT
echo
echo 3. Tag Docker Image
echo ===================
docker tag azure-vote-front <ACR_NAME_ONLY>.azurecr.io/azure-vote-front:$COMMIT
echo Successfully tagged Docker image as <ACR_NAME_ONLY>.azurecr.io/azure-vote-front:$COMMIT
echo
echo 4. Push Docker Image
echo ====================
az acr login --name <ACR_NAME_ONLY>
docker push <ACR_NAME_ONLY>.azurecr.io/azure-vote-front:$COMMIT
echo Successfully pushed Docker image to ACR
az acr repository show-tags --name <ACR_NAME_ONLY> --repository azure-vote-front
echo
echo 5. Updating deployment
echo ======================
sed "s/VERSION/$COMMIT/g" azure-vote-all-in-one-redis.yaml > deployment-latest.yaml
kubectl apply -f deployment-latest.yaml

The moment you run this script, if everything goes well, you will see a new set of pods spinning up based on updated images with your new code.

Hope this helps!

What next?

Well, stay tuned for upcoming articles. You may contact us at contact@attosol.com for your software and consultancy requirements.

© 2023, Attosol Private Ltd. All Rights Reserved.