Azure DevOps Build Agents in Kubernetes

9/26/2021

We are planning to run our Azure Devops build agents in a Kubernetes pods.But going through the internet, couldn't find any recommended approach to follow.

Details:

  • Azure Devops Server
  • AKS- 1.19.11

Looking for

  • AKS kubernetes cluster where ADO can trigger its pipeline with the dependencies.
  • The scaling of pods should happen as the load from the ADO will be initiating
  • Is there any default MS provided image available currently for the build agents?
  • The image should be light weight with BuildAgents and the zulu jdk debian as we are running java based apps.

Any suggestions highly appreciated

-- Vowneee
azure
azure-aks
azure-devops
kubernetes

1 Answer

9/26/2021

This article provides instructions for running your Azure Pipelines agent in Docker. You can set up a self-hosted agent in Azure Pipelines to run inside a Windows Server Core (for Windows hosts), or Ubuntu container (for Linux hosts) with Docker.

The image should be light weight with BuildAgents and the zulu jdk debian as we are running java based apps.

Add tools and customize the container

Once you have created a basic build agent, you can extend the Dockerfile to include additional tools and their dependencies, or build your own container by using this one as a base layer. Just make sure that the following are left untouched:

  • The start.sh script is called by the Dockerfile.
  • The start.sh script is the last command in the Dockerfile.
  • Ensure that derivative containers don't remove any of the dependencies stated by the Dockerfile.

Note: Docker was replaced with containerd in Kubernetes 1.19, and Docker-in-Docker became unavailable. A few use cases to run docker inside a docker container:

  • One potential use case for docker in docker is for the CI pipeline, where you need to build and push docker images to a container registry after a successful code build.
  • Building Docker images with a VM is pretty straightforward. However, when you plan to use Jenkins Docker-based dynamic agents for your CI/CD pipelines, docker in docker comes as a must-have functionality.
  • Sandboxed environments.
  • For experimental purposes on your local development workstation.

If your use case requires running docker inside a container then, you must use Kubernetes with version <= 1.18.x (currently not supported on Azure) as shown here or run the agent in an alternative docker environment as shown here.

Else if you are deploying the self hosted agent on AKS, the azdevops-deployment Deployment at step 4, here, must be changed to:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: azdevops-deployment
  labels:
    app: azdevops-agent
spec:
  replicas: 1 #here is the configuration for the actual agent always running
  selector:
    matchLabels:
      app: azdevops-agent
  template:
    metadata:
      labels:
        app: azdevops-agent
    spec:
      containers:
      - name: azdevops-agent
        image: <acr-server>/dockeragent:latest
        env:
          - name: AZP_URL
            valueFrom:
              secretKeyRef:
                name: azdevops
                key: AZP_URL
          - name: AZP_TOKEN
            valueFrom:
              secretKeyRef:
                name: azdevops
                key: AZP_TOKEN
          - name: AZP_POOL
            valueFrom:
              secretKeyRef:
                name: azdevops
                key: AZP_POOL

The scaling of pods should happen as the load from the ADO will be initiating

You can use cluster-autoscaler and horizontal pod autoscaler. When combined, the horizontal pod autoscaler is focused on running the number of pods required to meet application demand. The cluster autoscaler is focused on running the number of nodes required to support the scheduled pods. [Reference]

-- Srijit_Bose-MSFT
Source: StackOverflow