| 1 |
# Copyright 2018 The Kubernetes Authors. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
# kind node base image |
| 16 |
# |
| 17 |
# For systemd + docker configuration used below, see the following references: |
| 18 |
# https://systemd.io/CONTAINER_INTERFACE/ |
| 19 |
|
| 20 |
# start from debian slim, this image is reasonably small as a starting point |
| 21 |
# for a kubernetes node image, it doesn't contain much (anything?) we don't need |
| 22 |
# this stage will install basic files and packages |
| 23 |
ARG BASE_IMAGE=debian:bookworm-slim |
| 24 |
FROM $BASE_IMAGE AS base |
| 25 |
|
| 26 |
# copy in static files |
| 27 |
# all scripts and directories are 0755 (rwx r-x r-x) |
| 28 |
# all non-scripts are 0644 (rw- r-- r--) |
| 29 |
COPY --chmod=0755 files/usr/local/bin/* /usr/local/bin/ |
| 30 |
|
| 31 |
COPY --chmod=0644 files/kind/ /kind/ |
| 32 |
# COPY only applies to files, not the directory itself, so the permissions are |
| 33 |
# fixed in RUN below with a chmod. |
| 34 |
COPY --chmod=0755 files/kind/bin/ /kind/bin/ |
| 35 |
|
| 36 |
COPY --chmod=0644 files/LICENSES/* /LICENSES/* |
| 37 |
COPY --chmod=0644 files/etc/* /etc/ |
| 38 |
COPY --chmod=0644 files/etc/containerd/* /etc/containerd/ |
| 39 |
COPY --chmod=0644 files/etc/default/* /etc/default/ |
| 40 |
COPY --chmod=0644 files/etc/sysctl.d/* /etc/sysctl.d/ |
| 41 |
COPY --chmod=0644 files/etc/systemd/system/* /etc/systemd/system/ |
| 42 |
COPY --chmod=0644 files/etc/systemd/system/kubelet.service.d/* /etc/systemd/system/kubelet.service.d/ |
| 43 |
|
| 44 |
# Install dependencies, first from apt, then from release tarballs. |
| 45 |
# NOTE: we use one RUN to minimize layers. |
| 46 |
# |
| 47 |
# The base image already has a basic userspace + apt but we need to install more packages. |
| 48 |
# Packages installed are broken down into (each on a line): |
| 49 |
# - packages needed to run services (systemd) |
| 50 |
# - packages needed for kubernetes components |
| 51 |
# - packages needed for networked backed storage with kubernetes |
| 52 |
# - packages needed by the container runtime |
| 53 |
# - misc packages kind uses itself |
| 54 |
# - packages that provide semi-core kubernetes functionality |
| 55 |
# After installing packages we cleanup by: |
| 56 |
# - removing unwanted systemd services |
| 57 |
# - disabling kmsg in journald (these log entries would be confusing) |
| 58 |
# |
| 59 |
# Then we install containerd from our nightly build infrastructure, as this |
| 60 |
# build for multiple architectures and allows us to upgrade to patched releases |
| 61 |
# more quickly. |
| 62 |
# |
| 63 |
# Next we download and extract crictl and CNI plugin binaries from upstream. |
| 64 |
# |
| 65 |
# Next we ensure the /etc/kubernetes/manifests directory exists. Normally |
| 66 |
# a kubeadm debian / rpm package would ensure that this exists but we install |
| 67 |
# freshly built binaries directly when we build the node image. |
| 68 |
# |
| 69 |
# Finally we adjust tempfiles cleanup to be 1 minute after "boot" instead of 15m |
| 70 |
# This is plenty after we've done initial setup for a node, but before we are |
| 71 |
# likely to try to export logs etc. |
| 72 |
RUN chmod 755 /kind/bin && \ |
| 73 |
echo "Installing Packages ..." \ |
| 74 |
&& DEBIAN_FRONTEND=noninteractive clean-install \ |
| 75 |
systemd \ |
| 76 |
conntrack iptables nftables iproute2 ethtool util-linux mount kmod \ |
| 77 |
libseccomp2 pigz fuse-overlayfs \ |
| 78 |
nfs-common open-iscsi \ |
| 79 |
bash ca-certificates curl jq procps \ |
| 80 |
&& find /lib/systemd/system/sysinit.target.wants/ -name "systemd-tmpfiles-setup.service" -delete \ |
| 81 |
&& rm -f /lib/systemd/system/multi-user.target.wants/* \ |
| 82 |
&& rm -f /etc/systemd/system/*.wants/* \ |
| 83 |
&& rm -f /lib/systemd/system/local-fs.target.wants/* \ |
| 84 |
&& rm -f /lib/systemd/system/sockets.target.wants/*udev* \ |
| 85 |
&& rm -f /lib/systemd/system/sockets.target.wants/*initctl* \ |
| 86 |
&& rm -f /lib/systemd/system/basic.target.wants/* \ |
| 87 |
&& echo "ReadKMsg=no" >> /etc/systemd/journald.conf \ |
| 88 |
&& ln -s "$(which systemd)" /sbin/init |
| 89 |
|
| 90 |
# NOTE: systemd-binfmt.service will register things into binfmt_misc which is kernel-global |
| 91 |
RUN echo "Enabling / Disabling services ... " \ |
| 92 |
&& systemctl enable kubelet.service \ |
| 93 |
&& systemctl enable containerd.service \ |
| 94 |
&& systemctl enable undo-mount-hacks.service \ |
| 95 |
&& systemctl mask systemd-binfmt.service |
| 96 |
|
| 97 |
RUN echo "Ensuring /etc/kubernetes/manifests" \ |
| 98 |
&& mkdir -p /etc/kubernetes/manifests |
| 99 |
|
| 100 |
# shared stage to setup go version for building binaries |
| 101 |
# NOTE we will be cross-compiling for performance reasons |
| 102 |
# This is also why we start again FROM the same base image but a different |
| 103 |
# platform and only the files needed for building |
| 104 |
# We will copy the built binaries from later stages to the final stage(s) |
| 105 |
FROM --platform=$BUILDPLATFORM $BASE_IMAGE AS go-build |
| 106 |
COPY --chmod=0755 files/usr/local/bin/* /usr/local/bin/ |
| 107 |
COPY --chmod=0755 scripts/third_party/gimme/gimme /usr/local/bin/ |
| 108 |
COPY --chmod=0755 scripts/target-cc /usr/local/bin/ |
| 109 |
# tools needed at build-time only |
| 110 |
# first ensure we can install packages for both architectures |
| 111 |
RUN dpkg --add-architecture arm64 && dpkg --add-architecture amd64 \ |
| 112 |
&& clean-install bash ca-certificates curl git make pkg-config \ |
| 113 |
crossbuild-essential-amd64 crossbuild-essential-arm64 \ |
| 114 |
libseccomp-dev:amd64 libseccomp-dev:arm64 |
| 115 |
# set by makefile to .go-version |
| 116 |
ARG GO_VERSION |
| 117 |
RUN eval "$(gimme "${GO_VERSION}")" \ |
| 118 |
&& export GOTOOLCHAIN="go${GO_VERSION}" \ |
| 119 |
&& GOBIN=/usr/local/bin go install github.com/google/go-licenses@latest |
| 120 |
|
| 121 |
|
| 122 |
# stage for building containerd |
| 123 |
FROM go-build AS build-containerd |
| 124 |
ARG TARGETARCH GO_VERSION |
| 125 |
ARG CONTAINERD_VERSION="v2.2.0" |
| 126 |
ARG CONTAINERD_CLONE_URL="https://github.com/containerd/containerd" |
| 127 |
# we don't build with optional snapshotters, we never select any of these |
| 128 |
# they're not ideal inside kind anyhow, and we save some disk space |
| 129 |
ARG BUILDTAGS="no_aufs no_zfs no_btrfs no_devmapper" |
| 130 |
RUN git clone --filter=tree:0 "${CONTAINERD_CLONE_URL}" /containerd \ |
| 131 |
&& cd /containerd \ |
| 132 |
&& git checkout "${CONTAINERD_VERSION}" \ |
| 133 |
&& eval "$(gimme "${GO_VERSION}")" \ |
| 134 |
&& export GOTOOLCHAIN="go${GO_VERSION}" \ |
| 135 |
&& export GOARCH=$TARGETARCH && export CC=$(target-cc) && export CGO_ENABLED=1 \ |
| 136 |
&& make bin/ctr bin/containerd bin/containerd-shim-runc-v2 \ |
| 137 |
&& GOARCH=$TARGETARCH go-licenses save --save_path=/_LICENSES \ |
| 138 |
./cmd/ctr ./cmd/containerd ./cmd/containerd-shim-runc-v2 |
| 139 |
|
| 140 |
# stage for building runc |
| 141 |
FROM go-build AS build-runc |
| 142 |
ARG TARGETARCH GO_VERSION |
| 143 |
# TODO: upgrade to 1.3.x to match containerd after resolving: |
| 144 |
# https://github.com/kubernetes/kubernetes/issues/135214 |
| 145 |
ARG RUNC_VERSION="v1.2.9" |
| 146 |
ARG RUNC_CLONE_URL="https://github.com/opencontainers/runc" |
| 147 |
RUN git clone --filter=tree:0 "${RUNC_CLONE_URL}" /runc \ |
| 148 |
&& cd /runc \ |
| 149 |
&& git checkout "${RUNC_VERSION}" \ |
| 150 |
&& eval "$(gimme "${GO_VERSION}")" \ |
| 151 |
&& export GOTOOLCHAIN="go${GO_VERSION}" \ |
| 152 |
&& export GOARCH=$TARGETARCH && export CC=$(target-cc) && export CGO_ENABLED=1 \ |
| 153 |
&& make runc \ |
| 154 |
&& GOARCH=$TARGETARCH go-licenses save --save_path=/_LICENSES . |
| 155 |
|
| 156 |
# stage for building crictl |
| 157 |
FROM go-build AS build-crictl |
| 158 |
ARG TARGETARCH GO_VERSION |
| 159 |
ARG CRI_TOOLS_CLONE_URL="https://github.com/kubernetes-sigs/cri-tools" |
| 160 |
ARG CRICTL_VERSION="v1.33.0" |
| 161 |
RUN git clone --filter=tree:0 "${CRI_TOOLS_CLONE_URL}" /cri-tools \ |
| 162 |
&& cd /cri-tools \ |
| 163 |
&& git checkout "${CRICTL_VERSION}" \ |
| 164 |
&& eval "$(gimme "${GO_VERSION}")" \ |
| 165 |
&& export GOTOOLCHAIN="go${GO_VERSION}" \ |
| 166 |
&& export GOARCH=$TARGETARCH && export CC=$(target-cc) && export CGO_ENABLED=1 \ |
| 167 |
&& make BUILD_BIN_PATH=./build crictl \ |
| 168 |
&& GOARCH=$TARGETARCH go-licenses save --save_path=/_LICENSES ./cmd/crictl |
| 169 |
|
| 170 |
# stage for building cni-plugins |
| 171 |
FROM go-build AS build-cni |
| 172 |
ARG TARGETARCH GO_VERSION |
| 173 |
ARG CNI_PLUGINS_VERSION="v1.8.0" |
| 174 |
ARG CNI_PLUGINS_CLONE_URL="https://github.com/containernetworking/plugins" |
| 175 |
RUN git clone --filter=tree:0 "${CNI_PLUGINS_CLONE_URL}" /cni-plugins \ |
| 176 |
&& cd /cni-plugins \ |
| 177 |
&& git checkout "${CNI_PLUGINS_VERSION}" \ |
| 178 |
&& eval "$(gimme "${GO_VERSION}")" \ |
| 179 |
&& export GOTOOLCHAIN="go${GO_VERSION}" \ |
| 180 |
&& mkdir ./bin \ |
| 181 |
&& export GOARCH=$TARGETARCH && export CC=$(target-cc) && export CGO_ENABLED=0 \ |
| 182 |
&& go build -o ./bin/host-local -mod=vendor ./plugins/ipam/host-local \ |
| 183 |
&& go build -o ./bin/loopback -mod=vendor ./plugins/main/loopback \ |
| 184 |
&& go build -o ./bin/ptp -mod=vendor ./plugins/main/ptp \ |
| 185 |
&& go build -o ./bin/portmap -mod=vendor ./plugins/meta/portmap \ |
| 186 |
&& GOARCH=$TARGETARCH go-licenses save --save_path=/_LICENSES \ |
| 187 |
./plugins/ipam/host-local \ |
| 188 |
./plugins/main/loopback ./plugins/main/ptp \ |
| 189 |
./plugins/meta/portmap |
| 190 |
|
| 191 |
# stage for building containerd-fuse-overlayfs |
| 192 |
FROM go-build AS build-fuse-overlayfs |
| 193 |
ARG TARGETARCH GO_VERSION |
| 194 |
ARG CONTAINERD_FUSE_OVERLAYFS_VERSION="v2.1.7" |
| 195 |
ARG CONTAINERD_FUSE_OVERLAYFS_CLONE_URL="https://github.com/containerd/fuse-overlayfs-snapshotter" |
| 196 |
RUN git clone --filter=tree:0 "${CONTAINERD_FUSE_OVERLAYFS_CLONE_URL}" /fuse-overlayfs-snapshotter \ |
| 197 |
&& cd /fuse-overlayfs-snapshotter \ |
| 198 |
&& git checkout "${CONTAINERD_FUSE_OVERLAYFS_VERSION}" \ |
| 199 |
&& eval "$(gimme "${GO_VERSION}")" \ |
| 200 |
&& export GOTOOLCHAIN="go${GO_VERSION}" \ |
| 201 |
&& export GOARCH=$TARGETARCH && export CC=$(target-cc) && export CGO_ENABLED=1 \ |
| 202 |
&& make bin/containerd-fuse-overlayfs-grpc \ |
| 203 |
&& GOARCH=$TARGETARCH go-licenses save --save_path=/_LICENSES ./cmd/containerd-fuse-overlayfs-grpc |
| 204 |
|
| 205 |
|
| 206 |
# build final image layout from other stages |
| 207 |
FROM base AS build |
| 208 |
# copy over containerd build and install |
| 209 |
COPY --from=build-containerd /containerd/bin/containerd /usr/local/bin/ |
| 210 |
COPY --from=build-containerd /containerd/bin/ctr /usr/local/bin/ |
| 211 |
COPY --from=build-containerd /containerd/bin/containerd-shim-runc-v2 /usr/local/bin/ |
| 212 |
RUN ctr oci spec \ |
| 213 |
| jq '.hooks.createContainer[.hooks.createContainer| length] |= . + {"path": "/kind/bin/mount-product-files.sh"}' \ |
| 214 |
| jq 'del(.process.rlimits)' \ |
| 215 |
> /etc/containerd/cri-base.json \ |
| 216 |
&& containerd --version |
| 217 |
COPY --from=build-containerd /_LICENSES/* /LICENSES/ |
| 218 |
# copy over runc build and install |
| 219 |
COPY --from=build-runc /runc/runc /usr/local/sbin/runc |
| 220 |
RUN runc --version |
| 221 |
COPY --from=build-runc /_LICENSES/* /LICENSES/ |
| 222 |
# copy over crictl build and install |
| 223 |
COPY --from=build-crictl /cri-tools/build/crictl /usr/local/bin/ |
| 224 |
COPY --from=build-crictl /_LICENSES/* /LICENSES/ |
| 225 |
# copy over CNI plugins build and install |
| 226 |
RUN mkdir -p /opt/cni/bin |
| 227 |
COPY --from=build-cni /cni-plugins/bin/host-local /opt/cni/bin/ |
| 228 |
COPY --from=build-cni /cni-plugins/bin/loopback /opt/cni/bin/ |
| 229 |
COPY --from=build-cni /cni-plugins/bin/ptp /opt/cni/bin/ |
| 230 |
COPY --from=build-cni /cni-plugins/bin/portmap /opt/cni/bin/ |
| 231 |
COPY --from=build-cni /_LICENSES/* /LICENSES/ |
| 232 |
# copy over containerd-fuse-overlayfs and install |
| 233 |
COPY --from=build-fuse-overlayfs /fuse-overlayfs-snapshotter/bin/containerd-fuse-overlayfs-grpc /usr/local/bin/ |
| 234 |
COPY --from=build-fuse-overlayfs /_LICENSES/* /LICENSES/ |
| 235 |
|
| 236 |
# squash down to one compressed layer, without any lingering whiteout files etc |
| 237 |
FROM scratch |
| 238 |
COPY --from=build / / |
| 239 |
# add metadata, must be done after the squashing |
| 240 |
# first tell systemd that it is in docker (it will check for the container env) |
| 241 |
# https://systemd.io/CONTAINER_INTERFACE/ |
| 242 |
ENV container=docker |
| 243 |
# systemd exits on SIGRTMIN+3, not SIGTERM (which re-executes it) |
| 244 |
# https://bugzilla.redhat.com/show_bug.cgi?id=1201657 |
| 245 |
STOPSIGNAL SIGRTMIN+3 |
| 246 |
# NOTE: this is *only* for documentation, the entrypoint is overridden later |
| 247 |
ENTRYPOINT [ "/usr/local/bin/entrypoint", "/sbin/init" ] |