We use machine learning technology to do auto-translation. Click "English" on top navigation bar to check Chinese version.
Announcing pull through cache for registry.k8s.io in Amazon Elastic Container Registry
Introduction
Container images are stored in registries and pulled into environments where they run. There are many different types of registries from private, self-run registries to public, unauthenticated registries. The registry you use is a direct dependency that can have an impact on how fast you can scale, the security of the software you run, and the availability of your systems. For production environments, it’s recommended that customers limit external dependencies that impact these areas and host container images in a private registry.
Amazon Elastic Container Registry (
At launch, Amazon ECR supported
The
Solution overview
Even with the Kubernetes registry changes, Amazon Web Services and the Kubernetes project recommend customers take ownership of their dependencies to avoid unexpected availability incidents and for disaster recovery. There are two typical options to own upstream container dependencies:
- Manually sync images from one registry to another
- Cache images as they are requested
Syncing images between registries requires you to first identify all of the images and tags that you want to sync and then
A pull through cache is an automatic way to store images in a new repository, when they are requested. The pull through cache automatically creates the image repository in your registry when it’s first requested and keeps the image updated and available for future pulls. You aren’t required to manually identify upstream dependencies or manually sync images when updating your images.
Further benefits for Amazon EKS customers include:
- Reduce image pull time by storing images in the same Region
- Optional automatic replication to multiple Regions and accounts
- Cross account pull permissions
- Image vulnerability scanning and encryption
In addition to those benefits, you also support the upstream Kubernetes project by reducing image pulls from the upstream sources. There is no additional cost to use Amazon ECR pull through cache and standard
Walkthrough
Getting started with Amazon ECR pull through cache
Log into the
Select Add rule and in the Public registry drop down select registry.k8s.io . In the destination tab create a namespace. Cached images keep the same path as upstream, with the namespace prefixed to their path.
If you create the namespace k8s , then your cached images will be available at:
You can also create a pull through cache rule from the Amazon Web Services Command Line Interface (
Use the cache
To test the cache, you can manually pull an image found in
The first time you pull an image using the pull through cache namespace it automatically creates the repository. This command pulls the busybox image, which creates the repository and populates it with the upstream image.
Now you can configure all of your workloads and clusters to pull from the cache instead of the community registry. Here are three examples for how you can use the new cached repositories depending on how you manage your Kubernetes workloads.
Manually update manifests
The first option to use the new cached images is the most straightforward. If you have static Kubernetes manifest files, then you can update the image: field in the manifests to use the new repository. This works for a Git repo full of manifests that are manually applied to the cluster or for a GitOps repo of rendered manifest files.
To identify running workloads in a cluster that use the
Add helm variables to override the repository
If you’re using
You can also set this variable in a
Automatically rewrite registry URI with policy
A third option to use the registry, is to have your image specification modified when jobs are submitted to the cluster. This can be accomplished with a custom webhook or generically with a policy. Here we’ll show you how to write a policy for
The benefit of dynamically rewriting jobs to use a cache is that it also modifies sidecars, init containers, and debug containers that may not have predefined manifests.
You can follow the
Now you can create a ClusterPolicy to perform the registry rewrite dynamically for workloads that try to use the upstream
It’s important to note that this policy may not catch every workload deployed to the cluster depending on the
Additional usage options
Here are some additional ways you can take advantage of a pull through cache with other Amazon ECR features.
Replication and cross-account permissions
Replication rules are only required in the Region where the pull through cache rule is created. You should create a replication rule before pulling images because replication happens when the repository is populated. If the pull through cache rule exists in us-east-1 and we want to replicate to us-west-2 and us-east-2, then we can use the following replication rule.
After the rule has been created, all repositories that are pulled and cached in the primary region are automatically created and replicated to the other Regions.
If you need to pull images from other accounts, then you need to add permissions on each repository in each Region. Make sure the repositories have already been created and replicated before adding cross account permissions.
First create a repo-policy.txt file to allow image pulls from the other accounts needed.
Then get a list of all of the repositories that need the policy.
If the list of repositories looks correct you can set the policy for all of them with this command:
Make sure to repeat that step for each region where you store containers.
Automatic repo creation when worker nodes pull images
Some companies have restrictions on what images can be used in their environments. If you have those restrictions, then you should identify and prepopulate the pull through cache images and labels. Kubernetes worker nodes, by default, won’t be able to pull a new image from a pull through cache because it requires additional Amazon Web Services Identity and Access Management (
If you want to have repositories created automatically when Amazon EKS nodes request upstream images, then you need to add the following Amazon Web Services IAM permission to worker nodes.
If you have a Kubernetes cluster in the same account and Region as the Amazon ECR registry, then you can deploy the following pod to validate image pulls are working.
This pulls the image registry.k8s.io/busybox:latest and caches it in the Amazon ECR repo under the k8s/ namespace.
If your worker nodes are on a private subnet without internet access, then you need to prepopulate the images you want to use because the pull through cache requires internet access to query the upstream registry for image metadata. You can do that from a separate cluster that has internet access or manually via the command line with docker or
Self-hosted Kubernetes
If you are hosting your own Kubernetes control plane on Amazon Web Services you should use the pull through cache for your control plane components. This ensures you can continue to deploy and manage clusters without relying on the community registry.
Tools like
Cleaning up
If you no longer want to use the Amazon ECR pull through cache you can delete it with the following command:
Delete each repository that was created by first listing all of the repositories:
And deleting them with the following command:
You will need to update your Kubernetes manifests, Helm charts, or policy rules to revert the image URI back to registry.k8s.io.
Conclusion
In this post, we showed you how to create a container image pull through cache for Kubernetes images from registry.k8s.io. The upstream Kubernetes registry is run by volunteers in the Kubernetes community and is funded by credits from Amazon Web Services and other cloud providers. There is no on-call schedule or service level agreement (SLA) for availability. While the community has done a fantastic job at scaling it and making it performent—Thank you all!—it’s an external risk to depend on for critical availability.
With a container pull through cache and updated workload definitions, you have additional control of your workload dependencies and reliability. These changes have an initial setup cost, but they help the upstream registry and provide more control and insights into how these images are being used in your environment.
The mentioned AWS GenAI Services service names relating to generative AI are only available or previewed in the Global Regions. Amazon Web Services China promotes AWS GenAI Services relating to generative AI solely for China-to-global business purposes and/or advanced technology introduction.