In-Cluster Building
One of Garden's most powerful features is the ability to build images in your Kubernetes development cluster, thus avoiding the need for local Kubernetes clusters. This guide covers the requirements for in-cluster building and how to set it up.
This guide assumes you've already read through the Remote Kubernetes guide.

tl;dr

If in doubt, use the following setup for builds:
  • kaniko build mode, which works well for most scenarios.
  • Use the project namespace for build pods.
  • Connect a remote deployment registry to use for built images. Note: You can also skip this and use the included in-cluster registry while testing, but be aware that you may hit scaling issues as you go.
Here's a basic configuration example:
1
kind: Project
2
name: my-project
3
...
4
providers:
5
- name: kubernetes
6
# Use the kaniko build mode
7
buildMode: kaniko
8
kaniko:
9
namespace: null # <--- use the project namespace for builds
10
# Recommended: Configure a remote registry
11
deploymentRegistry:
12
hostname: my-private-registry.com # <--- the hostname of your registry
13
namespace: my-project # <--- the namespace to use within your registry
14
imagePullSecrets:
15
- name: my-deployment-registry-secret # <--- the name and namespace of a valid Kubernetes imagePullSecret
16
namespace: default
Copied!
The only tricky bit would be connecting the remote registry, so we suggest reading more about that below.

Security considerations

First off, you should only use in-cluster building in development and testing clusters! Production clusters should not run the builder services for multiple reasons, both to do with resource and security concerns.
You should also avoid using in-cluster building in clusters where you don't control/trust all the code being deployed, i.e. multi-tenant setups (where tenants are external, or otherwise not fully trusted).

General requirements

In-cluster building works with most Kubernetes clusters, provided they have enough resources allocated and meet some basic requirements. We have tested it on GKE, AKS, EKS, DigitalOcean, and various other custom installations.
The specific requirements vary by the build mode used, and whether you're using the optional in-cluster registry or not.
In all cases you'll need at least 2GB of RAM on top of your own service requirements. More RAM is strongly recommended if you have many concurrent developers or CI builds.
For the cluster-docker mode, and the (optional) in-cluster image registry, support for PersistentVolumeClaims is required, with enough disk space for layer caches and built images. The in-cluster registry also requires support for hostPort, and for reaching hostPorts from the node/Kubelet. This should work out-of-the-box in most standard setups, but clusters using Cilium for networking may need to configure this specifically, for example.
You can—and should—adjust the allocated resources and storage in the provider configuration, under resources and storage. See the individual modes below as well for more information on how to allocate resources appropriately.
We also strongly recommend a separate image registry to use for built images. Garden can also—and does by default—deploy an in-cluster registry. The latter is convenient to test things out and may be fine for individual users or small teams. However, we generally recommend using managed container registries (such as ECR, GCR etc.) since they tend to perform better, they scale more easily, and don't need to be operated by your team. See the Configuring a deployment registry section for more details.

Build modes

Garden supports multiple methods for building images and making them available to the cluster:
  1. 1.
    kaniko — Individual Kaniko pods created for each build.
  2. 2.
    cluster-buildkit — A BuildKit deployment created for each project namespace.
  3. 3.
    cluster-docker — (Deprecated) A single Docker daemon installed in the garden-system namespace and shared between users/deployments. It is no longer recommended and we will remove it in future releases.
  4. 4.
    local-docker — Build using the local Docker daemon on the developer/CI machine before pushing to the cluster/registry.
The local-docker mode is set by default. You should definitely use that when using Docker for Desktop, Minikube and most other local development clusters.
The other modes—which are why you're reading this guide—all build your images inside your development/testing cluster, so you don't need to run Docker on your machine, and avoid having to build locally and push build artifacts over the wire to the cluster for every change to your code.
The remote building options each have some pros and cons. You'll find more details below but here are our general recommendations at the moment:
  • kaniko is a solid choice for most cases and is currently our first recommendation. It is battle-tested among Garden's most demanding users (including the Garden team itself). It also scales horizontally and elastically, since individual Pods are created for each build. It doesn't require priviliged containers to run and requires no shared cluster-wide services.
  • cluster-buildkit is a new addition and replaces the older cluster-docker mode. A BuildKit Deployment is dynamically created in each project namespace and much like Kaniko requires no other cluster-wide services. This mode also offers a rootless option, which runs without any elevated privileges, in clusters that support it.
We recommend picking a mode based on your usage patterns and scalability requirements. For ephemeral namespaces, kaniko is generally the better option, since the persistent BuildKit deployment won't have a warm cache anyway. For long-lived namespaces, like the ones a developer uses while working, cluster-buildkit may be a more performant option.
Let's look at how each mode works in more detail, and how you configure them:

kaniko

This mode uses an individual Kaniko Pod for each image build.
The Kaniko project provides a compelling alternative to a Docker daemon because it can run without special privileges on the cluster, and is thus more secure. It also scales better because it doesn't rely on a single daemon shared across multiple users and/or builds; builds are executed in individual Pods and thus scale horizontally and elastically.
In this mode, builds are executed as follows:
  1. 1.
    Your code (build context) is synchronized to a sync service in the cluster, which holds a cache of the build context, so that each change can be uploaded quickly.
  2. 2.
    A Kaniko pod is created, which pulls the build context from the sync service, and performs the build.
  3. 3.
    Kaniko pulls caches from the deployment registry, builds the image, and then pushes the built image back to the registry, which makes it available to the cluster.

Configuration and requirements

As of Garden v0.12.22, the kaniko build mode no longer requires shared system services or an NFS provisioner, nor running cluster-init ahead of usage.
Enable this by setting buildMode: kaniko in your kubernetes provider configuration.
As of Garden v0.12.22, we also recommend setting kaniko.namespace: null in the kubernetes provider configuration, so that builder pods are started in the project namespace instead of the garden-system namespace, which is the current default. This will become the default in Garden v0.13.
Note the difference in how resources for the builder are allocated between Kaniko and the other modes. For this mode, the resource configuration applies to each Kaniko pod. See the builder resources reference for details.
If you're using ECR on AWS, you may need to create a cache repository manually for Kaniko to store caches.
That is, if you have a repository like, my-org/my-image, you need to manually create a repository next to it called my-org/my-image/cache. AWS ECR supports immutable image tags, see the announcement and documentation. Make sure to set the cache repository's image tag mutability setting to mutable. By default, Kaniko's TTL on old cache layers is two weeks, and every layer of the image cache must be rebuilt after that if the image tags are immutable.
You can also select a different name for the cache repository and pass the path to Kaniko via the --cache-repo flag, which you can set on the extraFlags field. See this GitHub comment in the Kaniko repo for more details.
This does not appear to be an issue for GCR on GCP. We haven't tested this on other container repositories.
You can provide extra arguments to Kaniko via the extraFlags field. Users with projects with a large number of files should take a look at the --snapshotMode=redo and --use-new-run options as these can provide significant performance improvements. Please refer to the official docs for the full list of available flags.
The Kaniko pods will always have the following toleration set:
1
key: "garden-build",
2
operator: "Equal",
3
value: "true",
4
effect: "NoSchedule"
Copied!
This allows you to set corresponding Taints on cluster nodes to control which nodes builder deployments are deployed to. You can also configure a nodeSelector to serve the same purpose.

cluster-buildkit

With this mode, a BuildKit Deployment is dynamically created in each project namespace to perform in-cluster builds.
Much like kaniko (and unlike cluster-docker), this mode requires no cluster-wide services or permissions to be managed, and thus no permissions outside of a single namespace for each user/project.
In this mode, builds are executed as follows:
  1. 1.
    BuildKit is automatically deployed to the project namespace, if it hasn't already been deployed there.
  2. 2.
    Your code (build context) is synchronized directly to the BuildKit deployment.
  3. 3.
    BuildKit imports caches from the deployment registry, builds the image, and then pushes the built image and caches back to the registry.

Configuration and requirements

Enable this mode by setting buildMode: cluster-buildkit in your kubernetes provider configuration.
In order to enable rootless mode, add the following to your kubernetes provider configuration:
1
clusterBuildkit:
2
rootless: true
Copied!
Note that not all clusters can currently support rootless operation, and that you may need to configure your cluster with this in mind. Please see the BuildKits docs for details.
You should also set the builder resource requests/limits. For this mode, the resource configuration applies to each BuildKit deployment, i.e. for each project namespace. See the builder resources reference for details.
The BuildKit deployments will always have the following toleration set:
1
key: "garden-build",
2
operator: "Equal",
3
value: "true",
4
effect: "NoSchedule"
Copied!
This allows you to set corresponding Taints on cluster nodes to control which nodes builder deployments are deployed to. You can also configure a nodeSelector to serve the same purpose.

cluster-docker

The cluster-docker build mode has been deprecated and will be removed in an upcoming release. Please use kaniko or cluster-buildkit instead.
The cluster-docker mode installs a standalone Docker daemon into your cluster, that is then used for builds across all users of the clusters, along with a handful of other supporting services.
In this mode, builds are executed as follows:
  1. 1.
    Your code (build context) is synchronized to a sync service in the cluster, making it available to the Docker daemon.
  2. 2.
    A build is triggered in the Docker daemon.
  3. 3.
    The built image is pushed to the deployment registry, which makes it available to the cluster.

Configuration and requirements

Enable this mode by setting buildMode: cluster-docker in your kubernetes provider configuration.
After enabling this mode, you will need to run garden plugins kubernetes cluster-init --env=<env-name> for each applicable environment, in order to install the required cluster-wide services. Those services include the Docker daemon itself, as well as an image registry, a sync service for receiving build contexts, two persistent volumes, an NFS volume provisioner for one of those volumes, and a couple of small utility services.
By default, Garden will install an NFS volume provisioner into garden-system in order to be able to efficiently synchronize build sources to the cluster and then attaching those to the Kaniko pods. You can also specify a storageClass to provide another ReadWriteMany capable storage class to use instead of NFS. This may be advisable if your cloud provider provides a good alternative, or if you already have such a provisioner installed.
Optionally, you can also enable BuildKit to be used by the Docker daemon. This is not to be confused with the cluster-buildkit build mode, which doesn't use Docker at all. In most cases, this should work well and offer a bit of added performance, but it remains optional for now. If you have cluster-docker set as your buildMode you can enable BuildKit for an environment by adding the following to your kubernetes provider configuration:
1
clusterDocker:
2
enableBuildKit: true
Copied!
Make sure your cluster has enough resources and storage to support the required services, and keep in mind that these services are shared across all users of the cluster. Please look at the resources and storage sections in the provider reference for details.

Local Docker

This is the default mode. It is the least efficient one for remote clusters, but requires no additional configuration or services to be deployed to the cluster. For remote clusters, you do however need to explicitly configure a deployment registry, and obviously you'll need to have Docker running locally.
See the Local Docker builds section in the Remote Clusters guide for details.

Configuring a deployment registry

To deploy a built image to a remote Kubernetes cluster, the image first needs to be pushed to a container registry that is accessible to the cluster. We refer to this as a deployment registry. Garden offers two options to handle this process:
  1. 1.
    An in-cluster registry.
  2. 2.
    An external registry, e.g. a cloud provider managed registry like ECR or GCR. (recommended)
The in-cluster registry is a simple way to get started with Garden that requires no configuration. To set it up, leave the deploymentRegistry field on the kubernetes provider config undefined, and run garden plugins kubernetes cluster-init --env=<env-name> to install the registry. This is nice and convenient, but is not a particularly good approach for clusters with many users or lots of builds. When using the in-cluster registry you need to take care of cleaning it up routinely, and it may become a performance and redundancy bottleneck with many users and frequent (or heavy) builds.
So, for any scenario with a non-trivial amount of users and builds, we strongly suggest configuring a separate registry outside of your cluster. If your cloud provider offers a managed option, that's usually a good choice.
To configure a deployment registry, you need to specify at least the deploymentRegistry field on your kubernetes provider, and in many cases you also need to provide a Secret in order to authenticate with the registry via the imagePullSecrets field:
1
kind: Project
2
name: my-project
3
...
4
providers:
5
- name: kubernetes
6
...
7
deploymentRegistry:
8
hostname: my-private-registry.com # <--- the hostname of your registry
9
namespace: my-project # <--- the namespace to use within your registry
10
imagePullSecrets:
11
- name: my-deployment-registry-secret # <--- the name and namespace of a valid Kubernetes imagePullSecret
12
namespace: default
Copied!
Now say, if you specify hostname: my-registry.com and namespace: my-project-id for the deploymentRegistry field, and you have a container module named some-module in your project, it will be tagged and pushed to my-registry.com/my-project-id/some-module:v:<module-version> after building. That image ID will be then used in Kubernetes manifests when running containers.
For this to work, you in most cases also need to provide the authentication necessary for both the cluster to read the image and for the builder to push to the registry. We use the same format and mechanisms as Kubernetes imagePullSecrets for this. See this guide for how to create the secret, but keep in mind that for this context, the authentication provided must have write privileges to the configured registry and namespace.
See below for specific instruction for working with ECR.
Note: If you're using the kaniko or cluster-docker build mode, you need to re-run garden plugins kubernetes cluster-init any time you add or modify imagePullSecrets, for them to work.

Using in-cluster building with ECR

For AWS ECR (Elastic Container Registry), you need to enable the ECR credential helper once for the repository by adding an imagePullSecret for you ECR repository.
First create a config.json somewhere with the following contents (<aws_account_id> and <region> are placeholders that you need to replace for your repo):
1
{
2
"credHelpers": {
3
"<aws_account_id>.dkr.ecr.<region>.amazonaws.com": "ecr-login"
4
}
5
}
Copied!
Next create the imagePullSecret in your cluster (feel free to replace the default namespace, just make sure it's correctly referenced in the config below):
1
kubectl --namespace default create secret generic ecr-config \
2
--from-file=.dockerconfigjson=./config.json \
3
--type=kubernetes.io/dockerconfigjson
Copied!
Finally, add the secret reference to your kubernetes provider configuration:
1
kind: Project
2
name: my-project
3
...
4
providers:
5
- name: kubernetes
6
...
7
imagePullSecrets:
8
- name: ecr-config
9
namespace: default
Copied!

Using in-cluster building with GCR

To use in-cluster building with GCR (Google Container Registry) you need to set up authentication, with the following steps:
  1. 1.
    Create a Google Service Account (GSA).
  2. 2.
    Give the GSA the appropriate permissions.
  3. 3.
    Create a JSON key for the account.
  4. 4.
    Create an imagePullSecret for using the JSON key.
  5. 5.
    Add a reference to the imagePullSecret in your Garden project configuration.
First, create a Google Service Account:
1
# You can replace the gcr-access name of course, but make sure you also replace it in the commands below
2
gcloud iam service-accounts create gcr-access --project ${PROJECT_ID}
Copied!
Then, to grant the Google Service account the right permission to push to GCR, run the following GCR commands:
1
# Create a role with the required permissions
2
gcloud iam roles create gcrAccess \
3
--project ${PROJECT_ID} \
4
--permissions=storage.objects.get,storage.objects.create,storage.objects.list,storage.objects.update,storage.objects.delete,storage.buckets.create,storage.buckets.get
5
6
# Attach the role to the newly create Google Service Account
7
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
8
--member=serviceAccount:[email protected]${PROJECT_ID}.iam.gserviceaccount.com \
9
--role=projects/${PROJECT_ID}/roles/gcrAccess
Copied!
Next create a JSON key file for the GSA:
1
gcloud iam service-accounts keys create keyfile.json --iam-account [email protected]${PROJECT_ID}.iam.gserviceaccount.com
Copied!
Then prepare the imagePullSecret in your Kubernetes cluster. Run the following command, if appropriate replacing gcr.io with the correct registry hostname (e.g. eu.gcr.io or asia.gcr.io):
1
kubectl --namespace default create secret docker-registry gcr-config \
2
--docker-server=gcr.io \
3
--docker-username=_json_key \
4
--docker-password="$(cat keyfile.json)"
Copied!
Finally, add the created imagePullSecret to your kubernetes provider configuration:
1
kind: Project
2
name: my-project
3
...
4
providers:
5
- name: kubernetes
6
...
7
imagePullSecrets:
8
- name: gcr-config
9
namespace: default
Copied!

Publishing images

You can publish images that have been built in your cluster, using the garden publish command. See the Publishing images section in the Container Modules guide for details.
Note that you currently need to have Docker running locally even when using remote building, and you need to have authenticated with the target registry. When publishing, we pull the image from the remote registry to the local Docker daemon, and then go on to push it from there. We do this to avoid having to (re-)implement all the various authentication methods (and by extension key management) involved in pushing directly from the cluster, and because it's often not desired to give clusters access to directly push to production registries.

Cleaning up cached images

In order to avoid disk-space issues in the cluster when using the in-cluster registry and/or either of the kaniko or cluster-docker build modes, the kubernetes provider exposes a utility command:
1
garden --env=<your-environment> plugins kubernetes cleanup-cluster-registry
Copied!
The command does the following:
  1. 1.
    Looks through all Pods in the cluster to see which images/tags are in use, and flags all other images as deleted in the in-cluster registry and.
  2. 2.
    Restarts the registry in read-only mode.
  3. 3.
    Runs the registry garbage collection.
  4. 4.
    Restarts the registry again without the read-only mode.
  5. 5.
    When using the cluster-docker build mode, we additionally untag in the Docker daemon all images that are no longer in the registry, and then clean up the dangling image layers by running docker image prune.
There are plans to do this automatically when disk-space runs low, but for now you can run this manually or set up your own cron jobs.
You can avoid this entirely by using a remote deployment registry and the cluster-buildkit build mode.

Pulling base images from private registries

The in-cluster builder may need to be able to pull base images from a private registry, e.g. if your Dockerfile starts with something like this:
1
FROM my-private-registry.com/my-image:tag
Copied!
where my-private-registry.com requires authorization.
For this to work, you need to create a registry secret in your cluster (see this guide for how to create the secret) and then configure the imagePullSecrets field in your kubernetes provider configuration:
1
kind: Project
2
name: my-project
3
...
4
providers:
5
- name: kubernetes
6
...
7
imagePullSecrets:
8
# The name of the registry auth secret you created.
9
- name: my-registry-secret
10
# Change this if you store the secret in another namespace.
11
namespace: default
Copied!
This registry auth secret will then be copied and passed to the in-cluster builder. You can specify as many as you like, and they will be merged together.
Note: If you're using the kaniko or cluster-docker build mode, you need to re-run garden plugins kubernetes cluster-init any time you add or modify imagePullSecrets, for them to work when pulling base images!
Last modified 2mo ago