Automate your Kubernetes deployments with GitOps.
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.
Before you begin, make sure you have the following:
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
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
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
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
Download and install the Flux CLI. Follow the official Flux documentation for the latest installation instructions: Flux CD Installation.
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
This command will install the necessary Flux controllers into your cluster and configure them to watch your specified Git path.
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
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
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
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
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.
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.
For further details, refer to the official Flux CD documentation and Azure AKS documentation.