Azure Kubernetes Service (AKS) GitOps Tutorial

Automate your Kubernetes deployments with GitOps.

Introduction to GitOps with Azure AKS

This tutorial will guide you through setting up and using GitOps to manage your Azure Kubernetes Service (AKS) clusters. GitOps is a paradigm that uses Git as the single source of truth for declarative infrastructure and applications. By applying GitOps principles, you can achieve faster, more reliable, and auditable deployments.

What You'll Learn

Tip: Ensure you have an Azure subscription and the Azure CLI installed and configured before starting.

Prerequisites

Before you begin, make sure you have the following:

Step 1: Create an Azure Kubernetes Service (AKS) Cluster

1

Create a Resource Group and AKS Cluster

First, create a resource group for your AKS cluster.


az group create --name myAKSResourceGroup --location eastus
                

Now, create the AKS cluster. This might take several minutes.


az aks create --resource-group myAKSResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys
                
Note: For production environments, consider using a higher node count and configuring advanced networking options.
2

Configure kubectl for AKS

Connect kubectl to your AKS cluster by downloading its credentials.


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

Verify the connection by listing the nodes in your cluster:


kubectl get nodes
                

Step 2: Set Up Your Git Repository

3

Create a New Git Repository

Create a new repository on your Git provider (e.g., GitHub, Azure Repos). This repository will store your Kubernetes manifests and Flux CD configurations.

Clone the repository locally:


git clone 
cd 
                
4

Create a Kubernetes Manifest Directory

Inside your cloned repository, create a directory to hold your Kubernetes manifests. A common convention is clusters/myAKSCluster/.


mkdir -p clusters/myAKSCluster
                

Commit and push these changes:


git add .
git commit -m "Initial repository setup"
git push origin main
                

Step 3: Install and Configure Flux CD

5

Install Flux CLI

Download and install the Flux CLI. Follow the official Flux documentation for the latest installation instructions: Flux CD Installation.

6

Bootstrap Flux CD on AKS

Bootstrap Flux CD on your AKS cluster, pointing it to your Git repository. Replace with your actual repository URL.


flux bootstrap git \
  --url= \
  --branch=main \
  --path=./clusters/myAKSCluster \
  --namespace=flux-system
                
Warning: Ensure your Git repository is accessible by Flux. For private repositories, you'll need to configure SSH keys or access tokens during the bootstrap process.

This command will install the necessary Flux controllers into your cluster and configure them to watch your specified Git path.

Step 4: Deploy an Application using GitOps

7

Create an Application Manifest

Create a simple Nginx deployment manifest file inside your Git repository's managed path (e.g., clusters/myAKSCluster/nginx-deployment.yaml).


apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer
                
8

Commit and Push the Manifest

Add the new manifest to your Git repository, commit it, and push it.


git add clusters/myAKSCluster/nginx-deployment.yaml
git commit -m "Add Nginx deployment"
git push origin main
                
9

Verify Deployment in AKS

Flux CD will automatically detect the changes in your Git repository and apply them to your AKS cluster. You can verify the deployment status using kubectl.


# Wait a minute for Flux to sync
kubectl get deployments -l app=nginx
kubectl get services -l app=nginx
                

You should see the Nginx deployment and a LoadBalancer service created. It might take a few minutes for the LoadBalancer IP to be provisioned.


kubectl get service nginx-service
                
Success! Your application is now deployed and managed by GitOps.

Step 5: Updating Your Application

10

Modify the Manifest in Git

To update your application (e.g., change the number of replicas), modify the nginx-deployment.yaml file in your Git repository.

For example, to change replicas to 3:


# ... other parts of the deployment ...
spec:
  replicas: 3 # Changed from 2 to 3
  selector:
    matchLabels:
      app: nginx
# ... rest of the deployment ...
                

Commit and push the changes:


git add clusters/myAKSCluster/nginx-deployment.yaml
git commit -m "Increase Nginx replicas to 3"
git push origin main
                
11

Observe Automatic Updates

Flux CD will detect the change and automatically update your AKS cluster. Verify the change:


kubectl get deployments -l app=nginx
                

You should see the replica count updated to 3.

Conclusion

Congratulations! You have successfully set up and used GitOps with Azure AKS using Flux CD. You've learned how to provision an AKS cluster, configure a Git repository, install Flux CD, deploy applications, and manage updates solely through Git commits.

Next Steps

For further details, refer to the official Flux CD documentation and Azure AKS documentation.