Working with Azure Container Registry and Azure Kubernetes Service

Working with ACR and AKS​​ 

 

 

System Requirement

  • Windows 10​​ 

  • Visual Studio 2017​​ 

  • .Net Core tools

  • AZ Cross platform tools

  • Enable Hyper-V

  • Install docker for windows​​ 

https://docs.docker.com/docker-for-windows/install/

 

 

 

 

 

 

 

  • Create a .Net Core​​ Web Api App from the Visual Studio by File -> New -> Project -> .Net Core -> Asp.Net Core Web Application and name it coresample.

 ​​ ​​ ​​​​ 

​​ 

 

  • ​​ Click on Web API and Enable Docker Support and select OS and Linux.​​ 

 

 

 

 

 

 

  • Once the project is created build the solution and then publish the coresample project.

 

  • In the publish wizard, select the folder option and give the folder path as “bin\Release\PublishOutput”

 

  • Once the publish is completed, Open the powershell and cd to the project directory and build the Docker image. Before running it make sure to move out the Docker​​ file to the​​ project root directory from the ​​ coresample folder.

CMD :​​ docker build -t coresample .

 

 

 

 

  • Run the image locally by the following cmd.

 

Docker run –it –p 8082:80​​ coresample

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Here, we are running the image locally and then mapping the internal port 8082 to external port 80. ​​ 

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Open the brower and hit the localhost:8082/api/values to see the api is running.  ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ 

 

  • Create azure container registry by passing the appropriate values to the name and create a new resource group. Select the location where you want to host your cluster and make sure to enable the azure user and select the SKU as basic.

 

  • Create the AKS cluster

 

az aks create --resource-group​​ coresample​​ --name​​ coresamplecluster​​ --node-count 1 --generate-ssh-keys

 

  • Allow​​ 

az​​ aks​​ get-credentials --resource-group​​ coresample​​ --name​​ coresamplecluster

  • Create a service account and give it permission​​ 

 

#!/bin/bash

 

$AKS_RESOURCE_GROUP=​​ ”coresample”

$AKS_CLUSTER_NAME=”coresamplecluster”

$ACR_RESOURCE_GROUP=”coresample”

$ACR_NAME=“coresampleregistry”

 

# Get the id of the service principal configured for AKS

$CLIENT_ID=$(az aks show --resource-group $AKS_RESOURCE_GROUP --name $AKS_CLUSTER_NAME --query "servicePrincipalProfile.clientId" --output tsv)

 

# Get the ACR registry resource id

$ACR_ID=$(az acr show --name $ACR_NAME --resource-group $ACR_RESOURCE_GROUP --query "id" --output tsv)

 

# Create role assignment

az role assignment create --assignee $CLIENT_ID --role owner --scope $ACR_ID

 

 

  • Login to ACR

 

Docker login coresampleregistry.azurecr.io

Take the username and password from the access keys from portal.​​ 

 

  • Push Image to ACR

Tag the image to ACR name and repo

Docker tag coresample coresampleregistry.azurecr.io/coresample:v1

 

Docker push coresampleregistry.azurecr.io/coresample:v1

 

  •  ​​​​ Go to the portal and check if the repo got push or not.

 

  • Create a deploy.yml file and add the following​​ please update the name of the registry accordingly

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

  name: coresample-deployment

spec:

  replicas: 3  

  minReadySeconds: 10

  strategy:

    type: RollingUpdate

    rollingUpdate:

      maxUnavailable: 1

      maxSurge: 1 

  template:

    metadata:

      labels:

        app: mywebapi

    spec:

      containers:

      - name: coresample

        image: coresample.azurecr.io/coresample        

        ports:

        - containerPort: 80

        imagePullPolicy: Always   

 

  • Go to powershell and run the following command​​ 

Kubectl create –f deploy.yml

 

  • Create the service yml ​​ file and name it deployservice.yml

apiVersion: v1

kind: Service

metadata:

 ​​​​ name: coresampleservice

spec:

 ​​​​ ports:

 ​​ ​​ ​​​​ - port: 80

 ​​​​ selector:

 ​​ ​​ ​​​​ app: coresample

 ​​​​ type: LoadBalancer

  • Deploy the yml of the service

Kubectl create –f deployservice.yml​​ –save-configx

 

  • Run the Kubectl Proxy to open the dashboard​​ 

19) Or, we can run the kubectl get svc to see the service status and take the IP address.

20) Take the api address and append /api/values to check if the api is running.

 

Comments

comments

One thought on “Working with Azure Container Registry and Azure Kubernetes Service

  • November 4, 2018 at 9:36 am
    Permalink

    I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to design my own blog and would like to find out where u got this from. many thanks

    Reply

Leave a Reply

Your email address will not be published.