"How to Setup CI/CD Pipeline Using Azure DevOps for AKS | Atakan Gül Blog | Technology Insights"

How to Setup CI/CD Pipeline Using Azure DevOps for AKS

Learn how to set up a CI/CD pipeline using Azure DevOps for AKS. This guide covers everything from setting up Azure DevOps and Azure Repos to creating build pipelines and connecting AKS with ACR. Find continue reading...

Avatar

Atakan G.

Aug 11, 2024

How to Setup CI/CD Pipeline Using Azure DevOps for AKS

Setting up CI/CD pipeline for development team is crucial for fast development and fast deployment. Without any automation, servers and system components should get set up manually which isn't efficient and takes time.


For that reason, creating a pipeline that has capabilities to manage the whole deployment with a single touch is required. The pipeline should be able to rollback to previous versions easily to prevent unavailability. Furthermore, the pipeline should also be able to build, test, and deploy the app in an automated manner.


In this Article, you will find answers to:


  1. What is Azure DevOps?
  2. What is Azure Repos?
  3. How to set up local agent to run Tasks.
  4. How to set up service connections in Azure DevOps?
  5. How to set up connection between AKS and ACR?
  6. Basic commands to manipulate AKS
  7. How to create a build pipeline in Azure DevOps?


What is Azure DevOps?

Azure DevOps supports a collaborative culture and set of processes that bring together developers, project managers, and contributors to develop software. It allows organizations to create and improve products at a faster pace than they can with traditional software development approaches.


What is Azure Repos?

Azure Repos is a set of version control tools that you can use to manage your code. Whether your software project is large or small, using version control as soon as possible is a good idea. Version control systems are software that helps you track changes you make in your code over time.


You can use git commands to manipulate azure repos which is great. In short terms, Azure repos is kind of enhanced version of Github and git actions. You have the whole capability of managing your teams, projects, testing, DevOps pipelines and your artifacts.


Requirements

Before diving into deep of CI/CD creation, make sure you have the necessary resources to start. First of all, you need to have an Azure subscription. If you don't have an Azure subscription yet, no need to worry create one now and get 200$ free credits.


If you already have an Azure subscription, simply create AKS and ACR services with the following commands: (Dont forget to change the variables according to yourself)

# Set environment variables
RESOURCE_GROUP="myResourceGroup"
REGISTRY_NAME="myContainerRegistry"
AKS_CLUSTER_NAME="myAKSCluster"
LOCATION="eastus" # Check Azure Region Abbreviations

# Create Resource Group
az group create --name $RESOURCE_GROUP --location $LOCATION

# Create Azure Container Registry (ACR)
az acr create --resource-group $RESOURCE_GROUP --name $REGISTRY_NAME --sku Basic

# Create Azure Kubernetes Service (AKS) Cluster
az aks create --resource-group $RESOURCE_GROUP --name $AKS_CLUSTER_NAME --node-count 1 --enable-addons monitoring --generate-ssh-keys

# Configure kubectl to use the new AKS Cluster
az aks get-credentials --resource-group $RESOURCE_GROUP --name $AKS_CLUSTER_NAME

# Attach ACR to AKS
az aks update --resource-group $RESOURCE_GROUP --name $AKS_CLUSTER_NAME --attach-acr $REGISTRY_NAME


This will create Azure Kubernetes service and Azure container registry. By attaching the ACR to AKS, you enable AKS to have necessary AcrPull and permissions to fetch images from the registry. Luckily, attaching directly in the command-line gives us the relief of not configuring RBAC explicitly.


Since I assume you have already set up the Azure DevOps organization, please refer to creating Azure Devops organization for project creation in case you dont have yet.


Also, I will be using Azure Repos, however I will not show how to set up one. This is trivial task, you can find out on the internet easily. Resource


How to Set Up Local Agent to Run Tasks?

Let me explain, If you use free tier Azure or Azure for Students, so you need to set up a local agent which essentially uses your local machine to run tasks that you declared in your azure pipeline.


Also, If you don't want to use your computer, then you can spin up a virtual machine in Azure to use it as an agent. To set up, in Azure DevOps go to > /organization settings/agent pools/ and create an agent pool. Click on it, and then click new agent which will show you the commands to run on the agent machine to set up connection.


The image looks like this


After setting up the Agent machine, go ahead and click to the agent and check if it's online or not. If you get stuck here, check out other resources.

Here how it should look like.


How to Set Up Service Connections?

Since Azure Devops and Azure Cloud are different environments, we will need to set up neccessary connections to enable pipelines to connect respective resources. You can use the created Service connections in azure pipelines to connect with azure resources.


There are a lot of connection types, for the sake of this article we need only Kubernetes and Docker registry connection services. You may find the docker registry name related to Docker Hub, please don't think that :). Because, you can either connect to ACR or Docker registry with this connection type.


Go to /project settings/service connections directory which will look like this


Add two new connection types as mentioned: Docker registry and Kubernetes. To add them simply click to new service connection and type their name. It will show your resources within the your subscription' scope. (comment if you get stuck)


Creating Pipelines

I have already set up my Azure repo, and the repository has the necessary manifests and azure-pipeline.yml.


Please, configure the YAML files for your use cases. For example, you may want to change the agent name mypool-agent001 to your-agent-name.


azure-pipeline.yml

trigger:
- main

variables:
acrName: 'your-acr-name'
imageName: 'user-service'

stages:
- stage: BuildAndDeploy
jobs:
- job: BuildPushDeploy
pool:
name: mypool
demands:
- agent.name -equals mypool-agent001
steps:
# Checkout the code from the repository
- checkout: self

# Build and push the Docker image for user-service
- task: Docker@2
displayName: 'Build and Push user-service Docker Image'
inputs:
containerRegistry: 'atakan1927'
repository: '$(acrName)/$(imageName)'
command: 'buildAndPush'
Dockerfile: '**/user-service/Dockerfile'
tags: |
$(Build.BuildId)
latest

# Deploy to AKS

- task: Kubernetes@1
inputs:
connectionType: 'Kubernetes Service Connection'
kubernetesServiceEndpoint: 'mykubcluster'
namespace: 'default'
command: 'apply'
useConfigurationFile: true
configuration: '$(Pipeline.Workspace)/s/manifests/user-service-deployment.yaml'
containerRegistryType: 'Azure Container Registry'
azureContainerRegistry: 'atakan1927.azurecr.io'

# Print completion message
- script: echo "Pipeline completed successfully"
displayName: 'Pipeline Completion'


user-service-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
namespace: default
name: user-service
spec:
replicas: 1
selector:
matchLabels:
app: user-service
template:
metadata:
labels:
app: user-service
spec:
containers:
- name: user-service
image: $(acrName).azurecr.io/user-service:$(Build.BuildId)
ports:
- containerPort: 3001


We've set up the necessary services and configured Azure DevOps to manipulate these services with the provided YAML files. It's time to run.


1- It will connect to the Agent you've set up.

2 - Then build the image on the Agent machine.

3 - Deploy the image in the ACR to AKS.

The image successfully fetched from the ACR and run in the AKS, since my image has a small developer issue :). It CrashLoopBackOff, but its okay we got what we want I guess :9.


Summary

We created Azure DevOps project and repo, then created service connections to enable azure and azure devops to communicate, after that we created the CI/CD pipeline and manifests YAML files.


This basically enables us to deploy automatically as the developers pushes new features. However, this pipeline requires significant tasks like testing case, staging case, and logging of the pipeline results.


Overall, the article covered the necessary details to get started for Azure DevOps CI/CD pipeline and if you get stuck or have questions, feel free to comment.

How to Setup CI/CD Pipeline Using Azure DevOps for AKS

Published at Sunday, August 11, 2024
Avatar

Atakan G.

2
0
244

Comments

No comments yet.

Top Recommendations
Our most popular posts

Must-Know Free APIs for Developers
Must-Know Free APIs for Developers
Atakan G. Jul 12, 2024 1 Comments
Why I Started Blogging
Why I Started Blogging
Atakan G. Jul 12, 2024 0 Comments