Sub topic 5
The API server: the front door of the cluster
This chapter assumes you know what a Pod and a Deployment are, can read YAML, and have used kubectl at least once. It covers kube-apiserver, the component every other part of Kubernetes talks to.
What the API server does
The API server is a REST service. It is the only component that reads and writes cluster state in etcd. Users, kubectl, the scheduler, controllers, and every kubelet all go through it. None of them talk to each other directly to share state.
This gives the cluster one place to enforce rules. Every request passes the same checks, whoever sends it.
What happens to a request
When you run kubectl apply -f deployment.yaml, kubectl sends an HTTPS request to the API server. The server handles it in stages:
- Authentication: who is calling? The server checks client certificates, tokens, or another configured method.
- Authorization: is this caller allowed to perform this action? RBAC is the usual mechanism.
- Admission: controllers can modify the object (mutating) or reject it (validating). Defaults are often filled in here.
- Validation: the object is checked against its schema.
- Persistence: the object is written to etcd.
A request that fails at any stage is rejected, and nothing is stored. A 401 means authentication failed. A 403 means you were identified but not permitted.
Resources and API groups
Every object type is a resource with a path. The apiVersion field in your manifest names the group and version. In the Deployment manifest, apps/v1 selects the apps group, version v1. Core objects such as Pods and Services use v1 with no group name.
You can see the API directly:
kubectl get --raw /api/v1/namespaces/default/pods
kubectl api-resources
kubectl explain deployment.spec.replicaskubectl api-resources lists the resource types the server knows, including any added through custom resources. kubectl explain prints the schema the server uses for validation.
Watches
Components do not poll for changes. They open a watch, a long-lived request that streams events whenever matching objects change. The scheduler watches for Pods with no node assigned. A kubelet watches for Pods assigned to its node. This is how reconciliation loops react quickly without overloading the server.
Each object carries a resourceVersion. It supports optimistic concurrency. If two clients update the same object, the second update fails with a conflict, and the client must re-read and retry.
Common errors
connection refusedusually means the API server is down or your kubeconfig points to the wrong address.Unauthorizedmeans the credentials in your kubeconfig are missing or expired.forbiddenmeans RBAC does not grant your user that verb on that resource.no matches for kindmeans theapiVersionorkindis wrong, or a custom resource definition is not installed.the object has been modifiedmeans aresourceVersionconflict. Fetch the latest version and apply again.
Exercise
A user runs kubectl delete pod web-1 and gets a 403 Forbidden response. Which stage of API server processing rejected the request, and what does this tell you about the user's credentials?
Show answer
Authorization rejected it. A 403 means authentication succeeded, so the API server knows who the user is, but RBAC does not allow that user to delete Pods in that namespace. A failed login would instead return 401.