Containers

A container shares the kernel that a virtual machine brings with it

TWO VIRTUAL MACHINESapp 1a whole guest OSapp 2a whole guest OShypervisorhardwareeach brings its own kernel: heavier, stronger wallTHREE CONTAINERSapp 1app 2app 3container runtimeone host kernel, sharedhardwarethey share one kernel: lighter, start like a processA virtual machine brings its own operating system. A container shares the kernel of its host.A container is an ordinary process that the kernel has fenced in: namespaces limit what it can see,and cgroups limit what it can use. That is why containers start in about a second and pack densely,and also why the wall between them is thinner than between virtual machines.

A virtual machine carries its own OS. A container is a fenced-in process on a shared kernel.

Containers · Going deeper

Namespaces and cgroups

Two kernel features make a containerNamespaces: what a process can seepidIts own list of processes, starting at 1netIts own network interfaces, addresses and portsmntIts own view of the file systemuts, ipc, userIts own hostname, message queues and user numbersControl groups: what a process can usecgroupsCeilings and accounting for CPU, memory, input and outputNamespaces isolate. Cgroups limit. The image supplies the files. Those three are a container.Kubernetes requests and limits are enforced by cgroups. A pod is containers sharing a net namespace.

Namespaces isolate what a process sees. Cgroups limit what it uses.

Images

An image is read-only layers, and a container adds one thin layer

ONE IMAGE: READ-ONLY LAYERSyour applicationlanguage runtimebase files of a Linux distributionbuilt once, never changedTWO CONTAINERS FROM ITthe same image layers, sharedwritable layer of container 1writable layer of container 2delete a container and only its thin layer is lostdata that must survive belongs in a volumeAn image is a stack of read-only layers. A container is that image plus one thin writable layer.Each instruction in a Dockerfile adds a layer. Layers are shared: ten images built on the same basestore that base once, and ten containers from one image share all of it. An image is immutable, sowhat you tested is exactly what runs.

An image is immutable layers. Each container adds a writable layer that dies with it.

Images · Going deeper

Dockerfile instructions

The Dockerfile instructions worth knowingFROMThe base image to start fromCOPYAdd files from your project into the imageRUNRun a command while building, such as installing packagesENVSet an environment variableEXPOSEDocument the port the application listens onUSERThe user to run as; avoid rootENTRYPOINTThe program the container runsCMDDefault arguments, replaceable when the container startsA Dockerfile is the recipe, an image is the result, and a container is a running copy of it.Small images start faster and have less to attack: use a slim base, and build in one stage, run in another.

FROM a base, COPY and RUN to build, ENTRYPOINT and CMD to start.

Check yourself

Aappguest operating systemhypervisorhardwareBappruntimeone shared kernelhardware

Which stack is a container host?

Runtimes

The kubelet asks a runtime, and the runtime asks runc

kubeletasks for the pod'scontainerscontainerdor CRI-Opulls the image andmanages containersruncor crunsets up namespacesand cgroupsthe containera Linux processruns your codeCRIOCIContainer Runtime Interface:how Kubernetes talks to any runtimeOpen Container Initiative:the standard for images and for running themThe kubelet never starts a container itself. It asks a runtime, through a standard interface.A high-level runtime (containerd or CRI-O) pulls images and manages containers. It hands the laststep to a low-level runtime (runc), which makes the kernel calls. Because the kubelet speaks only CRI,any runtime that implements CRI can sit underneath.

kubelet to containerd over CRI, and containerd to runc by the OCI standard.

Runtimes · Going deeper

Runtimes compared

LevelWhat it iscontainerdhighThe most common runtime under Kubernetes; a CNCF graduated projectCRI-OhighA runtime built only for Kubernetes; a CNCF graduated projectrunclowThe reference OCI runtime: starts the container processcrunlowA smaller, faster OCI runtime written in CgVisorsandboxPuts a user-space kernel between container and hostKata ContainerssandboxRuns each pod in a lightweight virtual machineDocker EnginetoolBuilds and runs containers for developers; uses containerd insideKubernetes removed its built-in Docker adapter, dockershim, in version 1.24.Images built with Docker still run everywhere, because they are OCI images. Only the adapter went.A RuntimeClass lets a pod ask for a particular runtime, such as a sandboxed one.

containerd and CRI-O are high-level. runc is low-level. gVisor and Kata sandbox.

Standards

Three OCI standards let you swap any tool for another

BuildDocker, Podman,Buildah, kanikoimage spec: what an image isStore and shareDocker Hub, GHCR,Harbor, Quaydistribution spec: push and pullRuncontainerd, CRI-O,with runc or crunruntime spec: how to run itThree open standards mean any tool can build an image, any registry can hold it, any runtime can run it.The Open Container Initiative (OCI) publishes them. They are the reason "a Docker image" runs oncontainerd, and the reason you can change your build tool, registry or runtime without changingthe other two.

The OCI image, distribution and runtime specs make build tools, registries and runtimes interchangeable.

Standards · Going deeper

Registries and build tools

Who does what around an imageRegistryA server that stores images and serves them by nameRepositoryAll the versions of one image inside a registryHarborAn open source registry you host yourself; a CNCF graduated projectPodmanBuilds and runs containers without a background daemonBuildah, kanikoBuild images without Docker; kaniko builds inside a clusterImage scanningChecking image layers for known vulnerabilities, such as with TrivyImage signingProving who built an image, such as with Sigstore cosignA registry holds repositories, a repository holds tagged images, and an image holds layers.

A registry holds repositories. Harbor is one you host yourself.

Check yourself

kubelet?containerdor CRI-Othe standard the kubelet uses to talk to a container runtime

Which interface belongs at the "?"

Image names

A tag can move. A digest cannot

ghcr.ioregistry: where it livesacme/webrepository: which image1.4.2tag: a movable labelsha256:9f2c…digest: fixed for everMondayweb:latestsha256:9f2c…Fridayweb:latestsha256:41ab…the same tag now names a different imageA tag is a label that can be moved. A digest is a fingerprint of the content, and never changes.An image name is registry, repository and tag. "latest" is only a default tag: it promises nothingabout being new. The digest is computed from the image itself, so pulling by digest always gives thesame bytes. Production manifests should pin a version tag or a digest, never "latest".

Name an image by registry, repository and tag. Only the digest is fixed.

Image names · Going deeper · 1 of 2

Pulling images

How a node gets an imageimagePullPolicy: IfNotPresentPull only if the node does not already hold the imageimagePullPolicy: AlwaysCheck the registry every time a container startsimagePullPolicy: NeverUse only what is already on the nodeimagePullSecretsCredentials for a private registry, kept in a SecretErrImagePullThe pull failed: wrong name, wrong tag or no permissionImagePullBackOffKubernetes is waiting before it tries the pull againA pod stuck at ImagePullBackOff has a problem with the image name or the registry, not with its code.

imagePullPolicy, pull secrets, and what ImagePullBackOff means.

Image names · Going deeper · 2 of 2

Clue words for containerization

When the question saysThe answer isBecause it is"shares the host kernel"Containernot a virtual machine"isolates what a process can see"Namespacespid, net, mnt"limits CPU and memory"cgroupscontrol groups"read-only layers"Imageimmutable"stores and distributes images"RegistryDocker Hub, Harbor"kubelet talks to the runtime through"CRIContainer Runtime Interface"standard for image format and runtime"OCIOpen Container Initiative"default runtime", "graduated"containerdhigh-level runtime"low-level", "creates the container"runcOCI runtime"sandboxed", "extra isolation"gVisor, Kata Containersa second wall"always the same content"Digesta fingerprint"removed in 1.24"dockershimthe Docker adapterCRI is for runtimes, CNI for networking, CSI for storage. Lesson 5 covers the other two.

CRI for runtimes, OCI for the standards, digest for certainty.

Check yourself

Mondayapi:stablesha256:11aa…Fridayapi:stablesha256:77cd…

What does the drawing show?

Lesson complete

You finished “Containerization”

Answer the questions along the way to see your score here.

Open in a new tab