Repository navigation
Description
How to categorize this issue?
/kind bug
What happened:
After enabling a registry running in proxy mode, we started observing containerd image pull failures:
level=error msg="PullImage \"registry.k8s.io/kube-apiserver@sha256:5b28a364467cf7e134343bb3ee2c6d40682b473a743a72142c7bbe25767d36eb\" failed" error="failed to pull and unpack image \"registry.k8s.io/kube-apiserver@sha256:5b28a364467cf7e134343bb3ee2c6d40682b473a743a72142c7bbe25767d36eb\": failed to read expected number of bytes: unexpected EOF"
The underlying error is that for a blob the registry returns an empty response with the header Content-Length: 103742:
curl -X GET http://10.243.55.183:5000/v2/kube-apiserver/blobs/sha256:07a64a71e01156f8f99039bc246149925c6d1480d3957de78510bbec6ec68f7a -vv
Note: Unnecessary use of -X or --request, GET is already inferred.
* Trying 10.243.55.183:5000...
* Connected to 10.243.55.183 (10.243.55.183) port 5000 (#0)
> GET /v2/kube-apiserver/blobs/sha256:07a64a71e01156f8f99039bc246149925c6d1480d3957de78510bbec6ec68f7a HTTP/1.1
> Host: 10.243.55.183:5000
> User-Agent: curl/7.88.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: max-age=31536000
< Content-Length: 103742
< Content-Type: application/octet-stream
< Docker-Content-Digest: sha256:07a64a71e01156f8f99039bc246149925c6d1480d3957de78510bbec6ec68f7a
< Docker-Distribution-Api-Version: registry/2.0
< Etag: "sha256:07a64a71e01156f8f99039bc246149925c6d1480d3957de78510bbec6ec68f7a"
< X-Content-Type-Options: nosniff
< Date: Fri, 15 Dec 2023 09:45:59 GMT
<
* transfer closed with 103742 bytes remaining to read
* Closing connection 0
curl: (18) transfer closed with 103742 bytes remaining to read
The corresponding access log entry is:
time="2023-12-15T06:16:03.447853969Z" level=info msg="response completed" go.version=go1.20.8 http.request.host="10.243.55.183:5000" http.request.id=6aaf180d-d448-4f9c-82ea-7c3da20106d0 http.request.method=GET http.request.remoteaddr="10.64.4.1:14159" http.request.uri="/v2/kube-apiserver/blobs/sha256:07a64a71e01156f8f99039bc246149925c6d1480d3957de78510bbec6ec68f7a?ns=registry.k8s.io" http.request.useragent="containerd/1.6.20~ds1" http.response.contenttype="application/octet-stream" http.response.duration=1.575773ms http.response.status=200 http.response.written=0
Note the http.response.written=0 part. Hence, obviously the Content-Length: 103742 header does not match with the written response (http.response.written=0) and this is causing clients to fail with:
failed to read expected number of bytes: unexpected EOFfor containerdcurl: (18) transfer closed with 103742 bytes remaining to readfor curl
The issue happens when images that share common layers are cached in the registry proxy.
What you expected to happen:
The registry proxy to do not delete blobs when these blobs are still referenced by other images and are needed for the successful pull of these images. Otherwise, reigstry proxy returns wrong HTTP responses (with Content-Length header and with empty response) which makes clients to fail.
How to reproduce it (as minimally and precisely as possible):
For the steps to reproduce the following 2 images will be used - registry.k8s.io/kube-apiserver:v1.28.4 and registry.k8s.io/kube-controller-manager:v1.28.4.
The corresponding linux/arm64 manifests are:
% docker manifest inspect registry.k8s.io/kube-apiserver@sha256:a4c3e6bec39f5dcb221a2f08266513ab19b7d977ccc76a0bcaf04d4935ac0fb2
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 2579,
"digest": "sha256:04b4c447bb9d4840af3bf7e836397379d65df87c86e55dcd27f31a8d11df2419"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 83959,
"digest": "sha256:6ec9caf47f8f176a2299fbfc83c403d27535e8937c9bcca775308eb2ae467486"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 21202,
"digest": "sha256:fe5ca62666f04366c8e7f605aa82997d71320183e99962fa76b3209fdfbb8b58"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 716491,
"digest": "sha256:b02a7525f878e61fc1ef8a7405a2cc17f866e8de222c1c98fd6681aff6e509db"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 317,
"digest": "sha256:fcb6f6d2c9986d9cd6a2ea3cc2936e5fc613e09f1af9042329011e43057f3265"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 198,
"digest": "sha256:e8c73c638ae9ec5ad70c49df7e484040d889cca6b4a9af056579c3d058ea93f0"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 113,
"digest": "sha256:1e3d9b7d145208fa8fa3ee1c9612d0adaac7255f1bbc9ddea7e461e0b317805c"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 385,
"digest": "sha256:4aa0ea1413d37a58615488592a0b827ea4b2e48fa5a77cf707d0e35f025e613f"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 355,
"digest": "sha256:7c881f9ab25e0d86562a123b5fb56aebf8aa0ddd7d48ef602faf8d1e7cf43d8c"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 130562,
"digest": "sha256:5627a970d25e752d971a501ec7e35d0d6fdcd4a3ce9e958715a686853024794a"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 679123,
"digest": "sha256:c22227ffc2bebe262fe3064bfebb7d0b1d54cf18fcd331edbfe90b510fa0c521"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 29943095,
"digest": "sha256:4b48a9a58e216927daa4ac0e90b5f09a103e310d415204f4218895b006245bff"
}
]
}% docker manifest inspect registry.k8s.io/kube-controller-manager@sha256:fe49ea386d014cbf10cd16f53900b91bb7e7c32c5cf4fdcea550c33bedfdc92e
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 2425,
"digest": "sha256:9961cbceaf234d59b7dcf8a197a024f3e3ce4b7fe2b67c2378efd3d209ca994b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 83959,
"digest": "sha256:6ec9caf47f8f176a2299fbfc83c403d27535e8937c9bcca775308eb2ae467486"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 21202,
"digest": "sha256:fe5ca62666f04366c8e7f605aa82997d71320183e99962fa76b3209fdfbb8b58"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 716491,
"digest": "sha256:b02a7525f878e61fc1ef8a7405a2cc17f866e8de222c1c98fd6681aff6e509db"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 317,
"digest": "sha256:fcb6f6d2c9986d9cd6a2ea3cc2936e5fc613e09f1af9042329011e43057f3265"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 198,
"digest": "sha256:e8c73c638ae9ec5ad70c49df7e484040d889cca6b4a9af056579c3d058ea93f0"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 113,
"digest": "sha256:1e3d9b7d145208fa8fa3ee1c9612d0adaac7255f1bbc9ddea7e461e0b317805c"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 385,
"digest": "sha256:4aa0ea1413d37a58615488592a0b827ea4b2e48fa5a77cf707d0e35f025e613f"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 355,
"digest": "sha256:7c881f9ab25e0d86562a123b5fb56aebf8aa0ddd7d48ef602faf8d1e7cf43d8c"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 130562,
"digest": "sha256:5627a970d25e752d971a501ec7e35d0d6fdcd4a3ce9e958715a686853024794a"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 679123,
"digest": "sha256:c22227ffc2bebe262fe3064bfebb7d0b1d54cf18fcd331edbfe90b510fa0c521"
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 28721044,
"digest": "sha256:e36efbbb27622d68e958432979d5a26aaaa23f4b59ef154e9b751b6032d478b8"
}
]
}Note that the first 10 layers are the same (have the same digest), the 11th layers is different.
Steps to reproduce:
- Run a registry in proxy mode with upstream for
registry.k8s.io. The following config is used
version: 0.1
log:
fields:
service: registry
storage:
delete:
enabled: true
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: /var/lib/registry
http:
addr: :5000
debug:
addr: :5001
prometheus:
enabled: true
path: /metrics
headers:
X-Content-Type-Options: [nosniff]
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3
proxy:
remoteurl: https://registry.k8s.io- Pull
registry.k8s.io/kube-apiserver:v1.28.4andregistry.k8s.io/kube-controller-manager:v1.28.4from the registry proxy
Before pulling the next image, remove the pulled image so that it does not reuse the cached layers locally:
# ctr images pull --hosts-dir "/etc/containerd/certs.d" registry.k8s.io/kube-apiserver:v1.28.4
# ctr images rm registry.k8s.io/kube-apiserver:v1.28.4
# ctr images pull --hosts-dir "/etc/containerd/certs.d" registry.k8s.io/kube-controller-manager:v1.28.4
# ctr images rm registry.k8s.io/kube-controller-manager:v1.28.4
After the above image pulls, the scheduler-state.json file of the proxy should have duplicated entries for the layers in common between the 2 images. For example:
"kube-apiserver@sha256:1e3d9b7d145208fa8fa3ee1c9612d0adaac7255f1bbc9ddea7e461e0b317805c": {
"Key": "kube-apiserver@sha256:1e3d9b7d145208fa8fa3ee1c9612d0adaac7255f1bbc9ddea7e461e0b317805c",
"ExpiryData": "2023-12-22T12:59:01.123986259Z",
"EntryType": 0
},
"kube-apiserver@sha256:4aa0ea1413d37a58615488592a0b827ea4b2e48fa5a77cf707d0e35f025e613f": {
"Key": "kube-apiserver@sha256:4aa0ea1413d37a58615488592a0b827ea4b2e48fa5a77cf707d0e35f025e613f",
"ExpiryData": "2023-12-22T12:59:01.068447217Z",
"EntryType": 0
},
"kube-apiserver@sha256:6ec9caf47f8f176a2299fbfc83c403d27535e8937c9bcca775308eb2ae467486": {
"Key": "kube-apiserver@sha256:6ec9caf47f8f176a2299fbfc83c403d27535e8937c9bcca775308eb2ae467486",
"ExpiryData": "2023-12-22T12:59:00.963760467Z",
"EntryType": 0
},
// 7 more repeated layers for kube-apiserver
// ...
"kube-controller-manager@sha256:1e3d9b7d145208fa8fa3ee1c9612d0adaac7255f1bbc9ddea7e461e0b317805c": {
"Key": "kube-controller-manager@sha256:1e3d9b7d145208fa8fa3ee1c9612d0adaac7255f1bbc9ddea7e461e0b317805c",
"ExpiryData": "2023-12-22T12:59:40.116410221Z",
"EntryType": 0
},
"kube-controller-manager@sha256:4aa0ea1413d37a58615488592a0b827ea4b2e48fa5a77cf707d0e35f025e613f": {
"Key": "kube-controller-manager@sha256:4aa0ea1413d37a58615488592a0b827ea4b2e48fa5a77cf707d0e35f025e613f",
"ExpiryData": "2023-12-22T12:59:39.604159805Z",
"EntryType": 0
},
"kube-controller-manager@sha256:6ec9caf47f8f176a2299fbfc83c403d27535e8937c9bcca775308eb2ae467486": {
"Key": "kube-controller-manager@sha256:6ec9caf47f8f176a2299fbfc83c403d27535e8937c9bcca775308eb2ae467486",
"ExpiryData": "2023-12-22T12:59:40.032550138Z",
"EntryType": 0
},
// 7 more repeated layers for kube-controller-manager
// ...
Note that the layers sha256s will be different depending on the host architecture. The above examples are from linux/arm64.
- Edit the
scheduler-state.jsonfile: set one of the common layers to expire for 1 of the images by updating theExpiryDatafield. This step simulates expiration of one of the images before the other one.
Then restart the proxy. When the expiry data is passed, then it should delete the blob:
time="2023-12-15T12:25:19.121661502Z" level=info msg="Deleting blob: /docker/registry/v2/blobs/sha256/6e/6ec9caf47f8f176a2299fbfc83c403d27535e8937c9bcca775308eb2ae467486" go.version=go1.20.8 instance.id=03b7c75d-de68-4b12-9ec3-93ee372bb54f service=registry version=2.8.3
The corresponding blob (6ec9caf47f8f176a2299fbfc83c403d27535e8937c9bcca775308eb2ae467486) is removed from /var/lib/registry/docker/registry/v2/blobs/sha256 while the corresponding kube-controller-manager layer with the same digest exists:
# cat /var/lib/registry/docker/registry/v2/repositories/kube-controller-manager/_layers/sha256/6ec9caf47f8f176a2299fbfc83c403d27535e8937c9bcca775308eb2ae467486/link
sha256:6ec9caf47f8f176a2299fbfc83c403d27535e8937c9bcca775308eb2ae467486
- Try to pull
registry.k8s.io/kube-controller-manager:v1.28.4
# ctr images pull --hosts-dir "/etc/containerd/certs.d" registry.k8s.io/kube-controller-manager:v1.28.4
registry.k8s.io/kube-controller-manager:v1.28.4: resolving |--------------------------------------|
elapsed: 0.5 s total: 0.0 B (0.0 B/s)
ctr: failed to read expected number of bytes: unexpected EOF
Ensure it fails with the above error.
Environment:
registry version: v2.8.3
Activity
ialidzhikov commentedon Dec 15, 2023
cc @milosgajdos @thaJeztah
milosgajdos commentedon Dec 18, 2023
Yeah, the proxy hasnt received my effort. We might need to remove it from v4 completely unless there is some sizeable effort the community is willing to put into it. If it were up to me I'd remove it from v3 as well, just like we removed the client that has received similar amount of attention.
ialidzhikov commentedon Jan 11, 2024
Hi @milosgajdos 👋
I tried to reach over Slack but let me also try to reach here.
In the context of this issue, I was wondering how deletion of an image works in the normal registry cache case. I read this morning about garbage collection binary (https://distribution.github.io/distribution/about/garbage-collection/). Do I understand right that delete requests against a normal registry (not a pull through cache) actually only remove the corresponding manifests/layers (which are actually only references to the blobs) and do NOT delete blobs. According to my understanding blobs seems to be deleted only by the garbage collection binary. Can you confirm this? I see that the GC binary only deletes blob that are no longer referenced.
So, I was wondering whether we could reuse the same approach for the proxy. I mean to adapt the proxy to do not delete blobs directly but to run the GC binary (as sidecar or as cronjob) to delete the blobs. WDYT?
The part that deletes the blob in the context of the above issue is
distribution/registry/proxy/proxyregistry.go
Lines 79 to 82 in d0f5aa6
v.RemoveBlobcall after we make sure that the corresponding digest is not referenced by any other manifest in the repository (or in the scheduler-state.json file).@milosgajdos do you know of a good way how such check can be performed? I was thinking about listing all manifests in the repo, then all of their references. But I am unsure about this approach as meanwhile a new image can be added that references the blob that just gets deleted.
ialidzhikov commentedon Jan 11, 2024
As far as I see from the code, deleting a layer (
DELETE /v2/<name>/blobs/<digest>) only removes the layer but does NOT delete the blob. TheblobHandlercallsblobs.Deletedistribution/registry/handlers/blob.go
Line 80 in d0f5aa6
distribution/registry/storage/linkedblobstore.go
Lines 211 to 228 in d0f5aa6
distribution/registry/proxy/proxyregistry.go
Lines 72 to 82 in d0f5aa6
blobs.Delete. When I debugged this, blobs.Delete only deletes the layer/the reference. Thev.RemoveBlobcall is the actual call that deletes the blob.When I also check for usages of
vacuum.Delete, it is only called by the garbage collector (distribution/registry/storage/garbagecollect.go
Line 144 in 152af63
If some of the maintainers can confirm my observations, then that would be great.
If all of the above is true, then IMO the best option would be to require the garbage collector to be ran as sidecar or cronjob. I suggest to drop
distribution/registry/proxy/proxyregistry.go
Lines 78 to 81 in d0f5aa6
OnBlobExpireand let the garbage collector delete unreferenced blobs.WDYT?
cc @thaJeztah @Jamstah (I am tagging other folks in case @milosgajdos is OOO)
ialidzhikov commentedon Jan 12, 2024
According to my testing, the proxy does not delete blobs for manifests. The corresponding handling is:
distribution/registry/proxy/proxyregistry.go
Lines 86 to 107 in d0f5aa6
I pulled
alpine:3.19.0via the pull through cache. I took a snapshot of the file system with tree. Then I adapted thescheduler-state.jsonfile to expire all blobs/manifest. Below I attach the diff of the file system before and after expiration of blobs and manifests:You can see that blobs for manifests are not removed. I don't know if it is expected (implemented like this on purpose) or not.
So currently there is already a discrepancy how blobs and manifests are deleted. Blobs are deleted by removing the soft link and the blob content. Manifest are deleted by removing the soft link only.
Requiring the
garbage-collectto the proxy would also solve the above problem.ialidzhikov commentedon Jan 12, 2024
There is the following note about the garbage collection which is a little bit worrying:
I am not sure if this constraint is applicable for the proxy. 🤔 WDYT? Is it?
milosgajdos commentedon Jan 14, 2024
I'm on sabbatical for a couple of months -- I only occasionally check my GH notifications on the phone and do what's only possible via the GH app. There are a lot of other maintainers who can pick this up.
Why is this worrying? This makes perfect sense: take GC-collected languages -- there is a very tiny "stop-the-world" moment when the unreferenced memory is cleaned up. Until that's done nothing can mess about with the memory that's about to be cleaned; the key is to avoid race conditions.
I should also let you know that the GC as it is in this code is in a dire state - it was a cute OG attempt but it doesn't scale and has a boat load of bugs in it. Many orgs have their own in-house registry GC solutions for that very reason.
ialidzhikov commentedon Jan 15, 2024
My first idea to tackle the issue was to follow what it is followed in the normal registry case. I asked many times above whether a maintainer can confirm my observations. Again, as far I see for a normal registry, you cannot delete a blob directly, you can only delete the image layers (references to blobs) and the actual deletion of blobs happens via the GC. Can you confirm this?
My initial idea was to run the GC as a cronjob - for example once every day. But it is now getting worse with the requirement the registry to be in readonly mode. I am not sure if proxy at all respects the corresponding setting. I think for our use-case it is not okay to put the registry in readonly mode everyday.
Ok, enjoy. Who from the maintainers can support me with this issue?
:)
I was also thinking that we see occurrences of this issue mainly due to the hard-coded TTL in v2.8.3. As you know, it is hard-coded to
7din v2.8.3. In v3.0.0-alpha.1 it is configurable and we could mitigate the number of occurrences of this issue by setting a higher TTL (for example90d). Configurable TTL also means increased proxy effectiveness. Right now, with7dits effectiveness is low because it deletes its blobs every7d.But we again come to the question with the releases for distribution/distribution. I asked many times in #4156 for a GA release. I also asked this morning in Slack: https://cloud-native.slack.com/archives/C01GVR8SY4R/p1705303309837879
The open PR for cherry-picking configurable TTL to release/2.8 was closed in favor of release v3.0.0: #4090 (comment)
Can someone cut v3.0.0 so that we could mitigate this issue?
ialidzhikov commentedon Jan 16, 2024
I see that there is #2367 which is the same issue as this one.
Mitigate distribution/distribution#4191 by disabling blobdescriptor c…
Mitigate distribution/distribution#4191 by disabling blobdescriptor c…
ialidzhikov commentedon Jan 16, 2024
The mitigation #2367 (comment) works for us. I will close this issue as duplicate of #2367.
Mitigate distribution/distribution#4191 by disabling blobdescriptor c…