20 concurrent kaniko buildruns creation time is much slower than 20 buildpack ones
When I tried to create 20 buildruns with kaniko v0.19 executor image, the average buildrun creation time is several hundreds seconds, sometime is 500+s, comparing to single kaniko buildrun creation time of 90+s.
As a comparison, when creating 20 buildruns with paketo buildpack, the average buildrun creation time is acceptable of within 100s, comparing to single paketo buildrun creation time of 40+s.
Kaniko build definition:
---
apiVersion: build.dev/v1alpha1
kind: Build
metadata:
name: ${build_name}
spec:
source:
url: https://github.com/SaschaSchwarze0/npm-simple
contextDir: renamed
strategy:
name: kaniko
kind: ClusterBuildStrategy
dockerfile: RenamedDockerfile
resources:
limits:
cpu: "1"
memory: "1Gi"
output:
image: ${registry}/${build_name}
credentials:
name: icr-knbuild
buildpack buld definition:
---
apiVersion: build.dev/v1alpha1
kind: Build
metadata:
name: ${build_name}
annotations:
build.build.dev/build-run-deletion: "false"
spec:
source:
url: https://github.com/sclorg/nodejs-ex
builder:
image: gcr.io/paketo-buildpacks/builder:latest
strategy:
name: buildpacks-v3
kind: ClusterBuildStrategy
resources:
limits:
cpu: "1"
memory: "1Gi"
output:
image: ${registry}/${build_name}
credentials:
name: icr-knbuild
@sbose78 you mentioned in our call yesterday that you have some experiences to share about Kaniko. May you do this? Thank you. Specifically, I'd be interested whether the layer caching has been considered. Using the warmer to cache base images looks like a more sophisticated task (in our project's context).
My assessment on the Kaniko caching capabilities and whether they can be applied in build. Kaniko has two types that it caches:
- Layers: this cache is used to cache intermediate layers within a Dockerfile build as the Docker daemon would also do. The cache is enabled through the command line flag
cache. There is a TTL configurable through thecache-ttlcommand line argument which has a default of two weeks. - Base layer caching: this cache is used to cache base images (
FROMin Dockerfile). The cache is also enabled through the samecachecommand line flag. The kaniko warmer image needs to be run to add images to the cache.
Kaniko supports two storages for caching:
- Registry cache (https://github.com/GoogleContainerTools/kaniko/blob/aeaea502e95a6b209a1d44922b260bb3e54f140d/pkg/cache/cache.go#L42-L93). Things are stored in a container registry. The location can be set through the
cache-repocommand line argument. It will fall back to the firstdestination. - Local cache (https://github.com/GoogleContainerTools/kaniko/blob/aeaea502e95a6b209a1d44922b260bb3e54f140d/pkg/cache/cache.go#L110-L136). Things are stored in the file system. The location can be set through the
cache-dircommand line argument, the default is/cache. For Kubernetes, it means that a persistent volume is required that is mounted to this path.
The current Kaniko code limits which storage is used for which cached artifact. The base layer caching only supports a local cache. This makes sense as caching base images just in another repository does not sound so reasonable. Intermediate layers are currently only supported to be cached in a registry cache. That's imo questionable. Caching those in a local cache is imo technically possible and reasonable. The code changes in Kaniko necessary to enable this are imo not complex. An issue exists, https://github.com/GoogleContainerTools/kaniko/issues/1272, but is not answered.
To support the one or other way in the build project, I brainstormed these things:
Important to note upfront is the scope of the caches that would be used. To allow isolation, a caching capability that we introduce should be supported cluster-wide, but also namespace-specific.
For the base layer caching, a persistent volume would be needed. Persistent volumes are cluster-scope resources. To enable namespace-isolation, one would need one persistent volume with reclaim policy delete for each namespace and a matching persistent volume claim in the namespace. Our current capabilities on (cluster) build strategies then would need to be extended. In the build strategy, the volumes section would need to be added. Those volumes then can be referenced in the volumeMounts of the buildSteps. Through that, the cache can be made available to the Kaniko executor. Interesting is the second part = the warming. When would that run? One could run it as part of the build run. We would need one step first that parses the Dockerfile to detect all base images and then pass this into another step, the kaniko warmer, to fill the cache. But then we would not win much. Ideally, the base images are cached before the build run is created. This means that it would need to be filled and updated depending on the build itself. A concept that we have not in our controller so far.
For the intermediate layer caching we have three options:
- Add a cache container registry to the Kaniko configuration (with secret). If this is provided in the build definition, then we set the Kaniko command line arguments
--cache --cache-repo={user-provided}. This is probably not too complicated (beside the conditional command line arguments that we have not yet a mechanism in generate_taskrun). The user would then need to take care of cleaning up old cached layers. This option is not so nice as the user has to provide a repository for this caching purpose and administer it. Anyway, it is what Google Cloud Build is doing, see https://cloud.google.com/cloud-build/docs/kaniko-cache. - The build project manages a container registry (globally or namespace-specific) using either ephemeral storage (it is only for layer caching) or a persistent volume to store the layers through a Kubernetes Deployment. This registry would then need to be exposed to the kaniko executor pod. Secrets would need to be managed automatically. We would also need to take care of cleanup, especially if we use a persistent volume. This is a good user experience for the build user as he does not not need to do anything, but not trivial to implement.
- We get the local cache support for the intermediate layer caching into Kaniko and then use this with a persistent volume as outlined for the base layer caching. Imo also a good user experience as the build user does not need to do anything.
Hi @SaschaSchwarze0 ,
Thanks for the investigation! It is really helpful.
I have some questions:
- For the cluster scope build strategy, if we apply a volume, where it should be? I remember a PVC should be adhere in one namespace. But I think we can cache the base image because some common base images are used frequently
- Don't prefer the cache container registry, who should own or paid for it (as it is a private registry), and the layers are more sensitive because it is related the user source code or info.
As I know, buildpacks may create a PVC in default namespace for some cache, I think we can also take a look that to see if we can leverage something:
✔ $ k get pv
kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-3344b800-fd3b-47a7-9d12-991258ba7258 10Gi RWO Delete Bound default/buildpacks-cache-pvc ibmc-vpc-block-10iops-tier 13d
jordan at jordandembp.cn.ibm.com in ~/Work/workspace/demo
✔ $ k get pvc -A
kubectl get pvc -A
NAMESPACE NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
default buildpacks-cache-pvc Bound pvc-3344b800-fd3b-47a7-9d12-991258ba7258 10Gi RWO ibmc-vpc-block-10iops-tier 13d
But anyway, about Cache enhancement, it should be a new big feature after release v0.1, and I think we can enhance it step by step. Such as start from base image, etc... :)
@SaschaSchwarze0
Options 1 and 2 are somewhat feasible on OpenShift via the internal registry and imagestreams. Based on the referenced docs, it is not clear to me how Kaniko pushes the intermediate layers. If the push happens using tags, then in theory pushing to an ImageStreamTag should work fine. However, if Kaniko is pushing raw blobs then ImageStreams won't work, as this is currently not supported.
Outside of openshift, providing our own registry is IMO beyond the scope of this project.
cc @dmage
@zhangtbj
Persistent volumes can only live on cluster scope. Only the persistent volume claim has namespace support. The idea of using a pre-filled base layer cache with well known base layers is a good idea. For this, we do not need to implement anything here in the build project, I think. Instead, we can handle this through documentation (where it is explained how to create a persistent volume, how this is filled with one or many images and how the cluster build strategy of Kaniko would need to be changed to use this cache. There is one problem with that, and that is with Kaniko. To start using this cache, one will need to start setting the --cache flag for the executor. This has the unfortunate side effect that the destination repository will be used to cache the intermediate layers. Hm, I am really not happy with Kaniko's caching params.
No matter which option we chose for the intermediate layer caching, we will need some persistent resource to store the layers. For a service that is providing the build capability, the payment needs to be clarified, that's clear.
Regarding what to start with. I should have mentioned the impact of the one or other cache:
- the build layer cache is a small improvement in systems where network bandwidth is not a limit because the download of the base images during a Kaniko build really only is a small thing
- the layer cache improves your build time way more
You can compare this with the local Dockerbuilds that you do. If you are missing the base layer locally, then it downloads it and often that goes quick assuming you have a good network connection. Having a Dockerfile with many steps on the other hand where the first steps are the installation of some prereqs that do not rely on your code but take a lot of time, can be greatly speeded up with intermediate layer caching.
--
@adambkaplan
Kaniko is pushing tags, yes. The image name is what is defined in the cache-repo command line argument with a fallback to the first destination command line argument suffixed with /cache. The tag name is calculated and is the cache key. See https://github.com/GoogleContainerTools/kaniko/blob/aeaea502e95a6b209a1d44922b260bb3e54f140d/pkg/executor/push.go#L269