0
0
AzureHow-ToBeginner · 4 min read

How to Deploy to AKS in Azure: Simple Steps for Beginners

To deploy to AKS in Azure, first create an AKS cluster using the Azure CLI with az aks create. Then connect to the cluster using az aks get-credentials and deploy your app with kubectl apply -f using your Kubernetes manifest files.
📐

Syntax

Here are the main commands to deploy to AKS:

  • az aks create --resource-group <group> --name <cluster-name> --node-count <count> --enable-addons monitoring --generate-ssh-keys: Creates the AKS cluster.
  • az aks get-credentials --resource-group <group> --name <cluster-name>: Connects your local kubectl to the AKS cluster.
  • kubectl apply -f <deployment-file.yaml>: Deploys your app to the cluster.
bash
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 3 --enable-addons monitoring --generate-ssh-keys

az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

kubectl apply -f myapp-deployment.yaml
💻

Example

This example shows how to create an AKS cluster, connect to it, and deploy a simple nginx app.

bash
az group create --name myResourceGroup --location eastus

az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 2 --enable-addons monitoring --generate-ssh-keys

az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21.6
        ports:
        - containerPort: 80
EOF

kubectl get pods
Output
deployment.apps/nginx-deployment created NAME READY STATUS RESTARTS AGE nginx-deployment-5d8f9f7d7b-abc12 1/1 Running 0 30s nginx-deployment-5d8f9f7d7b-def34 1/1 Running 0 30s
⚠️

Common Pitfalls

Common mistakes when deploying to AKS include:

  • Not creating or selecting the correct Azure resource group before creating the cluster.
  • Forgetting to run az aks get-credentials to connect kubectl to your cluster.
  • Using invalid Kubernetes manifest files or missing required fields.
  • Not having the Azure CLI or kubectl installed and configured properly.
bash
## Wrong: Trying to deploy without connecting kubectl
kubectl apply -f myapp.yaml

## Right: Connect first then deploy
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
kubectl apply -f myapp.yaml
📊

Quick Reference

Remember these quick tips:

  • Always create or use an existing Azure resource group.
  • Use az aks create to make your cluster.
  • Connect your local kubectl with az aks get-credentials.
  • Deploy apps with kubectl apply -f and your YAML files.
  • Check pod status with kubectl get pods.

Key Takeaways

Create your AKS cluster with Azure CLI before deploying apps.
Connect kubectl to your AKS cluster using az aks get-credentials.
Deploy applications using kubectl apply with valid Kubernetes YAML files.
Always verify your pods are running with kubectl get pods.
Ensure Azure CLI and kubectl are installed and configured correctly.