Deploy Kubernetes cluster with container registry

Before starting you need to install the Azure CLI for windows.

Deploy AKS & ACR

You can deploy the ACR (Azure Container Registry) before deploying the AKS (Azure Kubernetes Service):

az acr create --resource-group RESOURCEGROUP  --name myContainerRegistry007 --sku Basic

To deploy the aks simple run the following script. The deployment may take 10-15 minutes.

az aks create -n myAKSCluster -g RESOURCEGROUP --generate-ssh-keys --attach-acr myContainerRegistry007

The attach-acr command creates a connection between the aks and the acr, this makes deployment of docker images from the registry possible.

If you want to connect the aks to the acr later or use a different acr, you can use this command:

az aks update -n myAKSCluster  -g RESOURCEGROUP --attach-acr myContainerRegistry007

Deploy ACR image to aks

I've already created an docker image and pushed it to the acr. Read more about this here.

Make sure you have the Kubernetes CLI installed, otherwise the kubectl command is not available.

az aks install-cli

The image is basically a new ASP.NET Core Web App with the Weatherforecast sample API.

To deploy an web app image to aks, you basically need to create 2 things in kubernetes:

  1. the web app
  2. a load balancer which handles the requests and redirect them to an aks pod.

This is the yaml for the webapp:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: containerweb-deployment
  labels:
    app: containerweb-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: containerweb
  template:
    metadata:
      labels:
        app: containerweb
    spec:
      containers:
      - name: containerweb
        image: myContainerRegistry007.azurecr.io/YOURIMAGENAME:47
        ports:
        - containerPort: 80

and this is for the loadbalancer:

# service.yml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: containerweb-aks
  name: containerweb-aks
spec:
  ports:
    - port: 8080
      protocol: TCP
      targetPort: 80
  selector:
    app: containerweb-deployment
  type: LoadBalancer

Let's start with getting the credentials from our aks:

az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

Now we need to apply the webapp.yaml file:

kubectl apply -f webapp.yaml

and afterswards the loadbalancer.yaml:

kubectl apply -f loadbalancer.yaml

This will deploy the container and the loadbalancer.

Read more here: https://docs.microsoft.com/en-us/azure/aks/tutorial-kubernetes-deploy-application


comments powered by Disqus