Search This Blog

Kubernetes basic commands - Azure Kubernetes Service

Below commands shows how to ocreate AKS cluster with Azure cloud, deploy the application and finally delete complete infrastructure.

Pre-Requisite

Commands

Setup Azure Account on CLI

#Login to Azure
az login
#This will open browser window and ask for Azure account credentials. Confirm Azure account details using az account show command

az account show

Register Providers

Run below command to remove some errors by registering appropriate provides.

az provider register --namespace Microsoft.Network
az provider register --namespace Microsoft.Compute
az provider register --namespace Microsoft.OperationsManagement

Locations

Check all available locations associated with your subscription and choose the nearest one with you or decide based on your different priorities and preferences.

az account list locations

Create a resource group to group and Azure resources. Good practice to manage resources.

az group create --name aks --location southindia

Setup Docker & Regsitry

az acr create -g aks -l southindia -n mehtaacr --sku basic

sudo docker build -t mehtaacr.azurecr.io/ex/demo:1.0

sudo docker tag mehtaacr.azurecr.io/ex/demo:1.0

az acr login --name mehtaacr

sudo docker push mehtaacr.azurecr.io/ex/demo:1.0

Create AKS 

az vm availability-set list-sizes

az vm list-sizes

az aks create -g aks -n demo-cluster --node-vm-size=Standard_B2s --generate-ssh-keys

Create a Service Principal & assign the appropriate role

az ad sp <service principal name> --skip-assignmenet

CLIENT_ID=$(az aks show -g aks -n demo-cluster --query "servicePrincipalProfile.clientId" --output tsv)
echo $CLIENT_ID
ACR_ID=$(az acr show -g aks -n mehtaacr --query "id" --output tsv)
echo @ACR_ID

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

Multi-Cluster Connections

# Windows config file location, in this file server settings, might be pointing to local Kubernetes server
cat c:\users\<username>\.kube\config | more

# Run below command to point it to AKS

az aks get-credentials -g aks -n demo-cluster

Play with Pods

kubectl edit pod <podname> --opens a VI editor
kubectl describe pod <podname>
kubectl get all
kubectl get nodes
kubectl get pods

Deployment

kubectl create deployment demo-app --image=mehtaacr.azurecr.io/ex/demo:1.0

kubectl expose deployment demo-app --type=LoadBalancer --port 5000 --target-port 5000

kubectl get service

Access the application on IP address of service and port exposed.
http://20.44.40.86:5000/

kubectl get pods
kubectl get service --watch

# CTRL + C to stop watch

Scaling

az aks scale -g aks -n demo-cluster -c 1
kubectl scale deployment demo-app --replicas=3

Update Image

docker build -t mehtaacr.azurecr.io/ex/demo:2.0 .
sudo docker push mehtaacr.azurecr.io/ex/demo:2.0

kubectl set image deployment/demo-app demo=mehtaacr.azurecr.io/ex/demo:2.0

az acr build -t ex/demo:2.0 -r mehtaacr -f dockerfile .

kubectl get pods -o wide

Monitoring

az aks enable-addons -a monitoring -g aks -n demo-cluster

Delete resources

kubectl delete service demo-app

az aks delete -g aks -n demo-cluster

az acr repository delete -n mehtaacr --image ex/demo:2.0

az group delete -n aks