How to Connect the Azure Kubernetes Cluster Using Cloud Shell or Azure CLI

In this blog we are going to discuss how to connect the Azure Kubernetes cluster. We have two options to create cluster using azure cli & using azure portal. Microsoft azure is a cloud platform. It provides a multiple services such as virtual network ,Kubernetes services, containers & many more. its easy to use.

Option 1: Using Cloud Shell.

  • Click on created Azure Kubernetes Cluster.
  • Click on Cloud Shell,we can see the option on top of our screen.

  • Click on Connect option.

  • Run the following command on azure bash shell (cloud shell).

az account set --subscription <subscription-id>

For example: az account set --subscription a8178ec0-3306-4767-8c2a-e99cb5ad94d9

az aks get-credentials --resource-groups <name of resource group> --name <name of cluster>

For example: az aks get-credentials --Testing --name example

  • Run the command to get the Nodes running in our cluster.

kubectl get nodes

  • Here is the command output.

Option 2: Using Azure cli.

az group create --name <ResourceGroup-name> --location <location-name>

For example : az group create --name Testing --location eastus

  • Here is the command output.

{
"id": "/subscriptions/a8178ec0-3306-4767-8c2a-e99cb5ad94d9/resourceGroups/Testing",
"location": "eastus",
"managedBy": null,
"name": "Testing",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}

  • To Enable cluster monitoring.

az provider show -n Microsoft.OperationsManagement -o table
az provider show -n Microsoft.OperationalInsights -o table

  • Here is the command output.

root@ip-172-321-416-40:/home/ubuntu# az provider show -n Microsoft.OperationsManagement -o table
Namespace RegistrationPolicy RegistrationState
------------------------------ -------------------- -------------------
Microsoft.OperationsManagement RegistrationRequired Registered
root@ip-172-321-416-40:/home/ubuntu# az provider show -n Microsoft.OperationalInsights -o table
Namespace RegistrationPolicy RegistrationState
----------------------------- -------------------- -------------------
Microsoft.OperationalInsights RegistrationRequired Registered

  • If they are not registered,run the following commands:

az provider register --namespace Microsoft.OperationsManagement
az provider register --namespace Microsoft.OperationalInsights

  • To Create a Kubernetes cluster with a specific version.

For example:

az aks create -g <Resourcegroup-name> -n  <cluster-name> --kubernetes-version <kubernetes-version>

  • To Create a Kubernetes cluster with a node pool.

az aks create -g <Resourcegroup-name> -n <cluster-name> --node-count 3

  • To install kubectl using the az aks install-cli command.

az aks install-cli

  • To configure kubectl to connect to Kubernetes cluster.

az aks get-credentials --resource-group <Resourcegroup-name> --name <cluster-name>

  • Run the command to get the Nodes running in our cluster.

kubectl get nodes

Run the application

  • Create a .yaml file.

vim test.yaml

  • Add the following code:

---
apiVersion: v1
kind: Namespace
metadata:
name: apache-1
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: apache-1
name: deployment-1
spec:
selector:
matchLabels:
app.kubernetes.io/name: app-1
replicas: 3
template:
metadata:
labels:
app.kubernetes.io/name: app-1
spec:
containers:
- image: docker-image-url
imagePullPolicy: Always
name: app-1
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
namespace: apache-1
name: service-1
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
app.kubernetes.io/name: app-1
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
namespace: apache-1
name: ingress-1
annotations:
kubernetes.io/ingress.class: alb
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: service-1
servicePort: 80

  • Deploy the application.

kubectl apply -f test.yaml

  • To delete the deployment.

kubectl delete -f test.yaml

Leave a Reply