Containerd: Building a Container Supervisor by Michael Crosby

21
Building a Container Supervisor Michael Crosby

Transcript of Containerd: Building a Container Supervisor by Michael Crosby

Page 1: Containerd: Building a Container Supervisor by Michael Crosby

Building a Container Supervisor

MichaelCrosby

Page 2: Containerd: Building a Container Supervisor by Michael Crosby

Docker since 0.3 - maintainerdockerui - authorlibcontainer - authornsinit - authorrunc - authorOCI - maintainercontainerd - author

> whoami

Page 3: Containerd: Building a Container Supervisor by Michael Crosby

containerd

● Fast, lightweight container supervisor● runc (OCI) multiplexer● Container lifecycle operations● rm -rf docker/daemon/execdrivers

> man containerd

Page 4: Containerd: Building a Container Supervisor by Michael Crosby

● runc integration● Multiple runtime support● Execution v2● Decouple Execution from filesystem● daemonless containers● cleaner development

> why

Page 5: Containerd: Building a Container Supervisor by Michael Crosby

● lock free event loop● concurrency control

○ 10 > 100 at a time● easier to new developers

> events

Page 6: Containerd: Building a Container Supervisor by Michael Crosby

Benchmarks

> ./benchmark -count 100

INFO[0001] 1.149902846 seconds

Page 7: Containerd: Building a Container Supervisor by Michael Crosby

Managing state is easy when you don’t have any.

Don’t keep anything in memory.

> container state

Page 8: Containerd: Building a Container Supervisor by Michael Crosby

Restore

> containerd --debug

DEBU[0000] containerd: container restored id=0

DEBU[0000] containerd: container restored id=1

DEBU[0000] containerd: container restored id=2

DEBU[0000] containerd: container restored id=3

DEBU[0000] containerd: container restored id=4

DEBU[0000] containerd: container restored id=5

DEBU[0000] containerd: container restored id=6

...

Page 9: Containerd: Building a Container Supervisor by Michael Crosby

● Exit code● TTY / STDIO● Reparenting to sysinit

> shim

Page 10: Containerd: Building a Container Supervisor by Michael Crosby

● Pipe + File● O_CLOEXEC● Multiple subscribers

> exit status

Page 11: Containerd: Building a Container Supervisor by Michael Crosby

O_CLOEXEC

if (mkfifo("exit-fifo", 0666) != 0) {

printf("%s\n", strerror(errno));

exit(EXIT_FAILURE);

}

int fd = open("exit-fifo", O_WRONLY | O_CLOEXEC, 0);

Page 12: Containerd: Building a Container Supervisor by Michael Crosby

● FIFOs - the good, bad, and the stupid● open() never blocks :trollface:● fifos have a buffer

○ /proc/sys/fs/pipe-max-size

> stdio reattach

Page 13: Containerd: Building a Container Supervisor by Michael Crosby

● prctl - PR_SET_CHILD_SUBREAPER● system init

> re-parenting

Page 14: Containerd: Building a Container Supervisor by Michael Crosby

1. Your parent is the process that forked you, your mommy2. If your parent dies, your new parent is PID 1*, the creator3. If the parent(s) of your parent has the subreaper set, they will become

your parent not PID 1, your nana4. If you die then your parent dies before doing a wait4(), you’re a

zombie

> re-parenting rules

Page 15: Containerd: Building a Container Supervisor by Michael Crosby

PR_SET_CHILD_SUBREAPER

> ./parent

main() parent 27538

child process 27540 with parent 27539

parent 27539 exiting

child process 27540 with new parent 2391

> ps x | grep 2391

2391 ? Ss 0:00 /sbin/upstart --user

Page 16: Containerd: Building a Container Supervisor by Michael Crosby

PR_SET_CHILD_SUBREAPER

> ./parent --subreaper

main() parent 27543

child process 27545 with parent 27544

parent 27544 exiting

child process 27545 with new parent 27543

Page 17: Containerd: Building a Container Supervisor by Michael Crosby

How do you connect to OOM notifications before the user process starts?

> The OOM Problem

Page 18: Containerd: Building a Container Supervisor by Michael Crosby

● create○ initialize namespaces and config

● start○ exec the user’s process

● delete○ destroy the container

> runtime workflow

Page 19: Containerd: Building a Container Supervisor by Michael Crosby

1. Create container2. Register OOM handler3. Exec the user's process

> runtime workflow

Page 20: Containerd: Building a Container Supervisor by Michael Crosby

https://github.com/crosbymichael/dockercon-2016

> code

Page 21: Containerd: Building a Container Supervisor by Michael Crosby

Thank you!