agent-k8s
by Scalr
A Helm chart for deploying the Scalr Agent on a Kubernetes cluster. Deploys an agent controller with a set of agent workers and executes runs in isolated pods.
About this chart
agent-k8s
A Helm chart for deploying the Scalr Agent on a Kubernetes cluster. Deploys an agent controller with a set of agent workers and executes runs in isolated pods.
Table of Contents
- Prerequisites
- Overview
- Deployment Diagram
- Installation
- Performance Optimization
- Disk Requirements
- Choosing the Data Home Directory
- Amazon EFS
- Root User Limitation
- Restrict Access to VM Metadata Service
- Termination
- HTTP Proxy
- SSL Certificate Bundles
- Troubleshooting
- Limitations
Prerequisites
- Kubernetes 1.29+ (older versions may work but only maintained Kubernetes versions are officially supported)
- Helm 3.0+
Overview
The Agent deploys as two components: a controller and a worker. The controller consumes jobs from Scalr and schedules pods, while the worker supervises the jobs.
The agent worker is a DaemonSet that scales up/down with the cluster, registering and deregistering agents from the pool. When an Agent controller receives a job from Scalr, it schedules a Pod for execution. The Kubernetes workload scheduler assigns the Pod to a specific Node, where the Agent worker running on that Node oversees the execution of the job. By enabling the Kubernetes auto-scaler, Scalr Run workloads can scale linearly based on the load.
Pros
- Cost-efficient for bursty workloads — e.g., deployments with high number of Runs during short periods and low activity otherwise, as resources allocated on demand for each Scalr Run.
- High multi-tenant isolation, as each Scalr Run always has its own newly provisioned environment.
- Better observability, as each Scalr Run is tied to its own unique Pod.
Cons
- Requires access to the Kubernetes API to launch new Pods.
- Requires dedicated node pool. Details.
- Relies on a hostPath volume. Details.
Deployment Diagram
Installation
To install the chart with the release name scalr-agent:
$ helm repo add scalr-agent-helm https://scalr.github.io/agent-helm/
$ helm upgrade --install scalr-agent scalr-agent-helm/agent-k8s \
--set agent.url="https://<account>.scalr.io" \
--set agent.token="<agent-pool-token>"
You can also control the placement of both the controller and the worker on the cluster using the controllerNodeSelector
and workerNodeSelector options. Here's an example using GKE specific labels:
$ helm upgrade --install scalr-agent scalr-agent-helm/agent-k8s
--set agent.url="https://<account>.scalr.io" \
--set agent.token="<agent-pool-token>" \
--set controllerNodeSelector."kubernetes\\.io\\/hostname"="<node-name>" \
--set workerNodeSelector."cloud\\.google\\.com\\/gke-nodepool"="<node-pool-name>"
To use a separate agent pool for Scalr workloads, you may want to configure Taint and Tolerations.
Set up the taints on the Node Pool, and add tolerations to the agent worker with the workerTolerations option. An example:
--set workerTolerations[0].operator=Equal,workerTolerations[0].effect=NoSchedule,workerTolerations[0].key=dedicated,workerTolerations[0].value=scalr-agent-worker-pool
Performance Optimization
The following additional configurations are recommended to optimize Scalr Run startup time and overall chart performance.
Optimize Run Startup Time
This chart uses individual Pods to isolate Scalr runs and creates a new pod each time a Scalr run stage is queued. As a result, fast pod startup is critical for low run startup latency. Common bottlenecks that may introduce latency include slow image pull times. To optimize this, you can:
- Use image copies in an OCI-compatible registry mirror (Google Container Registry, Amazon Elastic Container Registry, Azure Container Registry, and similar) located in the same region as your node pool. This enables faster pull times and reduces the risk of hitting Docker Hub rate limits.
Use the
container_task_imageorcontainer_task_image_registrysettings (the latter applies to the older Docker-image–based software version integrations) to configure the run image. - Use a DaemonSet to preemptively cache all images used in this chart (
scalr/agent,scalr/runner). Otherwise, runs on cold nodes will be delayed by image pull time. - Enable Image Streaming (GKE only) to improve Pod launch time.
The run startup performance also depends on node scaling speed when autoscaling is enabled.
Use Dedicated Node Pool
The Scalr agent workers are deployed using a DaemonSet that auto-scales workers across all nodes.
This requires a dedicated cluster or a separate node pool for agent installation. Otherwise, it may scale across a large number of nodes, spawning excessive number of idle workers.
Resource Limits
Proper resource limit configuration is critical for achieving good run performance. We recommend avoiding CPU limits for worker services, as they are installed per node (via a DaemonSet) and can process multiple runs concurrently, all scheduled on the same node. Setting CPU limits on workers may lead to throttling and degraded performance under load.
For the run environment (the container where OpenTofu/Terraform is executed), resource requests and limits can be configured using the following Helm values:
# -- CPU resource request, defined in cores.
# For example:
# 2.0 = two full CPU cores
# 0.25 = one quarter of a CPU core
agent.container_task_cpu_request: 1.0
# -- CPU resource limit, defined in cores.
# This is the maximum amount of CPU the container is allowed to use.
agent.container_task_cpu_limit: 8.0
# -- Memory resource request, defined in megabytes.
# This is the amount of memory reserved for the container.
agent.container_task_mem_request: 1024
# -- Memory resource limit, defined in megabytes.
# This is the maximum amount of memory the container can use before being terminated.
agent.container_task_mem_limit: 16384
Disk Requirements
Currently, the Agent is not fully cloud-native and utilizes the hostPath volume for storing a shared OpenTofu/Terraform plugin cache and managing configuration version artifacts for agent task Pods.
The volume is configured via the agent.data_home option. The filesystem on this volume must be
writable, executable, and stateful (within the lifecycle of the Scalr Agent Pod).
Choosing the Data Home Directory
In the default template example, the node disk is utilized at the path /home/kubernetes/flexvolume/agent-k8s.
This path is specific to Container-Optimized OS (GKE) and varies depending on the Kubernetes provider in use.
There is also a known issue with the default agent.data_home directory, which will be changed in the future.
It is recommended to alter the default directory to /home/kubernetes/bin/scalr/{unique-name}.
For EKS (Amazon Linux 2 or Bottlerocket OS), the recommended path is /var/lib/{unique-name}.
Using a unique name in the path is necessary when installing multiple agents on the cluster
to prevent collisions. Additionally, it is important to note that the Agent does not delete its
data when uninstalling the chart or modifying the agent.data_home option, which may result
in artifacts being left on the node's root disk.
Example of setting agent.data_home:
$ helm upgrade ... \
--set agent.data_home="/var/lib/{unique-name}"
Amazon EFS
[!WARNING] This chart is designed around the use of
hostPath, and using a network volume instead of hostPath means the entire Scalr run and Terraform runtime depend on it. It is not possible to use a network volume only for the cache, the whole Scalr run would be executed on a network disk, and communication between worker and runner containers will be performed over the network. This would negatively impact run performance and slow down the entire runtime. Using network disks is generally not recommended. If you want to mitigate risks associated with using hostPath, please consider dedicating a separate node pool for Scalr agents, or consider theagent-localchart or theagent-jobchart instead.
Amazon EFS can be used as a shared ReadWriteMany volume instead of a node disk. To configure it,
install the Amazon EFS CSI Driver via an add-on. See the documentation: https://docs.aws.amazon.com/eks/latest/userguide/efs-csi.html#efs-install-driver.
Ensure the add-on is active before proceeding.
Next, configure the Amazon EFS file system ID using the efsVolumeHandle option:
$ helm upgrade ... \
--set agent.data_home="/var/lib/{unique-name}" \
--set efsVolumeHandle="fs-582a03f3"
# Alternatively, if using an Access Point:
# see: https://docs.aws.amazon.com/efs/latest/ug/accessing-fs-nfs-permissions.html#accessing-fs-nfs-permissions-access-points
--set efsVolumeHandle="fs-582a03f3::fsap-01e050b7d9a3109d5"
The EFS storage will be mounted in all worker containers at the agent.data_home path. All child containers
for Runs will inherit the EFS configuration. The controller will continue to use an ephemeral directory
as its data home.
The EFS configuration includes default mount options to ensure that the NFS attribute cache is kept minimal, providing better read-after-write consistency across pods:
efsMountOptions:
- acregmin=1
- acregmax=3
- acdirmin=1
- acdirmax=3
Changing these defaults may affect Scalr Agent behavior. For more information, see: https://www.ibm.com/docs/en/aix/7.2.0?topic=client-nfs-file-attribute-cache-tuning
Root User Limitation
The chart defaults to running the agent as root because hostPath storage is typically owned by root on the node.
While you can configure securityContext to run as a non-root user, this is intended for EFS
and is not supported for the default hostPath setup.
The chart is expected to be run on a dedicated node pool to mitigate security risks associated with hostPath and root users.
If you consider this an issue, we recommend looking into the agent-local or agent-job charts, which provide more secure defaults.
Restrict Access to VM Metadata Service
The chart includes an optional feature to restrict the pods from accessing the VM metadata service at 169.254.169.254, that is common for both AWS and GCP environments.
To enable it, use the restrictMetadataService option:
$ helm upgrade ... \
--set restrictMetadataService=true
With this option enabled, a Kubernetes NetworkPolicy is applied to the agent pods that denies egress traffic to 169.254.169.254/32, blocking access to the VM metadata service. All other outbound traffic is allowed.
Termination
Both the controller (singleton service) and worker (one per node service) agents maintain a registration and liveness indicator within the Scalr Agent Pool throughout their entire runtime. When an agent stops, it deregisters itself from the Scalr platform as part of its shutdown procedure after receiving a SIGTERM signal.
Because agents may be managing an active run stage, it is important to allow them to terminate gracefully rather than being abruptly stopped with SIGKILL, which would leave no opportunity to perform a graceful shutdown of the underlying OpenTofu/Terraform workload or push a status update to the Scalr platform, and can lead to undefined behavior — ranging from degraded performance and Scalr Run processing delays to agent capacity issues, stuck runs, or even OpenTofu/Terraform state loss.
Pod Eviction
To reduce the risk of Pod eviction for active Scalr agents, the default configuration applies the following annotations for common autoscalers such as Cluster Autoscaler:
cluster-autoscaler.kubernetes.io/safe-to-evict: "false"
Monitor node resource pressure and eviction events to ensure stable operation.
Scalr Run Out-of-Memory Termination
The runner container executes Scalr Run workloads and processes end-user IaC configuration and code, resulting in highly variable memory utilization and an elevated risk of exceeding the memory limit and triggering an OOM kill.
When a runner container exceeds its memory limit, Kubernetes sends SIGKILL directly to the process with no opportunity to clean up. For OpenTofu/Terraform workloads, this can result in state loss or corruption if the process is killed before it can push state.
Monitor your run's resource usage and configure resource requests and limits accordingly to mitigate the risk of unexpected termination.
HTTP Proxy
To configure an HTTP proxy, set the HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables.
Example of running agent with HTTP proxy enabled:
$ helm upgrade ... \
--set extraEnv.HTTP_PROXY="<proxy-address>" \
--set extraEnv.HTTPS_PROXY="<proxy-address>" \
--set extraEnv.NO_PROXY="<addr1>,<addr2>"
Deploy agent-k8s with OpenTenant
Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has agent-k8s on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.
- Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
- Place the chartagent-k8s is pinned at v0.6.16 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
- ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below opentenant validate opentenant.yaml terraform init && terraform apply