Deploying Applications on Kubernetes AKS via Terminal

Deploying Applications on Kubernetes AKS via Terminal

A Step-by-Step Tutorial for Deployment and Cleanup

·

6 min read

Introduction:

In this tutorial, we'll deploy an application on an AKS (Azure Kubernetes Service) cluster using only the terminal. AKS streamlines Kubernetes cluster management in Azure, making it easier to orchestrate containers. Let's dive into the process.

Prerequisites:

Before we dive in, ensure you have the following:

  • Azure CLI installed and configured.

  • Basic familiarity with Kubernetes concepts.

  • An application containerized and hosted on a Docker registry (e.g., Docker Hub).

Step 1: Creating a Resource Group

az group create --name rg-helloabhii --location eastus

We start by creating an Azure resource group to contain our AKS cluster and associated resources.

Step 2: Generating SSH Key (if needed)

ssh-keygen -t rsa -b 4096 -C "your_email_here@gmail.com"

Generate an SSH key pair if you haven't already. SSH keys are useful for secure communication with servers.

Step 3: Creating AKS Cluster

az aks create --resource-group rg-helloabhii \
--name myAKSCluster \
--node-count 1 \
--node-vm-size Standard_DS2_V2 \
--kubernetes-version 1.28.5 \
--generate-ssh-keys

Create an AKS cluster specifying details such as resource group, cluster name, node count, VM size, and Kubernetes version.

Step 4: Connecting to the Cluster

  az aks get-credentials --resource-group rg-helloabhii --name myAKSCluster

Connect to the AKS cluster using az aks get-credentials. This command configures kubectl to use of the AKS cluster.

Check if it connects or not try:

kubectl get all

if it returns like this means you are good to go further

➜  ~  kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   6m2s

Step 5: Deploying the Application

kubectl apply -f https://raw.githubusercontent.com/helloabhii/GoWebApp/kubernetes/DeployUsingK8s/go-deployment.yaml

Deploy the application using kubectl apply -f, providing the URL or local file path to the Kubernetes manifest file describing your application deployment.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gowebapp-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: gowebapp
  template:
    metadata:
      labels:
        app: gowebapp
    spec:
      containers:
      - name: gowebapp
        image: helloabhii/gowebapp:latest
        ports:
        - containerPort: 8080

Let's break down the components briefly:

  • apiVersion: Specifies the Kubernetes API version used (apps/v1).

  • kind: Defines the type of resource (Deployment).

  • metadata: Contains metadata like the Deployment's name.

  • spec: Specifies the desired state, including replica count, selector, and pod template.

    • replicas: Specifies the desired number of pods (3 in this case).

    • selector: Identifies managed pods via labels (app: gowebapp).

    • template: Defines pod creation template.

      • metadata: Assigns labels to created pods.

      • spec: Defines pod specifications.

        • containers: Lists containers in the pod.

          • name: Provides container name ("gowebapp").

          • image: Specifies Docker image ("helloabhii/gowebapp:latest").

          • ports: Exposes port 8080 within the container for external traffic.

deployment.apps/gowebapp-deployment created
➜  ~  kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
gowebapp-deployment   3/3     3            3           3m10s

Step 6: Creating a Service

kubectl apply -f https://raw.githubusercontent.com/helloabhii/GoWebApp/kubernetes/DeployUsingK8s/go-service.yaml
# Inside go-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: gowebapp-service
spec:
  selector:
    app: gowebapp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

Let's briefly explain the key components:

  • apiVersion: Specifies the version of the Kubernetes API being used.

  • kind: Defines the type of Kubernetes resource being created, in this case, a Service.

  • metadata: Contains metadata about the Service, including its name.

  • spec: Describes the desired state of the Service.

    • selector: Defines how the Service identifies which Pods to route traffic to. In this case, it selects Pods labeled with app: gowebapp.

    • ports: Specifies the ports on which the Service will listen for incoming traffic and where to forward that traffic.

      • protocol: Specifies the protocol used for the port, in this case, TCP.

      • port: Defines the port number on which the Service will listen for incoming traffic, which is port 80 in this example.

      • targetPort: Specifies the port number on the Pods to which incoming traffic should be forwarded, which is port 8080 in this example.

    • type: Specifies the type of Service. Here, LoadBalancer is used, which requests a cloud provider's load balancer to distribute incoming traffic to the Pods in the Service.

Create a Kubernetes Service to expose your application to external traffic, listening on port 80 and forwarding traffic to port 8080 on the pods.

Step 7: Watching the Service

kubectl get svc -w
➜  ~  kubectl get svc -w   
NAME               TYPE           CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
gowebapp-service   LoadBalancer   10.0.50.34   <pending>     80:31442/TCP   6s
kubernetes         ClusterIP      10.0.0.1     <none>        443/TCP        17m
gowebapp-service   LoadBalancer   10.0.50.34   4.157.238.191   80:31442/TCP   11s

Monitor the creation of the service, waiting until it gets an external IP address assigned.

Step 8: Accessing the Application

Once the service has an external IP, you can access your application using that IP.

Step 9: Checking Resources

kubectl get pods -w
kubectl get replicaset
➜  GoWebApp git:(kubernetes/DeployUsingK8s) ✗ kubectl get pods -w                                       
NAME                                  READY   STATUS    RESTARTS   AGE
gowebapp-deployment-55b6d6794-2q7g4   1/1     Running   0          33m
gowebapp-deployment-55b6d6794-d4rtx   1/1     Running   0          33m
gowebapp-deployment-55b6d6794-h5rsv   1/1     Running   0          33m
➜  GoWebApp git:(kubernetes/DeployUsingK8s) ✗ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY   AGE
gowebapp-deployment-55b6d6794   3         3         3       29m

Check various Kubernetes resources you've created, such as pods and replicasets.

Step 10: Scaling Up the Deployment

kubectl scale --replicas=10 deployment/gowebapp-deployment
➜  GoWebApp git:(kubernetes/DeployUsingK8s) ✗ kubectl scale --replicas=10 deployment/gowebapp-deployment

deployment.apps/gowebapp-deployment scaled
➜  GoWebApp git:(kubernetes/DeployUsingK8s) ✗ kubectl get pods -w                                       
NAME                                  READY   STATUS    RESTARTS   AGE
gowebapp-deployment-55b6d6794-22cjt   1/1     Running   0          4s
gowebapp-deployment-55b6d6794-2mmsz   1/1     Running   0          4s
gowebapp-deployment-55b6d6794-2q7g4   1/1     Running   0          32m
gowebapp-deployment-55b6d6794-d4rtx   1/1     Running   0          32m
gowebapp-deployment-55b6d6794-gcwn9   1/1     Running   0          4s
gowebapp-deployment-55b6d6794-h5rsv   1/1     Running   0          32m
gowebapp-deployment-55b6d6794-hfgng   1/1     Running   0          4s
gowebapp-deployment-55b6d6794-nds7k   1/1     Running   0          4s
gowebapp-deployment-55b6d6794-q65qs   1/1     Running   0          4s
gowebapp-deployment-55b6d6794-w6hzp   1/1     Running   0          4s

Demonstrate scaling the application by increasing the number of replicas in the deployment from 3 to 10.

Step 11: Clearing Resources

# Delete the service
kubectl delete service gowebapp-service

# Delete the deployment
kubectl delete deployment gowebapp-deployment

# Delete the AKS cluster
az aks delete --resource-group rg-helloabhii --name myAKSCluster --yes

# Delete the resource group
az group delete --name rg-helloabhii --yes

# If there are any remaining resources, delete them manually using the Azure portal or CLI

# Once verified, confirm that the resource group has been deleted
az group list --output table
#if still running delet them as well as we do earlier

Always double-check resource deletion to avoid any unexpected costs or resource clutter.

Conclusion:

This tutorial walks you through deploying an application on a Kubernetes AKS cluster via the terminal. With these steps, you can efficiently manage your applications on Kubernetes in Azure. Now you're ready to deploy confidently. Happy deploying!

Additionally, feel free to reach out to me on LinkedIn, Docker Hub, Twitter, Hashnode, and Reddit, or view more of my projects on GitHub.

Did you find this article valuable?

Support Abhishek by becoming a sponsor. Any amount is appreciated!