Debian Codegraph @

docs/overview.md

raw
1 # Containerd Stargz Snapshotter Plugin Overview
2
3 __Before get through this overview document, we recommend you to read [README](../README.md).__
4
5 Pulling image is one of the time-consuming steps in the container startup process.
6 In containerd community, we have had a lot of discussions to address this issue as the following,
7
8 - [#3731 Support remote snapshotter to speed up image pulling](https://github.com/containerd/containerd/issues/3731)
9 - [#2968 Support `Prepare` for existing snapshots in Snapshotter interface](https://github.com/containerd/containerd/issues/2968)
10 - [#2943 remote filesystem snapshotter](https://github.com/containerd/containerd/issues/2943)
11
12 The solution for the fast image distribution is called *Remote Snapshotter* plugin.
13 This prepares container's rootfs layers by directly mounting from remote stores instead of downloading and unpacking the entire image contents.
14 The actual image contents can be fetched *lazily* so runtimes can startup containers before the entire image contents to be locally available.
15 We call these remotely mounted layers as *remote snapshots*.
16
17 *Stargz Snapshotter* is a remote snapshotter plugin implementation which supports standard compatible remote snapshots functionality.
18 This snapshotter leverages [eStargz](/docs/stargz-estargz.md) image, which is lazily-pullable and still standard-compatible.
19 Because of this compatibility, eStargz image can be pushed to and lazily pulled from [OCI](https://github.com/opencontainers/distribution-spec)/[Docker](https://docs.docker.com/registry/spec/api/) registries (e.g. ghcr.io).
20 Furthermore, images can run even on eStargz-agnostic runtimes (e.g. Docker).
21 When you run a container image and it is formatted by eStargz, stargz snapshotter prepares container's rootfs layers as remote snapshots by mounting layers from the registry to the node, instead of pulling the entire image contents.
22
23 This document gives you a high-level overview of stargz snapshotter.
24
25 ![overview](/docs/images/overview01.png)
26
27 ## Stargz Snapshotter proxy plugin
28
29 Stargz snapshotter is implemented as a [proxy plugin](https://github.com/containerd/containerd/blob/04985039cede6aafbb7dfb3206c9c4d04e2f924d/PLUGINS.md#proxy-plugins) daemon (`containerd-stargz-grpc`) for containerd.
30 When containerd starts a container, it queries the rootfs snapshots to stargz snapshotter daemon through an unix socket.
31 This snapshotter remotely mounts queried eStargz layers from registries to the node and provides these mount points as remote snapshots to containerd.
32
33 Containerd recognizes this plugin through an unix socket specified in the configuration file (e.g. `/etc/containerd/config.toml`).
34 Stargz snapshotter can also be used through Kubernetes CRI by specifying the snapshotter name in the CRI plugin configuration.
35 We assume that you are using containerd (> v1.4.2).
36
37 ```toml
38 version = 2
39
40 # Plug stargz snapshotter into containerd
41 # Containerd recognizes stargz snapshotter through specified socket address.
42 # The specified address below is the default which stargz snapshotter listen to.
43 [proxy_plugins]
44 [proxy_plugins.stargz]
45 type = "snapshot"
46 address = "/run/containerd-stargz-grpc/containerd-stargz-grpc.sock"
47
48 # Use stargz snapshotter through CRI
49 [plugins."io.containerd.grpc.v1.cri".containerd]
50 snapshotter = "stargz"
51 disable_snapshot_annotations = false
52 ```
53
54 This repo contains [a Dockerfile as a KinD node image](/Dockerfile) which includes the above configuration.
55
56 ## State directory
57
58 Stargz snapshotter mounts eStargz layers from registries to the node using FUSE.
59 The all files metadata in the image are preserved on the filesystem and files contents are fetched from registries on demand.
60
61 At the root of the filesystem, there is a *state directory* (`/.stargz-snapshotter`) for status monitoring for the filesystem.
62 This directory is hidden from `getdents(2)` so you can't see this with `ls -a /`.
63 Instead, you can directly access the directory by specifying the path (`/.stargz-snapshotter`).
64
65 State directory contains JSON-formatted metadata files for each layer.
66 In the following example, metadata JSON files for overlayed 7 layers are visible.
67 In each metadata JSON file, the following fields are contained,
68
69 - `digest` contains the layer digest. This is the same value as that in the image's manifest.
70 - `size` is the size bytes of the layer.
71 - `fetchedSize` and `fetchedPercent` indicate how many bytes have been fetched for this layer. Stargz snapshotter aggressively downloads this layer in the background - unless configured otherwise - so these values gradually increase. When `fetchedPercent` reaches to `100` percents, this layer has been fully downloaded on the node and no further access will occur for reading files.
72
73 Note that the state directory layout and the metadata JSON structure are subject to change.
74
75 ```console
76 # ctr-remote run --rm -t --snapshotter=stargz docker.io/stargz/golang:1.12.9-esgz test /bin/bash
77 root@1d43741b8d29:/go# ls -a /
78 . bin dev go lib media opt root sbin sys usr
79 .. boot etc home lib64 mnt proc run srv tmp var
80 root@1d43741b8d29:/go# ls /.stargz-snapshotter/*
81 /.stargz-snapshotter/sha256:2b1fc65cafe05b65acc9e9f186df4dd81ae74c58ef73d89ecfc15e7286b3e960.json
82 /.stargz-snapshotter/sha256:42d56485c1f672e394a02855048774621731c8fd44a54dc816a421a3a52b8482.json
83 /.stargz-snapshotter/sha256:6a5826d877de5c93fb4a9e1d0369cfdef6d43df2610562501ebf42e4bcb2ef73.json
84 /.stargz-snapshotter/sha256:a4d35801573274df19d9c2ae2aed80eba96d5aa69a38c464e1f01f9abf81e34e.json
85 /.stargz-snapshotter/sha256:ab13100112faac6e04d2da2281db3df942efc8cef2532ab2cac688c6232944d8.json
86 /.stargz-snapshotter/sha256:e8cc31024eb09fe216ad906392aec139038330c6d29dfd3fe5c81c4b2dd21430.json
87 /.stargz-snapshotter/sha256:f077511be7d385c17ba88980379c5cd0aab7068844dffa7a1cefbf68cc3daea3.json
88 root@1d43741b8d29:/go# cat /.stargz-snapshotter/*
89 {"digest":"sha256:2b1fc65cafe05b65acc9e9f186df4dd81ae74c58ef73d89ecfc15e7286b3e960","size":131339690,"fetchedSize":7939690,"fetchedPercent":6.045156646859757}
90 {"digest":"sha256:42d56485c1f672e394a02855048774621731c8fd44a54dc816a421a3a52b8482","size":10047608,"fetchedSize":2047608,"fetchedPercent":20.379059374131632}
91 {"digest":"sha256:6a5826d877de5c93fb4a9e1d0369cfdef6d43df2610562501ebf42e4bcb2ef73","size":54352828,"fetchedSize":2302828,"fetchedPercent":4.236813584014432}
92 {"digest":"sha256:a4d35801573274df19d9c2ae2aed80eba96d5aa69a38c464e1f01f9abf81e34e","size":70359295,"fetchedSize":2259295,"fetchedPercent":3.211082487395588}
93 {"digest":"sha256:ab13100112faac6e04d2da2281db3df942efc8cef2532ab2cac688c6232944d8","size":7890588,"fetchedSize":2140588,"fetchedPercent":27.12837116828302}
94 {"digest":"sha256:e8cc31024eb09fe216ad906392aec139038330c6d29dfd3fe5c81c4b2dd21430","size":52934435,"fetchedSize":2634435,"fetchedPercent":4.976788738748227}
95 {"digest":"sha256:f077511be7d385c17ba88980379c5cd0aab7068844dffa7a1cefbf68cc3daea3","size":580,"fetchedSize":580,"fetchedPercent":100}
96 ```
97
98 ## Registry-related configuration
99
100 You can configure stargz snapshotter for accessing registries with custom configurations.
101 The config file must be formatted with TOML and can be passed to stargz snapshotter with `--config` option.
102
103 ### Authentication
104
105 Stargz snapshotter doesn't share private registries creds with containerd.
106 Instead, this supports authentication in the following methods,
107
108 - Using `$DOCKER_CONFIG` or `~/.docker/config.json`
109 - Proxying and scanning CRI Image Service API
110 - Using Kubernetes secrets (type = `kubernetes.io/dockerconfigjson`)
111
112 #### dockerconfig-based authentication
113
114 By default, This snapshotter tries to get creds from `$DOCKER_CONFIG` or `~/.docker/config.json`.
115 Following example enables stargz snapshotter to access to private registries using `docker login` command. [`nerdctl login`](https://github.com/containerd/nerdctl) can also be used for this.
116 Stargz snapshotter doesn't share credentials with containerd so credentials specified by `ctr-remote`'s `--user` option in the example is just for containerd.
117
118 ```console
119 # docker login
120 (Enter username and password)
121 # ctr-remote image rpull --user <username>:<password> docker.io/<your-repository>/ubuntu:18.04
122 ```
123
124 #### CRI-based authentication
125
126 Following configuration (typically located at `/etc/containerd-stargz-grpc/config.toml`) enables stargz snapshotter to pull private images on Kubernetes.
127 The snapshotter works as a proxy of CRI Image Service and exposes CRI Image Service API on the snapshotter's unix socket (i.e. `/run/containerd-stargz-grpc/containerd-stargz-grpc.sock`).
128 The snapshotter acquires registry creds by scanning requests.
129
130 You must specify `--image-service-endpoint=unix:///run/containerd-stargz-grpc/containerd-stargz-grpc.sock` option to kubelet.
131
132 ```toml
133 # Stargz Snapshotter proxies CRI Image Service into containerd socket.
134 [cri_keychain]
135 enable_keychain = true
136 image_service_path = "/run/containerd/containerd.sock"
137 ```
138
139 #### kubeconfig-based authentication
140
141 This is another way to enable lazy pulling of private images on Kubernetes.
142
143 Following configuration (typically located at `/etc/containerd-stargz-grpc/config.toml`) enables stargz snapshotter to access to private registries using kubernetes secrets (type = `kubernetes.io/dockerconfigjson`) in the cluster using kubeconfig files.
144 You can specify the path of kubeconfig file using `kubeconfig_path` option.
145 It's no problem that the specified file doesn't exist when this snapshotter starts.
146 In this case, snapsohtter polls the file until actually provided.
147 This is useful for some environments (e.g. single node cluster with containerized apiserver) where stargz snapshotter needs to start before everything, including booting containerd/kubelet/apiserver and configuring users/roles.
148 If no `kubeconfig_path` is specified, snapshotter searches kubeconfig files from `$KUBECONFIG` or `~/.kube/config`.
149
150 ```toml
151 # Use Kubernetes secrets accessible by the kubeconfig `/etc/kubernetes/snapshotter/config.conf`.
152 [kubeconfig_keychain]
153 enable_keychain = true
154 kubeconfig_path = "/etc/kubernetes/snapshotter/config.conf"
155 ```
156
157 Please note that kubeconfig-based authentication requires additional privilege (i.e. kubeconfig to list/watch secrets) to the node.
158 And this doesn't work if kubelet retrieve creds from somewhere not API server (e.g. [credential provider](https://kubernetes.io/docs/tasks/kubelet-credential-provider/kubelet-credential-provider/)).
159
160 ### Registry mirrors and insecure connection
161
162 You can also configure mirrored registries and insecure connection.
163 The hostname used as a mirror host can be specified using `host` option.
164 If an optional field `insecure` is `true`, snapshotter tries to connect to the registry using plain HTTP instead of HTTPS.
165
166 ```toml
167 # Use `mirrorhost.io` as a mirrored host of `exampleregistry.io` and
168 # use plain HTTP for connecting to the mirror host.
169 [[resolver.host."exampleregistry.io".mirrors]]
170 host = "mirrorhost.io"
171 insecure = true
172
173 # Use plain HTTP for connecting to `exampleregistry.io`.
174 [[resolver.host."exampleregistry.io".mirrors]]
175 host = "exampleregistry.io"
176 insecure = true
177 ```
178
179 `header` field allows to set headers to send to the server.
180
181 ```toml
182 [[resolver.host."registry2:5000".mirrors]]
183 host = "registry2:5000"
184 [resolver.host."registry2:5000".mirrors.header]
185 x-custom-2 = ["value3", "value4"]
186 ```
187
188 > NOTE: Headers aren't passed to the redirected location.
189
190 The config file can be passed to stargz snapshotter using `containerd-stargz-grpc`'s `--config` option.
191
192 ## Make your remote snapshotter
193
194 It isn't difficult for you to implement your remote snapshotter using [our general snapshotter package](/snapshot) without considering the protocol between that and containerd.
195 You can configure the remote snapshotter with your `FileSystem` structure which you want to use as a backend filesystem.
196 [Our snapshotter command](/cmd/containerd-stargz-grpc/main.go) is a good example for the integration.