kubernetes: imagePullPolicy to pull latest image OR use existing image if repository unavailable

What would you like to be added?

Add another option for a container’s imagePullPolicy, which attempts to pull the image, and falls back to using a local cached copy if the image could not be pulled.

Why is this needed?

In development, images are frequently updated, so it’s essential to grab the latest version of the image to test against.

In production, single points of failure should be minimized, so if the image is available through the repository OR cached locally, then the container should be able to start up (i.e. neither the repo nor the local cache is a single point of failure).

Currently, imagePullPolicy only offers three options:

  • Always: Only tries to pull the image; ignores cache and fails if repository is unavailable
  • IfNotPresent: First tries to use a cached image; falls back to pulling the image
  • Never: Only tries to use cached image; ignores repository

From these options, Always is the only one that grabs the latest changes, so it’s the only suitable option for development, but it turns the repository into a single point of failure, so it’s not suitable for production unless you’re okay with everything breaking if the repository goes offline.

In addition, using the latest tag (or any other tag that changes, like a v1 tag that gives the latest 1.x.y version) only works if the imagePullPolicy is Always, or the image cache on each node is cleared whenever the tag is updated, so if those tags are used in production, and you don’t want the repository to be a single point of failure, then there is NO suitable option for production at all.

A fourth option, which first tries to pull the image, and falls back to using a cached copy (the inverse of IfNotPresent), would be well-suited to both development and production, and would fit in very nicely in between Always and IfNotPresent.

Honestly, I think this would be the only imagePullPolicy I would ever use for ANYTHING I do (work and personal projects) if it were available, and I don’t understand why it’s missing.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 22
  • Comments: 36 (7 by maintainers)

Most upvoted comments

The problem with this feature is that it promotes the bad practice of using labels in production. If we are ignoring this for a moment, let’s clarify the requirements here. These are requirements listed in the issue description:

  1. Everything is working: Try resolve the tag. Download latest or use one on a disk.
  2. Registry is down: Try to resolve the tag. Failing to resolve tag. Return the image from the disk.

What would be the expected behavior in these edge case - Registry crashed after resolving the tag, let’s say latest (or resolved to non-existing version). So kubelet called PullImage. And runtime knows that the latest is no longer pointing to the version on the disk (successful call to get the metadata). Should runtime remove the latest from the on-disk version? I would say yes. Or it must preserve the tag on the on-disk version, which will be inconsistent behavior? I do not think we have this behavior documented. So there is a good chance that latest is not pointing to the on-disk version of an image after PullImage existed with the error. So GetImage will not return anything. Even if kubelet will call GetImage before the PullImage, the next attempt to use it will fail. So we have not really protected workload from the registry crash.

Also, when using image pull secrets, it is expected that on bootstrap some secretes may not be available for the first pull attempt. If this error to download will result in using the cached version, it may be a very unexpected behavior. Let’s say the images preloaded on a node are very old. Nodes are created from some “template” with these old preloaded images, and every time on bootstrap there will be old image used for the first container.

Another considerartions - if registry intentionally removed the latest tag. Or made it so that current image pull secretes are not valid any longer. Should kubelet and container runtime ignore those errors and still return the latest tagged image?

So implementing this behavior in straightforward way may not protect from all registry failures, and may result in unexpected behavior in edge cases. Thus, more designing effort is needed and since the issue promoting the bad use of labels in production, it is a low priority request.

Whoever wants to pick up this task, a good starting point will be a full writeup on the change, edge cases, and review it with runtime maintainers (cri-o and containerd) as I believe it will require to change cri api.

I had really hoped we could avoid turning this into an argument about the validity of using mutable tags, but here goes.

According to the Kubernetes website, “Kubernetes aims to support an extremely diverse variety of workloads”. I recognize that not everyone would have any use for this new imagePullPolicy, or mutable tags, but that’s okay; nobody is going to force you to use it.

I know the typical recommendation is to use immutable tags so you can pin down the exact version you want to use, but that does NOT suit all use cases. For example, it may be desired to use a tag like v1.2 which will update to the latest 1.2.x version, so you automatically get bug fixes (especially security bug fixes) whenever a pod restarts. For a small business with a small IT team hosting their corporate website, file server, et cetera, it may be preferable to have automatic updates rather than have to constantly check for updates and manually change the configuration to a new version in order to keep the system secure.

And then there’s situations like mine.

We were initially using immutable versioned tags, but we’ve switched to using latest except during development because we realized all we were doing by using immutable tags in testing and demo environments was forcing people to make more configuration changes, because there was no use case for running an outdated version.

The image I’ve been developing is just a monitoring sidecar, which gets added to pods created by over a dozen other companies, and deployed to a private cloud as part of a larger system-of-systems. The system-of-systems as a whole will eventually be installed on production hardware, formally tested (again), and then put into operation for multiple years, where it needs to keep working without system administrators to tend to it. There’s no risk of outdated images in production, because the ONLY version of the image in the entire cloud is the version in the registry we delivered along with the rest of our subsystem. So in production, it doesn’t matter whether we use immutable versioned tags, or latest, or even an SHA hash.

In testing or demos, it’s a different story. Over a dozen companies need to add our sidecar to their pods. With immutable versioned tags, we’d have to tell them all to update their YAML every time we update our image, so it matches what’s in the registry we give them as part of our next build. And of course, until they GET our next build (which is due at the same time as theirs is), that tag wouldn’t work for them. And that’s not even getting into what happens if compatibility issues forced someone to hold back the version of another subsystem in their integration testing environment. It was enough of a pain just going through this with our own test team. By just using latest, the pods will grab whatever version is in our registry, which is also the version that corresponds to the rest of our build they installed.

If, for some reason, the new version of our sidecar interferes with the rest of the pod, they can just remove the sidecar until we’ve fixed the compatibility issue, so there’s no need to roll back to an earlier version. And an earlier version may not work with our latest build, anyway, because we may have made breaking API changes.

So, for us, using latest is what makes the most sense.

+1 on this feature. If you have a private registry inside the K8S environment (e.g., a Gitea container) that craches due to a node crach, other containers won’t be able to spin back up since the registry is down. The registry might come up eventually, but let’s say the registry container depends on a DB container that can’t start either, since the registry is unreachable. Moment 22… Here, an option to always pull if possible, otherwise use a cached image, would be really helpful.

Am I missing something? Is there another solution out there?

Looks like someone linked to this issue in kube-image-keeper, which is a proxy to locally cache images from remote registries. It doesn’t support mutable tags but it sounds like they’re working on it. If they get that working, it would provide somewhat of a workaround for this issue in some cases, by making the local caching proxy the point of failure instead of the remote registry. Whether that’s a better or a worse point of failure would depend on the reliability/stability of kube-image-keeper, and the specific use case (i.e. can you tolerate one or two nodes that can’t start up containers but k8s still tries to assign to them?). In general it’s probably more reliable, as it protects from network disruption, but it increases the complexity of the cluster, and requires a mutating webhook and a local proxy service on each node. If that local proxy fails, then any container on that node that uses Always would fail to start up.

IfAvailable would be a far cleaner solution because it uses the existing cache on each node instead of introducing an additional cache layer.