Azure DevOps Self Hosted Agents in Azure Kubernetes Service

Azure AKS for Devops Agents

Kasun Rajapakse
Enlear Academy

--

Modern software development is driven by DevOps practices. From startups to enterprises are adopting DevOps to their application development. DevOps allows organizations to release applications more frequently and faster cycles.

CALMS is a framework that assesses a company’s ability to adopt DevOps processes, as well as a way of measuring success during a DevOps transformation. The acronym was coined by Jez Humble, co-author of “The DevOps Handbook,” and stands for Culture, Automation, Lean, Measurement, and Sharing. — Atlassian —

To achieve successful application from development to release, organizations have to go through a process. In good DevOps practice, we have to reduce the toil and automate as much as possible. To automate from development to release, we use CI/CD (Continuous Integration / Continues Delivery)

What is Azure DevOps?

Azure DevOps is a set of tools that enables organizations to develop and deploy applications to customers. It includes five services.

  • Azure Boards — Deliver value to your users faster using proven agile tools to plan, track, and discuss work across your teams.
  • Azure Pipelines — Build, test, and deploy with CI/CD that works with any language, platform, and cloud. Connect to GitHub or any other Git provider and deploy continuously.
  • Azure Repos — Get unlimited, cloud-hosted private Git repos and collaborate to build better code with pull requests and advanced file management.
  • Azure Test Plans — Test and ship with confidence using manual and exploratory testing tools.
  • Azure Artifacts — Create, host, and share packages with your team and add artifacts to your CI/CD pipelines with a single click.

Azure DevOps Self Hosted Agents

Azure DevOps pipelines are used for CI/CD automation. When automating builds and deployment through CI/CD pipelines, we need to use agent runners to build or deploy tasks. Out of the box, Azure provides us build agents with different operating systems(Linux, Windows, macOS). But the use of Azure-hosted build agents wouldn’t start the build immediately. So we can identify the following as advantages of using self-hosted agents.

  • Speed up the pipeline
  • Customize the agent tools for the build agent
  • Less cost compared to Azure Hosted
  • Give you more control to install dependent software needed for your builds and deployments.
  • Machine-level caches and configuration persist from run to run, which can boost speed.

Now we discussed Azure DevOps and Self Hosted agents. The following guide shows how to create a Docker self-hosted agent based on customer requirements and use the Docker image for running build agents in Azure Kubernetes Cluster. Let's get started.

Docker self-hosted agent for Azure DevOps builds agents.

1. Create Dockerfile for Docker agents ⚙️

We can create our custom Dockerfile as per the requirement needed. So we can use Microsoft-provided Dockerfile as a template. Following Dockerfile is modified to use as NodeJS build agent. Additionally, we added a node package for installation for Docker commands.

In this Dockerfile, we use the script called start.sh in ENTRYPOINT

In the local environment, I have the following files.

Figure 1
docker image build -t dockerbuildagent .

The above command builds a Docker image we need for us to create an Azure DevOps build agent.

Figure 2

We can run the Azure DevOps build agent as below.

docker run -e AZP_URL=<DevOps URL> -e AZP_TOKEN=<Token> -e AZ
P_AGENT_NAME=<AgentName> -e AZP_POOL=<PoolName> dockerbuildagent
Figure 3

We can verify the agent status from the Azure DevOps organization as below

Figure 4

2. Use Azure Kubernetes Cluster for Azure DevOps build agents 📦

After the build, we can push the image to the container registry. Then we can use the image for running Kubernetes deployment. These pods are used as Azure DevOps build agents.

First, we need to create a Kubernetes secret with PAT (Personal Access Token), Azure DevOps URL, agent pool name.

kubectl create secret generic azdevops --from-literal=AZP_URL=<URL> --from-literal=AZP_TOKEN=<Token> --from-literal=AZP_POOL=<PoolName> -n azure-devops-agents

Example deployment YAML

kubectl apply -f azure-devops-agent-deploy.yaml

After the YAML manifest is applied, we can identify the authenticated agents with the Azure DevOps organization as follows.

Figure 5

To follow on more details, you can watch the following tutorial 👇

Conclusion

We can use custom Azure DevOps build agents as containers. To take it further, we can deploy those containers as a resource to take advantage of Kubernetes advanced features.

Learn More

--

--

Anything related to Azure, AWS, GCP, Containers & Kubernetes. Technology enthusiastic, Learner, Blogger