Setting Up CI/CD Pipelines for Azure Kubernetes Service (AKS)
This tutorial guides you through creating a robust Continuous Integration and Continuous Deployment (CI/CD) pipeline for your applications running on Azure Kubernetes Service (AKS). We will leverage Azure Pipelines to automate the build, test, and deployment process.
Prerequisites:
- An Azure subscription.
- An Azure Kubernetes Service (AKS) cluster.
- A Git repository (e.g., Azure Repos, GitHub) with your application code.
- Basic understanding of Docker, Kubernetes, and CI/CD concepts.
1. Prepare Your Application
Ensure your application is containerized using Docker. You should have a Dockerfile in your project's root directory. For Kubernetes deployment, you'll need Kubernetes manifest files (e.g., deployment.yaml, service.yaml).
Example Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "YourApp.dll"]
Example deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-aks-app
spec:
replicas: 2
selector:
matchLabels:
app: my-aks-app
template:
metadata:
labels:
app: my-aks-app
spec:
containers:
- name: my-aks-app
image: youracr.azurecr.io/my-aks-app:$(tag)
ports:
- containerPort: 80
2. Set up Azure Container Registry (ACR)
Azure Container Registry is used to store your Docker images. If you don't have one, you can create it through the Azure portal or Azure CLI.
az acr create --resource-group <your-resource-group> --name <youracrname> --sku Basic
Ensure your AKS cluster is integrated with ACR for seamless image pulls.
3. Create an Azure Pipeline
Navigate to your Azure DevOps project and create a new pipeline.
- Select your repository source (Azure Repos Git, GitHub, etc.).
- Choose "Starter pipeline" or a pre-defined template.
- Configure your
azure-pipelines.yml file.
Example azure-pipelines.yml
This pipeline will build a Docker image, push it to ACR, and then deploy to AKS.
trigger:
- main
variables:
azureSubscription: ''
dockerRegistryServiceConnection: ''
kubernetesServiceConnection: ''
imageRepository: 'my-aks-app'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build and Push Stage
jobs:
- job: Build
displayName: Build and Push Job
pool:
vmImage: $(vmImageName)
steps:
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)
latest
- stage: Deploy
displayName: Deploy Stage
dependsOn: Build
jobs:
- deployment: DeployJob
displayName: Deploy to AKS
environment: 'production' # Or your desired environment
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: KubernetesManifest@0
displayName: Deploy to Kubernetes cluster
inputs:
action: deploy
kubernetesServiceConnection: $(kubernetesServiceConnection)
manifests: |
$(Build.SourcesDirectory)/deployment.yaml
$(Build.SourcesDirectory)/service.yaml
containers: '$(dockerRegistryServiceConnection).azurecr.io/$(imageRepository):$(tag)'
Note: You'll need to set up Service Connections in Azure DevOps for your Azure Subscription, ACR, and AKS cluster.
4. Configure Service Connections
In your Azure DevOps project settings, navigate to "Service connections" and create:
- An Azure Resource Manager service connection to your Azure subscription.
- A Docker Registry service connection to your Azure Container Registry.
- A Kubernetes service connection to your AKS cluster.
5. Run Your Pipeline
Commit your azure-pipelines.yml file to your Git repository. The pipeline will automatically trigger on commits to the main branch (or your configured trigger branch). You can also manually trigger the pipeline from the "Pipelines" section in Azure DevOps.
Monitor the pipeline's execution. Upon successful completion, your application will be deployed to your AKS cluster with the latest Docker image.
6. Advanced CI/CD Practices
- Environments: Utilize Azure DevOps Environments for managing deployments to different stages (dev, staging, production).
- Approval Gates: Implement manual approvals before deploying to production.
- Rollback Strategy: Configure automated rollbacks in case of deployment failures.
- Testing: Integrate various testing stages (unit tests, integration tests, security scans) into your pipeline.
- Helm: Consider using Helm charts for more complex Kubernetes application management.