Skip to content

Commit 1bde5d9

Browse files
committed
massive doc updates
* examples/kubernetes: newly added * docs/rootless.md: cleaned up for better readability * examples/README.md: split out from the main README.md * examples/build-using-dockerfile/README.md: split out from the main README.md * README.md: add TOC using https://github.com/thlorenz/doctoc * README.md: add mTLS configuration (relates to moby#1074) * README.md: add more adoptions * README.md: add inline cache (fix moby#976) Signed-off-by: Akihiro Suda <[email protected]>
1 parent 170ab6f commit 1bde5d9

21 files changed

+719
-270
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
bin
2+
.certs
23
.tmp

.github/CONTRIBUTING.md

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ make && sudo make install
6161

6262
You can also use `make binaries-all` to prepare `buildkitd.containerd_only` and `buildkitd.oci_only`.
6363

64+
To build containerized `moby/buildkit:local` and `moby/buildkit:local-rootless` images:
65+
```bash
66+
make images
67+
```
68+
6469
### Run the unit- and integration-tests
6570

6671
Running tests:

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
bin
2+
release-out
3+
.certs
24
.tmp
3-
release-out

README.md

+208-116
Large diffs are not rendered by default.

docs/rootless.md

+51-132
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,77 @@
11
# Rootless mode (Experimental)
22

3-
Requirements:
4-
- runc `a00bf0190895aa465a5fbed0268888e2c8ddfe85` (Oct 15, 2018) or later
5-
- Some distros such as Debian (excluding Ubuntu) and Arch Linux require `sudo sh -c "echo 1 > /proc/sys/kernel/unprivileged_userns_clone"`.
6-
- RHEL/CentOS 7 requires `sudo sh -c "echo 28633 > /proc/sys/user/max_user_namespaces"`. You may also need `sudo grubby --args="namespace.unpriv_enable=1 user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"`.
7-
- `newuidmap` and `newgidmap` need to be installed on the host. These commands are provided by the `uidmap` package. For RHEL/CentOS 7, RPM is not officially provided but available at https://copr.fedorainfracloud.org/coprs/vbatts/shadow-utils-newxidmap/ .
8-
- `/etc/subuid` and `/etc/subgid` should contain >= 65536 sub-IDs. e.g. `penguin:231072:65536`.
3+
Rootless mode allows running BuildKit daemon as a non-root user.
94

5+
## Distribution-specific hint
6+
Using Ubuntu kernel is recommended.
107

11-
## Set up
8+
### Ubuntu
9+
* No preparation is needed.
10+
* `overlayfs` snapshotter is enabled by default ([Ubuntu-specific kernel patch](https://kernel.ubuntu.com/git/ubuntu/ubuntu-bionic.git/commit/fs/overlayfs?id=3b7da90f28fe1ed4b79ef2d994c81efbc58f1144)).
1211

13-
Setting up rootless mode also requires some bothersome steps as follows, but you can also use [`rootlesskit`](https://github.com/rootless-containers/rootlesskit) for automating these steps.
12+
### Debian GNU/Linux
13+
* Add `kernel.unprivileged_userns_clone=1` to `/etc/sysctl.conf` (or `/etc/sysctl.d`) and run `sudo sysctl -p`
14+
* To use `overlayfs` snapshotter (recommended), run `sudo modprobe overlay permit_mounts_in_userns=1` ([Debian-specific kernel patch, introduced in Debian 10](https://salsa.debian.org/kernel-team/linux/blob/283390e7feb21b47779b48e0c8eb0cc409d2c815/debian/patches/debian/overlayfs-permit-mounts-in-userns.patch)). Put the configuration to `/etc/modprobe.d` for persistence.
1415

15-
### Terminal 1:
16+
### Arch Linux
17+
* Add `kernel.unprivileged_userns_clone=1` to `/etc/sysctl.conf` (or `/etc/sysctl.d`) and run `sudo sysctl -p`
1618

17-
```
18-
$ unshare -U -m
19-
unshared$ echo $$ > /tmp/pid
20-
```
19+
### Fedora 31 and later
20+
* Run `sudo grubby --update-kernel=ALL --args="systemd.unified_cgroup_hierarchy=0"` and reboot.
2121

22-
Unsharing mountns (and userns) is required for mounting filesystems without real root privileges.
22+
### Fedora 30
23+
* No preparation is needed
2324

24-
### Terminal 2:
25+
### RHEL/CentOS 8
26+
* No preparation is needed
2527

26-
```
27-
$ id -u
28-
1001
29-
$ grep $(whoami) /etc/subuid
30-
penguin:231072:65536
31-
$ grep $(whoami) /etc/subgid
32-
penguin:231072:65536
33-
$ newuidmap $(cat /tmp/pid) 0 1001 1 1 231072 65536
34-
$ newgidmap $(cat /tmp/pid) 0 1001 1 1 231072 65536
35-
```
28+
### RHEL/CentOS 7
29+
* Add `user.max_user_namespaces=28633` to `/etc/sysctl.conf` (or `/etc/sysctl.d`) and run `sudo sysctl -p`
30+
* Old releases (<= 7.6) require [extra configuration steps](https://github.com/moby/moby/pull/40076).
3631

37-
### Terminal 1:
32+
### Container-Optimized OS from Google
33+
* :warning: Currently unsupported. See [#879](https://github.com/moby/buildkit/issues/879).
3834

39-
```
40-
unshared# buildkitd
41-
```
35+
## Known limitations
36+
* No support for `overlayfs` snapshotter, except on Ubuntu and Debian kernels. We are planning to support `fuse-overlayfs` snapshotter instead for other kernels.
37+
* Network mode is always set to `network.host`.
38+
* No support for `containerd` worker
4239

43-
* The data dir will be set to `/home/penguin/.local/share/buildkit`
44-
* The address will be set to `unix:///run/user/1001/buildkit/buildkitd.sock`
45-
* `overlayfs` snapshotter is not supported except Ubuntu-flavored kernel: http://kernel.ubuntu.com/git/ubuntu/ubuntu-artful.git/commit/fs/overlayfs?h=Ubuntu-4.13.0-25.29&id=0a414bdc3d01f3b61ed86cfe3ce8b63a9240eba7
46-
* containerd worker is not supported ( pending PR: https://github.com/containerd/containerd/pull/2006 )
47-
* Network namespace is not used at the moment.
48-
* Cgroups is disabled.
40+
## Running BuildKit in Rootless mode
4941

50-
### Terminal 2:
42+
[RootlessKit](https://github.com/rootless-containers/rootlesskit/) needs to be installed.
5143

44+
```console
45+
$ rootlesskit buildkitd
5246
```
53-
$ go get ./examples/build-using-dockerfile
54-
$ build-using-dockerfile --buildkit-addr unix:///run/user/1001/buildkit/buildkitd.sock -t foo /path/to/somewhere
47+
48+
```console
49+
$ buildctl --addr unix:///run/user/$UID/buildkit/buildkitd.sock build ...
5550
```
5651

57-
## Set up (using a container)
52+
## Containerized deployment
5853

59-
Docker image is available as [`moby/buildkit:rootless`](https://hub.docker.com/r/moby/buildkit/tags/).
54+
### Kubernetes
55+
See [`../examples/kubernetes`](../examples/kubernetes).
6056

61-
```
62-
$ docker run --name buildkitd -d --privileged -p 1234:1234 moby/buildkit:rootless --addr tcp://0.0.0.0:1234
63-
```
57+
### Docker
6458

59+
```console
60+
$ docker run --name buildkitd -d --security-opt seccomp=unconfined --security-opt apparmor=unconfined moby/buildkit:rootless --oci-worker-no-process-sandbox
61+
$ buildctl --addr docker-container://buildkitd build ...
6562
```
66-
$ go get ./examples/build-using-dockerfile
67-
$ build-using-dockerfile --buildkit-addr tcp://127.0.0.1:1234 -t foo /path/to/somewhere
68-
```
63+
#### About `--oci-worker-no-process-sandbox`
6964

70-
### Security consideration
65+
By adding `--oci-worker-no-process-sandbox` to the `buildkitd` arguments, BuildKit can be executed in a container without adding `--privileged` to `docker run` arguments.
66+
However, you still need to pass `--security-opt seccomp=unconfined --security-opt apparmor=unconfined` to `docker run`.
67+
68+
Note that `--oci-worker-no-process-sandbox` allows build executor containers to `kill` (and potentially `ptrace` depending on the seccomp configuration) an arbitrary process in the BuildKit daemon container.
7169

72-
Although `moby/buildkit:rootless` executes the BuildKit daemon as a normal user, `docker run` still requires `--privileged`.
73-
This is to allow build executor containers to mount `/proc`, by providing "unmasked" `/proc` to the BuildKit daemon container.
70+
To allow running rootless `buildkitd` without `--oci-worker-no-process-sandbox`, run `docker run` with `--security-opt systempaths=unconfined`. (For Kubernetes, set `securityContext.procMount` to `Unmasked`.)
7471

75-
See [`docker/cli#1347`](https://github.com/docker/cli/pull/1347) for the ongoing work to remove this requirement.
76-
See also [Disabling process sandbox](#disabling-process-sandbox).
72+
The `--security-opt systempaths=unconfined` flag disables the masks for the `/proc` mount in the container and potentially allows reading and writing dangerous kernel files, but it is safe when you are running `buildkitd` as non-root.
7773

78-
#### UID/GID
74+
### Change UID/GID
7975

8076
The `moby/buildkit:rootless` image has the following UID/GID configuration:
8177

@@ -101,85 +97,8 @@ user:100000:65536
10197

10298
To change the UID/GID configuration, you need to modify and build the BuildKit image manually.
10399
```
104-
$ vi hack/dockerfiles/test.Dockerfile
105-
$ docker build -t buildkit-rootless-custom --target rootless -f hack/dockerfiles/test.Dockerfile .
106-
```
107-
108-
#### Disabling process sandbox
109-
110-
By passing `--oci-worker-no-process-sandbox` to the `buildkitd` arguments, BuildKit can be executed in a container without `--privileged`.
111-
However, you still need to pass `--security-opt seccomp=unconfined --security-opt apparmor=unconfined` to `docker run`.
112-
113-
```
114-
$ docker run --name buildkitd -d --security-opt seccomp=unconfined --security-opt apparmor=unconfined -p 1234:1234 moby/buildkit:rootless --addr tcp://0.0.0.0:1234 --oci-worker-no-process-sandbox
115-
```
116-
117-
Note that `--oci-worker-no-process-sandbox` allows build executor containers to `kill` (and potentially `ptrace` depending on the seccomp configuration) an arbitrary process in the BuildKit daemon container.
118-
119-
120-
## Set up (using Kubernetes)
121-
122-
### With `securityContext`
123-
124-
```yaml
125-
apiVersion: apps/v1
126-
kind: Deployment
127-
metadata:
128-
labels:
129-
app: buildkitd
130-
name: buildkitd
131-
spec:
132-
selector:
133-
matchLabels:
134-
app: buildkitd
135-
template:
136-
metadata:
137-
labels:
138-
app: buildkitd
139-
spec:
140-
containers:
141-
- image: moby/buildkit:rootless
142-
args:
143-
- --addr
144-
- tcp://0.0.0.0:1234
145-
name: buildkitd
146-
ports:
147-
- containerPort: 1234
148-
securityContext:
149-
privileged: true
150-
```
151-
152-
This configuration requires privileged containers to be enabled.
153-
154-
If you are using Kubernetes v1.12+ with either Docker v18.06+, containerd v1.2+, or CRI-O v1.12+ as the CRI runtime, you can replace `privileged: true` with `procMount: Unmasked`.
155-
156-
### Without `securityContext` but with `--oci-worker-no-process-sandbox`
157-
158-
```yaml
159-
apiVersion: apps/v1
160-
kind: Deployment
161-
metadata:
162-
labels:
163-
app: buildkitd
164-
name: buildkitd
165-
spec:
166-
selector:
167-
matchLabels:
168-
app: buildkitd
169-
template:
170-
metadata:
171-
labels:
172-
app: buildkitd
173-
spec:
174-
containers:
175-
- image: moby/buildkit:rootless
176-
args:
177-
- --addr
178-
- tcp://0.0.0.0:1234
179-
- --oci-worker-no-process-sandbox
180-
name: buildkitd
181-
ports:
182-
- containerPort: 1234
100+
$ vi hack/dockerfiles/test.buildkit.Dockerfile
101+
$ make images
102+
$ docker run ... moby/buildkit:local-rootless ...
183103
```
184104

185-
See [Disabling process sandbox](#disabling-process-sandbox) for security notice.

examples/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# BuildKit Examples
2+
3+
## Kubernetes manifests
4+
- [`./kubernetes`](./kubernetes): Kubernetes manifests (`Pod`, `Deployment`, `StatefulSet`, `Job`)
5+
6+
## CLI examples
7+
- [`./buildctl-daemonless`](./buildctl-daemonless): buildctl without daemon
8+
- [`./build-using-dockerfile`](./build-using-dockerfile): an example BuildKit client with `docker build`-style CLI
9+
10+
## LLB examples
11+
12+
For understanding the basics of LLB, `buildkit*` directory contains scripts that define how to build different configurations of BuildKit itself and its dependencies using the `client` package. Running one of these scripts generates a protobuf definition of a build graph. Note that the script itself does not execute any steps of the build.
13+
14+
You can use `buildctl debug dump-llb` to see what data is in this definition. Add `--dot` to generate dot layout.
15+
16+
```bash
17+
go run examples/buildkit0/buildkit.go \
18+
| buildctl debug dump-llb \
19+
| jq .
20+
```
21+
22+
To start building use `buildctl build` command. The example script accepts `--with-containerd` flag to choose if containerd binaries and support should be included in the end result as well.
23+
24+
```bash
25+
go run examples/buildkit0/buildkit.go \
26+
| buildctl build
27+
```
28+
29+
`buildctl build` will show interactive progress bar by default while the build job is running. If the path to the trace file is specified, the trace file generated will contain all information about the timing of the individual steps and logs.
30+
31+
Different versions of the example scripts show different ways of describing the build definition for this project to show the capabilities of the library. New versions have been added when new features have become available.
32+
33+
- `./buildkit0` - uses only exec operations, defines a full stage per component.
34+
- `./buildkit1` - cloning git repositories has been separated for extra concurrency.
35+
- `./buildkit2` - uses git sources directly instead of running `git clone`, allowing better performance and much safer caching.
36+
- `./buildkit3` - allows using local source files for separate components eg. `./buildkit3 --runc=local | buildctl build --local runc-src=some/local/path`
37+
- `./dockerfile2llb` - can be used to convert a Dockerfile to LLB for debugging purposes
38+
- `./nested-llb` - shows how to use nested invocation to generate LLB
39+
- `./gobuild` - shows how to use nested invocation to generate LLB for Go package internal dependencies
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `build-using-dockerfile` example
2+
3+
:information_source: [BuildKit has been integrated to `docker build` since Docker 18.06.](https://docs.docker.com/develop/develop-images/build_enhancements/)
4+
The `build-using-dockerfile` CLI is just provided as an example for writing a BuildKit client application.
5+
6+
For people familiar with `docker build` command, `build-using-dockerfile` is provided as an example for building Dockerfiles with BuildKit using a syntax similar to `docker build`.
7+
8+
```bash
9+
go get .
10+
11+
build-using-dockerfile -t myimage /path/to/dir
12+
13+
# build-using-dockerfile will automatically load the resulting image to Docker
14+
docker inspect myimage
15+
```

examples/kube-consistent-hash

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kubernetes/consistenthash

examples/kubernetes/README.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Kubernetes manifests for BuildKit
2+
3+
This directory contains Kubernetes manifests for `Pod`, `Deployment` (with `Service`), `StatefulSet`, and `Job`.
4+
* `Pod`: good for quick-start
5+
* `Deployment` + `Service`: good for random load balancing with registry-side cache
6+
* `StateFulset`: good for client-side load balancing, without registry-side cache
7+
* `Job`: good if you don't want to have daemon pods
8+
9+
Using Rootless mode (`*.rootless.yaml`) is recommended because Rootless mode image is executed as non-root user (UID 1000) and doesn't need `securityContext.privileged`.
10+
11+
:warning: Rootless mode may not work on some host kernels. See [`../../docs/rootless.md`](../../docs/rootless.md).
12+
13+
See also ["Building Images Efficiently And Securely On Kubernetes With BuildKit" (KubeCon EU 2019)](https://kccnceu19.sched.com/event/MPX5).
14+
15+
## `Pod`
16+
17+
```console
18+
$ kubectl apply -f pod.rootless.yaml
19+
$ buildctl \
20+
--addr kube-pod://buildkitd \
21+
build --frontend dockerfile.v0 --local context=/path/to/dir --local dockerfile=/path/to/dir
22+
```
23+
24+
If rootless mode doesn't work, try `pod.privileged.yaml`.
25+
26+
:warning: `kube-pod://` connection helper requires Kubernetes role that can access `pods/exec` resources. If `pods/exec` is not accessible, use `Service` instead (See below).
27+
28+
## `Deployment` + `Service`
29+
30+
Setting up mTLS is highly recommended.
31+
32+
`./create-certs.sh SAN [SAN...]` can be used for creating certificates.
33+
```console
34+
$ ./create-certs.sh 127.0.0.1
35+
```
36+
37+
The daemon certificates is created as `Secret` manifest named `buildkit-daemon-certs`.
38+
```console
39+
$ kubectl apply -f .certs/buildkit-daemon-certs.yaml
40+
```
41+
42+
Apply the `Deployment` and `Service` manifest:
43+
```console
44+
$ kubectl apply -f deployment+service.rootless.yaml
45+
$ kubectl scale --replicas=10 deployment/buildkitd
46+
```
47+
48+
Run `buildctl` with TLS client certificates:
49+
```console
50+
$ kubectl port-forward service/buildkitd 1234
51+
$ buildctl \
52+
--addr tcp://127.0.0.1:1234 \
53+
--tlscacert .certs/client/ca.pem \
54+
--tlscert .certs/client/cert.pem \
55+
--tlskey .certs/client/key.pem \
56+
build --frontend dockerfile.v0 --local context=/path/to/dir --local dockerfile=/path/to/dir
57+
```
58+
59+
## `StatefulSet`
60+
`StatefulSet` is useful for consistent hash mode.
61+
62+
```console
63+
$ kubectl apply -f statefulset.rootless.yaml
64+
$ kubectl scale --replicas=10 statefulset/buildkitd
65+
$ buildctl \
66+
--addr kube-pod://buildkitd-4 \
67+
build --frontend dockerfile.v0 --local context=/path/to/dir --local dockerfile=/path/to/dir
68+
```
69+
70+
See `[./consistenthash`](./consistenthash) for how to use consistent hashing.
71+
72+
## `Job`
73+
74+
```console
75+
$ kubectl apply -f job.rootless.yaml
76+
```
77+
78+
To push the image to the registry, you also need to mount `~/.docker/config.json`
79+
and set `$DOCKER_CONFIG` to `/path/to/.docker` directory.

examples/kube-consistent-hash/README.md examples/kubernetes/consistenthash/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Demo for efficiently using BuildKit daemon-local cache with multi-node cluster
55
## Deploy
66

77
```console
8-
$ kubectl apply -f buildkitd-rootless-statefulset.yaml
8+
$ kubectl apply -f ../statefulset.rootless.yaml
99
$ kubectl scale --replicas=10 statefulset/buildkitd
1010
```
1111

@@ -19,7 +19,7 @@ For example, the key can be defined as `<REPO NAME>:<CONTEXT PATH>`, e.g.
1919

2020
Then determine the pod that corresponds to the key:
2121
```console
22-
$ go get ./consistenthash
22+
$ go build -o consistenthash .
2323
$ pod=$(./show-running-pods.sh | consistenthash $key)
2424
```
2525

0 commit comments

Comments
 (0)