Kubernetes from Scratch

Last update: 2022-05-30 07:31:00 +0800 +0800
Available translations: 日本語 / 中文(简体) / 中文(繁體)

Building Kubernetes from source is suitable for studying or testing a patched component. A production cluster also requires a supported upgrade path, certificate rotation, backup recovery, networking, storage, monitoring, and incident procedures.

Object Model and API Paths #

Kubernetes models cluster operations as CRUD on resources. Each object has a UID, a type, a unique name within that type, labels for indexed selection, and annotations for non-identifying metadata.

Resources are grouped into API groups:

  • the core group is exposed under /api/v1;
  • named groups are exposed under /apis/<group>/<version>.

Discovery endpoints such as GET /api, GET /apis, and /openapi/v3 describe which resources the cluster supports. A resource list entry exposes the canonical name, kind, namespace scope, verbs, and short names such as pvc.

The common verbs map to HTTP methods:

  • get, list, and watch use GET;
  • create uses POST;
  • update uses PUT;
  • patch uses PATCH;
  • delete and deletecollection use DELETE.

This object model is why a Service is a stable endpoint backed by many Pods, while a Pod itself remains ephemeral and gets a new network identity when recreated.

Build and Record Binaries #

Build from a tagged Kubernetes source revision with the documented Go toolchain. Record the source commit, toolchain version, build flags, and checksums for every binary. Build selected commands with make WHAT=... or use the release build process; do not download arbitrary binaries into a control-plane host.

Establish the Control Plane #

An odd-sized etcd quorum stores API state. Protect peer and client traffic with TLS, test snapshot restoration, and restrict direct client access. Start the control plane in dependency order:

  1. Establish etcd, certificates, service-account signing keys, and network reachability.
  2. Start kube-apiserver with its serving certificate, etcd client credentials, service CIDR, admission configuration, and audit policy.
  3. Start kube-controller-manager and kube-scheduler with individual identities.
  4. Configure an administrative kubeconfig for initial bootstrap only.
  5. Install a CNI implementation, then join kubelets.

The kubelet requires a node identity, API endpoint, CRI-compatible runtime, CNI configuration, and matching Pod CIDR assumptions. Service routing is implemented by kube-proxy or an alternative data plane.

The control plane components have distinct jobs:

  • etcd stores cluster state;
  • kube-apiserver is the front door for all authenticated requests;
  • kube-controller-manager reconciles desired state into actual state;
  • kube-scheduler picks a node for pending Pods;
  • kubelet keeps one node aligned with the API server;
  • kube-proxy implements Service routing on each node.

Secure API Requests #

The API server accepts HTTPS requests. A request path is:

  1. authentication, which associates the request with a username, UID, groups, and optional extra attributes through client certificates, service-account tokens, or OIDC;
  2. authorization, which evaluates the identity, verb, resource, namespace, and name;
  3. admission, which validates or mutates the request;
  4. persistence to etcd when the request changes cluster state.

Enable Node and RBAC authorization unless a deliberate alternative is designed. Disable anonymous access, avoid static token files, separate component identities, and keep private keys outside source control. Verify /readyz, DNS, Service routing, resource enforcement, node loss, certificate renewal, and etcd restoration before considering the cluster operational.

Comments

Read or comment on GitHub (sign-in required to comment)