Docker Autoscaler executor

Before you use the Docker Autoscaler executor, see the feedback issue about GitLab Runner autoscaling for a list of known issues.

The Docker Autoscaler executor is an autoscale-enabled Docker executor that creates instances on demand to accommodate the jobs that the runner manager processes. It wraps the Docker executor so that all Docker executor options and features are supported.

The Docker Autoscaler uses fleeting plugins to autoscale. Fleeting is an abstraction for a group of autoscaled instances, which uses plugins that support cloud providers, like Google Cloud, AWS, and Azure.

Install a fleeting plugin

To install a plugin for your target platform, see Install the fleeting plugin. For specific configuration details, see the respective plugin project documentation.

Configure Docker Autoscaler

The Docker Autoscaler executor wraps the Docker executor so that all Docker executor options and features are supported.

To configure the Docker Autoscaler, in the config.toml:

Dedicated autoscaling groups for each runner configuration

Each Docker Autoscaler configuration must have its own dedicated autoscaling resource:

  • For AWS, a dedicated auto scaling group
  • For GCP, a dedicated instance group
  • For Azure, a dedicated scale set

Do not share these autoscaling resources across:

  • Multiple runner managers (separate GitLab Runner installations)
  • Multiple [[runners]] entries within the same runner manager’s config.toml

The Docker Autoscaler keeps track of the instance state that must be synchronized with the cloud provider’s autoscaling resource. When multiple systems attempt to manage the same autoscaling resource, they might issue conflicting scaling commands, resulting in unpredictable behavior, job failures, and potentially higher costs.

Configure an AWS Auto Scaling group

The Docker Autoscaler manages all scaling for an AWS Auto Scaling group. Configure the group with these settings:

SettingValueReason
Desired capacity and minimum capacity0The runner sets the desired capacity. A minimum above 0 keeps instances running that the runner can’t remove.
Maximum capacitymax_instances in [runners.autoscaler], or higherThe group must allow as many instances as the runner can request.
Scaling policyNoneA scaling policy changes the desired capacity that the runner sets. When this happens, the AWS fleeting plugin logs an out-of-sync capacity error.
Instance scale-in protectionTurned onWithout scale-in protection, AWS can terminate any instance when the desired capacity is lowered, including an instance that is running a job. The job then fails with an ssh tunnel: EOF error.
AZRebalance processSuspendedAWS can terminate instances to even out the number of instances across Availability Zones. The runner does not request these terminations, and jobs can fail with an unable to acquire instance: context deadline exceeded error.

If scale-in protection is turned off, the AWS fleeting plugin logs the error new instances are not protected from scale in and should be when it starts.

The runner manager also needs an IAM policy with the recommended permissions. When use_static_credentials in [runners.autoscaler.connector_config] is false, which is the default:

  • For Linux and macOS instances, the plugin uses ec2-instance-connect:SendSSHPublicKey to send an SSH public key to each instance through EC2 Instance Connect.
  • For Windows instances, the plugin uses ec2:GetPasswordData to get the password that AWS provisions. You must also set key_path in [runners.autoscaler.connector_config] to the RSA private key of the EC2 key pair that the instances use. The plugin uses this key to decrypt the password.

Example: AWS autoscaling for 1 job per instance

Prerequisites:

  • An AMI with Docker Engine installed. To enable Runner Manager’s access to the Docker socket on the AMI, the user must be part of the docker group.

    The AMI does not require GitLab Runner to be installed. The instances launched using the AMI must not register themselves as runners in GitLab.

  • An AWS Auto Scaling group. For the required settings, see configure an AWS Auto Scaling group.

  • An IAM policy with the correct permissions.

This configuration supports:

  • A capacity per instance of 1
  • A use count of 1
  • An idle count of 5
  • An idle time of 20 minutes
  • A maximum instance count of 10

By setting the capacity and use count to both 1, each job is given a secure ephemeral instance that cannot be affected by other jobs. As soon the job is complete the instance it was executed on is immediately deleted.

With an idle count of 5, the runner tries to keep 5 whole instances (because the capacity per instance is 1) available for future demand. These instances stay for at least 20 minutes.

The runner concurrent field is set to 10 (maximum number instances * capacity per instance).

toml
concurrent = 10

[[runners]]
  name = "docker autoscaler example"
  url = "https://gitlab.com"
  token = "<token>"
  shell = "sh"                                        # use powershell or pwsh for Windows AMIs

  # uncomment for Windows AMIs when the Runner manager is hosted on Linux
  # environment = ["FF_USE_POWERSHELL_PATH_RESOLVER=1"]

  executor = "docker-autoscaler"

  # Docker Executor config
  [runners.docker]
    image = "busybox:latest"

  # Autoscaler config
  [runners.autoscaler]
    plugin = "aws" # run `gitlab-runner fleeting install` to install the plugin

    capacity_per_instance = 1
    max_use_count = 1
    max_instances = 10

    [runners.autoscaler.plugin_config] # plugin specific configuration (see plugin documentation)
      name             = "my-docker-asg"               # AWS Autoscaling Group name
      profile          = "default"                     # optional, default is 'default'
      config_file      = "/home/user/.aws/config"      # optional, default is '~/.aws/config'
      credentials_file = "/home/user/.aws/credentials" # optional, default is '~/.aws/credentials'

    [runners.autoscaler.connector_config]
      username          = "ec2-user"
      use_external_addr = true

    [[runners.autoscaler.policy]]
      idle_count = 5
      idle_time = "20m0s"

Example: Google Cloud instance group for 1 job per instance

Prerequisites:

  • A VM image with Docker Engine installed, such as COS.

    The VM image does not require GitLab Runner to be installed. The instances launched using the VM image must not register themselves as runners in GitLab.

  • A single-zone Google Cloud instance group. For Autoscaling mode, select Do not autoscale. The runner handles autoscaling, not the Google Cloud instance group.

    Multi-zone instance groups are not currently supported. An issue exists to support multi-zone instance groups in the future.

  • An IAM policy with the correct permissions. If you’re deploying your runner in a GKE cluster, you can add an IAM binding between the Kubernetes service account and the GCP service account. You can add this binding with the iam.workloadIdentityUser role to authenticate to GCP instead of using a key file with credentials_file.

This configuration supports:

  • A capacity per instance of 1
  • A use count of 1
  • An idle count of 5
  • An idle time of 20 minutes
  • A maximum instance count of 10

By setting the capacity and use count to both 1, each job is given a secure ephemeral instance that cannot be affected by other jobs. As soon the job is complete the instance it was executed on is immediately deleted.

With an idle count of 5, the runner tries to keep 5 whole instances (because the capacity per instance is 1) available for future demand. These instances stay for at least 20 minutes.

The runner concurrent field is set to 10 (maximum number instances * capacity per instance).

toml
concurrent = 10

[[runners]]
  name = "docker autoscaler example"
  url = "https://gitlab.com"
  token = "<token>"
  shell = "sh"                                        # use powershell or pwsh for Windows Images

  # uncomment for Windows Images when the Runner manager is hosted on Linux
  # environment = ["FF_USE_POWERSHELL_PATH_RESOLVER=1"]

  executor = "docker-autoscaler"

  # Docker Executor config
  [runners.docker]
    image = "busybox:latest"

  # Autoscaler config
  [runners.autoscaler]
    plugin = "googlecloud" # run `gitlab-runner fleeting install` to install the plugin

    capacity_per_instance = 1
    max_use_count = 1
    max_instances = 10

    [runners.autoscaler.plugin_config] # plugin specific configuration (see plugin documentation)
      name             = "my-docker-instance-group" # Google Cloud Instance Group name
      project          = "my-gcp-project"
      zone             = "europe-west1-c"
      credentials_file = "/home/user/.config/gcloud/application_default_credentials.json" # optional, default is '~/.config/gcloud/application_default_credentials.json'

    [runners.autoscaler.connector_config]
      username          = "runner"
      use_external_addr = true

    [[runners.autoscaler.policy]]
      idle_count = 5
      idle_time = "20m0s"

Example: Azure scale set for 1 job per instance

Prerequisites:

  • An Azure VM image with Docker Engine installed.

    The VM image does not require GitLab Runner to be installed. The instances launched using the VM image must not register themselves as runners in GitLab.

  • An Azure scale set where the autoscaling policy is set to manual. The runner handles the scaling.

This configuration supports:

  • A capacity per instance of 1
  • A use count of 1
  • An idle count of 5
  • An idle time of 20 minutes
  • A maximum instance count of 10

When the capacity and use count are both set to 1, each job is given a secure ephemeral instance that cannot be affected by other jobs. When the job completes, the instance it was executed on is immediately deleted.

When the idle count is set to 5, the runner keeps 5 instances available for future demand (because the capacity per instance is 1). These instances stay for at least 20 minutes.

The runner concurrent field is set to 10 (maximum number instances * capacity per instance).

toml
concurrent = 10

[[runners]]
  name = "docker autoscaler example"
  url = "https://gitlab.com"
  token = "<token>"
  shell = "sh"                                        # use powershell or pwsh for Windows AMIs

  # uncomment for Windows AMIs when the Runner manager is hosted on Linux
  # environment = ["FF_USE_POWERSHELL_PATH_RESOLVER=1"]

  executor = "docker-autoscaler"

  # Docker Executor config
  [runners.docker]
    image = "busybox:latest"

  # Autoscaler config
  [runners.autoscaler]
    plugin = "azure" # run `gitlab-runner fleeting install` to install the plugin

    capacity_per_instance = 1
    max_use_count = 1
    max_instances = 10

    [runners.autoscaler.plugin_config] # plugin specific configuration (see plugin documentation)
      name = "my-docker-scale-set"
      subscription_id = "9b3c4602-cde2-4089-bed8-889e5a3e7102"
      resource_group_name = "my-resource-group"

    [runners.autoscaler.connector_config]
      username = "azureuser"
      password = "my-scale-set-static-password"
      use_static_credentials = true
      timeout = "10m"
      use_external_addr = true

    [[runners.autoscaler.policy]]
      idle_count = 5
      idle_time = "20m0s"

Suspend and resume job environments

  • Status: Experiment

The availability of this feature is controlled by a feature flag. For more information, see the history. This feature is available for testing, but not ready for production use.

With the FF_SUSPENDABLE_ENVIRONMENTS feature flag, a job can suspend its environment instead of releasing it, and a later job can resume it. When a job suspends, the runner keeps the job containers, networks, and volumes on the instance. A resumed job reuses them.

A job that asks to suspend fails at job start with a configuration error unless:

  • FF_SUSPENDABLE_ENVIRONMENTS is enabled in the runner configuration, not only as a CI/CD variable.
  • The [[runners]] entry in config.toml has id set. gitlab-runner register sets id automatically.
  • [runners.autoscaler.state_storage] is enabled.
  • vm_isolation is disabled.
  • FF_USE_DOCKER_AUTOSCALER_DIAL_STDIO, if used, is set in the runner configuration, not as a CI/CD variable.

The following example shows only the settings for suspend and resume:

toml
[[runners]]
  id = <your_runner_id>
  executor = "docker-autoscaler"
  [runners.feature_flags]
    FF_SUSPENDABLE_ENVIRONMENTS = true
  [runners.autoscaler.state_storage]
    enabled = true

A suspended environment expires after runtime_environment_ttl. A job that tries to resume an expired environment fails with runtime_environment_unavailable. Shortly after the TTL, the runner removes the job containers, networks, and volumes. Cache volumes are kept.

Environments suspended by an earlier runner version have no expiry, and the runner cannot resume them. The runner does not remove them after the TTL.

For the same feature on other executors, see the instance executor and the Kubernetes executor.

Persistent storage for each slot

Each instance has one slot for each job it can run at the same time. capacity_per_instance sets the number of slots on each instance. Slots start at 0. Slot-based cgroups use the same slots.

The Docker Autoscaler executor uses the slot number as the concurrent ID. The concurrent ID is part of the names of cache volumes and of the builds directory. The CI_CONCURRENT_PROJECT_ID variable keeps its value. Its value is unique among the jobs of one project that run at the same time on the runner, so it can differ from the slot number.

A job reuses the cache volumes of the previous job for the same project that ran in the same slot on the same instance. Jobs that run at the same time on one instance use different slots, so they do not share a volume. When capacity_per_instance is greater than 1, a job reuses only the volumes of its own slot. A job that runs in a different slot than the previous job of the project does not reuse that job’s cache.

Volumes that earlier versions of GitLab Runner created stay on the instance until the autoscaler removes the instance.

Slot-based cgroup support

The Docker Autoscaler executor supports slot-based cgroups for improved resource isolation between concurrent jobs. Cgroup paths are automatically applied to Docker containers using the --cgroup-parent flag.

For detailed information about slot-based cgroups, including benefits, prerequisites, and setup instructions, see slot-based cgroup support.

Docker-specific configuration

In addition to the standard slot cgroup configuration, you can specify a separate cgroup template for service containers:

toml
[[runners]]
  executor = "docker+autoscaler"
  use_slot_cgroups = true
  slot_cgroup_template = "gitlab-runner/slot-${slot}"

  [runners.docker]
    service_slot_cgroup_template = "gitlab-runner/service-slot-${slot}"

For all available options, see the slot-based cgroup configuration documentation.

Troubleshooting

ERROR: error during connect: ssh tunnel: EOF ()

When instances are removed by an external source (for example, an autoscaling group or automated script), jobs fail with the following error:

ERROR: Job failed (system failure): error during connect: Post "http://internal.tunnel.invalid/v1.43/containers/xyz/wait?condition=not-running": ssh tunnel: EOF ()

The GitLab Runner logs also show an instance unexpectedly removed error for the instance ID assigned to the job:

ERROR: instance unexpectedly removed    instance=<instance_id> max-use-count=9999 runner=XYZ slots=map[] subsystem=taskscaler used=45

To resolve this error, check the events related to the instance on your cloud provider platform. For example, on AWS, check the CloudTrail event history for the event source ec2.amazonaws.com. If the instance was terminated by a scale-in event, turn on instance scale-in protection for the Auto Scaling group.

ERROR: Preparation failed: unable to acquire instance: context deadline exceeded

When you use the AWS fleeting plugin, jobs might fail intermittently with the following error:

ERROR: Preparation failed: unable to acquire instance: context deadline exceeded

This often shows up in the AWS CloudWatch logs because the reserved instance count oscillates up and down:

"2024-07-23T18:10:24Z","instance_count:1,max_instance_count:1000,acquired:0,unavailable_capacity:0,pending:0,reserved:0,idle_count:0,scale_factor:0,scale_factor_limit:0,capacity_per_instance:1","required scaling change",
"2024-07-23T18:10:25Z","instance_count:1,max_instance_count:1000,acquired:0,unavailable_capacity:0,pending:0,reserved:1,idle_count:0,scale_factor:0,scale_factor_limit:0,capacity_per_instance:1","required scaling change",
"2024-07-23T18:11:15Z","instance_count:1,max_instance_count:1000,acquired:0,unavailable_capacity:0,pending:0,reserved:0,idle_count:0,scale_factor:0,scale_factor_limit:0,capacity_per_instance:1","required scaling change",
"2024-07-23T18:11:16Z","instance_count:1,max_instance_count:1000,acquired:0,unavailable_capacity:0,pending:0,reserved:1,idle_count:0,scale_factor:0,scale_factor_limit:0,capacity_per_instance:1","required scaling change",

To resolve this error, ensure that the AZRebalance process is disabled for your autoscaling group in AWS.

Job failures when scaling from zero instances on Azure VMSS

Microsoft Azure Virtual Machine Scale Sets have an overprovisioning feature, which can cause job failures. When Azure scales up, it creates extra VMs to ensure capacity and then terminates them after it meets the requested capacity. This behavior conflicts with GitLab Runner’s instance tracking, which causes the autoscaler to assign jobs to instances that Azure is about to terminate.

Disable overprovisioning by setting overprovision to false in your VMSS configuration.