0
0
Azurecloud~5 mins

Deploying workloads to AKS in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Deploying workloads to AKS means putting your app or service into Azure Kubernetes Service so it can run and be managed easily. This helps you run your app in the cloud with automatic updates and scaling.
When you want to run a web app that can handle more users by adding more copies automatically.
When you need to update your app without stopping it for users.
When you want to run multiple parts of your app separately but together in the cloud.
When you want to use Azure's managed Kubernetes to avoid handling complex setup.
When you want to test your app in a cloud environment similar to production.
Config File - deployment.yaml
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
  labels:
    app: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: nginx:1.23
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

This file has two parts:

  • Deployment: It tells AKS to run 2 copies of an Nginx web server container labeled 'my-app'.
  • Service: It creates a LoadBalancer to expose the app on port 80 so users can reach it from the internet.
Commands
This command connects your local computer to the AKS cluster so you can control it using Kubernetes commands.
Terminal
az aks get-credentials --resource-group example-resource-group --name example-aks-cluster
Expected OutputExpected
Merged "example-aks-cluster" as current context in /home/user/.kube/config
--resource-group - Specifies the Azure resource group where the AKS cluster is located
--name - Specifies the name of the AKS cluster
This command tells AKS to create the deployment and service defined in the deployment.yaml file.
Terminal
kubectl apply -f deployment.yaml
Expected OutputExpected
deployment.apps/my-app-deployment created service/my-app-service created
-f - Specifies the file containing the Kubernetes configuration
This command shows the status of the running app copies (pods) to check if they started successfully.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-deployment-6d4b75f7b7-abcde 1/1 Running 0 30s my-app-deployment-6d4b75f7b7-fghij 1/1 Running 0 30s
This command shows the external IP address assigned to your app so you can access it from the internet.
Terminal
kubectl get service my-app-service
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-app-service LoadBalancer 10.0.123.45 52.170.12.34 80:31234/TCP 1m
Key Concept

If you remember nothing else from this pattern, remember: you create a deployment to run your app and a service to expose it to users.

Common Mistakes
Not running 'az aks get-credentials' before kubectl commands
kubectl won't know which cluster to talk to and commands will fail.
Always run 'az aks get-credentials' with the right resource group and cluster name first.
Using a Deployment without a Service to expose the app
Your app runs but cannot be reached from outside the cluster.
Create a Service of type LoadBalancer to get an external IP for your app.
Setting replicas to 0 in the deployment
No app copies will run, so your app will be unavailable.
Set replicas to at least 1 to keep your app running.
Summary
Use 'az aks get-credentials' to connect your local machine to the AKS cluster.
Apply your workload configuration with 'kubectl apply -f deployment.yaml' to create app pods and services.
Check pod status with 'kubectl get pods' to ensure your app is running.
Find your app's external IP with 'kubectl get service' to access it from the internet.