Skip to content

Stargz Snapshotterを使用した遅延プル

⚡ 必要条件 nerdctl >= 0.0.1

遅延プルは、イメージのプルが完了する前にコンテナーを実行する手法です。

詳細については、https://github.com/containerd/stargz-snapshotter を参照してください。

asciicast

nerdctl実行の遅延プルを有効にする

注意 ルートレスインストールについては、rootless.md を参照してください。

  • https://github.com/containerd/stargz-snapshotter から Stargz プラグイン (containerd-stargz-grpc) をインストールします。

  • 以下を /etc/containerd/config.toml に追加します。

    [proxy_plugins]
      [proxy_plugins.stargz]
        type = "snapshot"
        address = "/run/containerd-stargz-grpc/containerd-stargz-grpc.sock"
    

  • containerd と containerd-stargz-grpc を起動します。

  • nerdctl--snapshotter=stargzをつけて実行します

    # nerdctl --snapshotter=stargz run -it --rm ghcr.io/stargz-containers/fedora:30-esgz
    

変換済みのStargzイメージのリストについては、https://github.com/containerd/stargz-snapshotter/blob/main/docs/pre-converted-images.md を参照してください。

ベンチマーク結果 (2020年12月9日)

For running python3 -c print("hi"), eStargz with Stargz Snapshotter is 3-4 times faster than the legacy OCI with overlayfs snapshotter.

overlayfs snapshotter を使用したレガシーOCI:

# time nerdctl --snapshotter=overlayfs run -it --rm ghcr.io/stargz-containers/python:3.7-org python3 -c 'print("hi")'
ghcr.io/stargz-containers/python:3.7-org:                                         resolved       |++++++++++++++++++++++++++++++++++++++|
index-sha256:6008006c63b0a6043a11ac151cee572e0c8676b4ba3130ff23deff5f5d711237:    done           |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:48eafda05f80010a6677294473d51a530e8f15375b6447195b6fb04dc2a30ce7: done           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:f860607a6cd9751ac8db2f33cbc3ce1777a44eb3c04853e116763441a304fbf6:    done           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:96b2c1e36db5f5910f58da2ca4f9311b0690810c7107fb055ee1541498b5061f:    done           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:c495e8de12d26c9843a7a2bf8c68de1e5652e66d80d9bc869279f9af6f86736a:    done           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:33382189822a108b249cf3ccd234d04c3a8dfe7d593df19c751dcfab3675d5f2:    done           |++++++++++++++++++++++++++++++++++++++|
config-sha256:94c9a318e47ab8a318582e2712bb495f92f17a7c1e50f13cc8a3e362c1b09290:   done           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:6eaa0b6b8562fb4a02e140ae53b3910fc4d0db6e68660390eaef993f42e21102:    done           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:adbdcbacafe93bf0791e49c8d3689bb78d9e60d02d384d4e14433aedae39f52c:    done           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:756975cb9c7e7933d824af9319b512dd72a50894232761d06ef3be59981df838:    done           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:d77915b4e630d47296770ce4cf481894885978072432456615172af463433cc5:    done           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:5f37a0a41b6b03489dd7de0aa2a79e369fd8b219bbc36b52f3f9790dc128e74b:    done           |++++++++++++++++++++++++++++++++++++++|
elapsed: 41.9s                                                                    total:  321.3  (7.7 MiB/s)                                       
hi

real    0m51.754s                                                                                         
user    0m2.687s
sys     0m5.533s

Stargz Snapshotterを使用したeStargz:

# time nerdctl --snapshotter=stargz run -it --rm ghcr.io/stargz-containers/python:3.7-esgz python3 -c 'print("hi")'
fetching sha256:2ea0dd96... application/vnd.oci.image.index.v1+json
fetching sha256:9612ff73... application/vnd.docker.distribution.manifest.v2+json
fetching sha256:34e5920e... application/vnd.docker.container.image.v1+json
hi

real    0m13.589s
user    0m0.132s
sys     0m0.158s

nerdctl ビルド中に基本イメージをプルするための遅延プルを有効にする

  • --oci-worker-snapshotter=stargz (containerd workerを使用する場合は--containerd-worker-snapshotter=stargz)を指定してbuildkitdを起動します
  • nerdctl build を起動します。nerdctl--snapshotter を指定する必要はありません。

nerdctl build を使用した stargz イメージのビルド

$ nerdctl build -t example.com/foo .
$ nerdctl image convert --estargz --oci example.com/foo example.com/foo:estargz
$ nerdctl push example.com/foo:estargz

注意: --estargz--ociと組み合せて指定する必要があります

Stargz イメージの構築に Stargz Snapshotter は必要ありません。

イメージ変換のヒント

ヒント1:より小さなeStargzイメージを作成する

nerdctl image convertでは、オプションでより小さなeStargzイメージを作成するために、次のフラグを使用できます。 結果のイメージでは、遅延プルのために stargz-snapshotter >= v0.13.0 が必要です。

  • --estargz-min-chunk-size: 指定した最小バイト数のデータを 1 つの gzip ストリームに書き込む指示をする。もし> 0 の場合、複数のファイルとチャンクを 1 つの gzip ストリームに書き込むことができます。gzip ヘッダーの数が少なくなり、結果の BLOB のサイズが小さくなることが予想されます。--estargz-min-chunk-size=0 は通常の eStargz を生成します。

  • --estargz-external-toc: 分かれた TOC JSON メタデータを他のイメージに書き込む ( "TOC image"とよぶ)。結果のeStargzにはTOCが含まれていないため、通常のeStargzよりも小さいサイズが期待できます。これは実験的な機能です。

--estargz-min-chunk-size usage

変換:

# nerdctl image convert --oci --estargz --estargz-min-chunk-size=50000 ghcr.io/stargz-containers/ubuntu:22.04 registry2:5000/ubuntu:22.04-chunk50000
# nerdctl image ls
REPOSITORY                          TAG                 IMAGE ID        CREATED           PLATFORM       SIZE        BLOB SIZE
ghcr.io/stargz-containers/ubuntu    22.04               20fa2d7bb4de    14 seconds ago    linux/amd64    83.4 MiB    29.0 MiB
registry2:5000/ubuntu               22.04-chunk50000    562e09e1b3c1    2 seconds ago     linux/amd64    0.0 B       29.2 MiB
# nerdctl push --insecure-registry registry2:5000/ubuntu:22.04-chunk50000

遅延プル

# nerdctl pull --snapshotter=stargz --insecure-registry registry2:5000/ubuntu:22.04-chunk50000
# mount | grep "stargz on"
stargz on /var/lib/containerd-stargz-grpc/snapshotter/snapshots/1/fs type fuse.rawBridge (rw,nodev,relatime,user_id=0,group_id=0,allow_other)

--estargz-external-toc 使用法

変換:

# nerdctl image convert --oci --estargz --estargz-external-toc ghcr.io/stargz-containers/ubuntu:22.04 registry2:5000/ubuntu:22.04-ex
INFO[0005] Extra image(0) registry2:5000/ubuntu:22.04-ex-esgztoc
sha256:3059dd5d9c404344e0b7c43d9782de8cae908531897262b7772103a0b585bbee
# nerdctl images
REPOSITORY                          TAG                 IMAGE ID        CREATED           PLATFORM       SIZE        BLOB SIZE
ghcr.io/stargz-containers/ubuntu    22.04                20fa2d7bb4de    9 seconds ago    linux/amd64    83.4 MiB    29.0 MiB
registry2:5000/ubuntu               22.04-ex             3059dd5d9c40    1 second ago     linux/amd64    0.0 B       30.8 MiB
registry2:5000/ubuntu               22.04-ex-esgztoc     18c042b6eb8b    1 second ago     linux          0.0 B       151.3 KiB

次に、eStargz(registry2:5000 / ubuntu:22.04-ex)とTOCイメージ(registry2:5000 / ubuntu:22.04-ex-esgztoc)を同じレジストリにプッシュします(この例ではregistry2が使用されていますが、任意のレジストリを使用できます)。``

# nerdctl push --insecure-registry registry2:5000/ubuntu:22.04-ex
# nerdctl push --insecure-registry registry2:5000/ubuntu:22.04-ex-esgztoc

遅延プル

# nerdctl pull --insecure-registry --snapshotter=stargz registry2:5000/ubuntu:22.04-ex

Stargz Snapshotter は、同じレジストリ上の TOC イメージを自動的に参照します。

オプションで--estargz-keep-diff-id フラグにより、レイヤーの diffID を変更せずに変換できます。

nerdctl イメージ変換は、--estargz-external-toc で指定されたオプションのフラグ--estargz-keep-diff-id をサポートします。 これにより、diffID(非圧縮ダイジェスト)を変更せずにイメージがeStargzに変換されるため、eStargzに依存しないgzip解凍プログラム(gunzipなど)でも元のtarブロブを復元できます。

# nerdctl image convert --oci --estargz --estargz-external-toc --estargz-keep-diff-id ghcr.io/stargz-containers/ubuntu:22.04 registry2:5000/ubuntu:22.04-ex-keepdiff
# nerdctl push --insecure-registry registry2:5000/ubuntu:22.04-ex-keepdiff
# nerdctl push --insecure-registry registry2:5000/ubuntu:22.04-ex-keepdiff-esgztoc
# crane --insecure blob registry2:5000/ubuntu:22.04-ex-keepdiff@sha256:2dc39ba059dcd42ade30aae30147b5692777ba9ff0779a62ad93a74de02e3e1f | jq -r '.rootfs.diff_ids[]'
sha256:7f5cbd8cc787c8d628630756bcc7240e6c96b876c2882e6fc980a8b60cdfa274
# crane blob ghcr.io/stargz-containers/ubuntu:22.04@sha256:2dc39ba059dcd42ade30aae30147b5692777ba9ff0779a62ad93a74de02e3e1f | jq -r '.rootfs.diff_ids[]'
sha256:7f5cbd8cc787c8d628630756bcc7240e6c96b876c2882e6fc980a8b60cdfa274

ヒント2:gzipの代わりにzstdを使用する(別名zstd:chunked)

gzipの代わりに、遅延プルサポート(別名zstd:chunked)を備えたzstd圧縮を使用できます。

  • 長所
  • 圧縮/解凍の高速化
  • 短所
  • 古いツールではサポートされていない可能性があります。また、一部のツールではまだサポートされていません。
  • min-chunk-size, external-toc (ヒント 1に記載した) は、サポートされません。
$ nerdctl build -t example.com/foo .
$ nerdctl image convert --zstdchunked --oci example.com/foo example.com/foo:zstdchunked
$ nerdctl push example.com/foo:zstdchunked