Kubernetes Development Practice: Code Compilation and Image Building
This guide walks through preparing the hardware and software environment, cloning the Kubernetes source, checking out a specific tag, compiling the code, building release images with appropriate build parameters, extracting and loading the images, and updating static manifests for custom Kubernetes deployments.
1. Environment Preparation
Hardware requirements: 8 GB+ memory, 50 GB+ disk, CentOS 8, kernel 5.10.23.
Software setup:
Install development tools: sudo dnf update sudo dnf groupinstall "Development Tools"
Install Docker: yum install docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io
Install jq: sudo dnf install jq
Install PyYAML: pip install pyyaml
Install etcd (optional for testing): export PATH="$GOPATH/src/k8s.io/kubernetes/third_party/etcd:${PATH}"
Install Go (required version depends on Kubernetes version – see table below)
Compatibility table:
Kubernetes
requires Go
1.0 - 1.2
1.4.2
1.3, 1.4
1.6
1.5, 1.6
1.7 - 1.7.5
1.7
1.8.1
1.8
1.8.3
1.9
1.9.1
1.10
1.9.1
1.11
1.10.2
1.12
1.10.4
1.13
1.11.13
1.14 - 1.16
1.12.9
1.17 - 1.18
1.13.15
1.19 - 1.20
1.15.5
1.21 - 1.22
1.16.7
1.23
1.17
1.24
1.18
1.25+
1.19
2. Clone Code and Branch Preparation
Clone the Kubernetes repository and checkout the desired tag (e.g., v1.20.8):
git clone https://github.com/kubernetes/kubernetes.git git fetch --tags origin v1.20.8Modify the source as needed (not detailed here).
3. Build Preparation
Prepare the required build images for the selected Kubernetes version:
k8s.gcr.io/build-image/kube-cross:v1.15.13-legacy-1
k8s.gcr.io/build-image/go-runner:v2.3.1-go1.15.13-buster.0
k8s.gcr.io/build-image/debian-iptables:buster-v1.6.1These images can be pre‑downloaded to the build node.
4. Compilation and Image Build
Run the make command with specific build parameters:
KUBE_BUILD_PLATFORMS=linux/amd64 WHAT="cmd/kube-controller-manager" KUBE_BUILD_CONFORMANCE=n
KUBE_BUILD_HYPERKUBE=n KUBE_BUILD_PULL_LATEST_IMAGES=n make release-imagesExplanation of parameters:
KUBE_BUILD_PLATFORMS – target architecture (linux/amd64).
WHAT – component to build (may be ignored, building all components).
KUBE_BUILD_CONFORMANCE – run conformance tests.
KUBE_BUILD_HYPERKUBE – create a hyperkube image.
KUBE_BUILD_PULL_LATEST_IMAGES – pull the latest base images.
After about ten minutes, the built image tarballs appear in _output/release-images/amd64/ :
kube-apiserver.tar kube-controller-manager.tar kube-proxy.tar kube-scheduler.tar5. Image Extraction
Load a specific component image into Docker:
docker load -i kube-controller-manager.tarThe image will then be visible via docker images .
6. Using the Custom Image
Update the static pod manifest (e.g., /etc/kubernetes/manifests/kube-controller-manager.yaml ) to reference the newly built image:
image:k8s.gcr.io/kube-controller-manager:v1.20.8
==>
image: k8s.gcr.io/kube-controller-manager-amd64:v1.20.8-2_a16dce2b0c61edAfter editing, the pod restarts with the new image, allowing verification that code changes are effective.
References
https://github.com/kubernetes/community/blob/master/contributors/devel/development.md
https://juejin.cn/post/6844903918720270350
TAL Education Technology
TAL Education is a technology-driven education company committed to the mission of 'making education better through love and technology'. The TAL technology team has always been dedicated to educational technology research and innovation. This is the external platform of the TAL technology team, sharing weekly curated technical articles and recruitment information.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.