Lesson 10 · Section 2: Core Concepts
Lesson 10: Kube-API Server: the front door of the cluster
What the API server does, the path every request takes through authentication, authorization and admission, how kubeadm runs it as a static Pod, and how to fix it when kubectl stops answering. · 22 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why the API server exists
A cluster is many programs on many machines: a scheduler, controllers, a kubelet on every node, and you with
kubectl. All of them need the same picture of what should be running. That picture lives in etcd, but if every
program read and wrote etcd directly, each one would need its own access rules, its own validation and its own way
of learning about changes. One bug in one client could corrupt the cluster's state.
So Kubernetes puts a single program in front of the data: kube-apiserver. It is the only component that talks to etcd. Everything else, including the rest of the control plane, is a client that sends it HTTPS requests. That gives the cluster one place to check who is calling, decide what they may do, refuse objects that make no sense, and tell everyone who is interested when something changes.
What the API server is
Parts:
- kubectl (you, or CI)
- kube-apiserver (HTTPS :6443), in Control plane node
- etcd (:2379, the only state), in Control plane node
- Controller manager (watches, then fixes), in Control plane node
- Scheduler (watches unbound Pods), in Control plane node
- kubelet (watches its Pods), in Every node
- kube-proxy (watches Services), in Every node
Connections:
- kubectl to kube-apiserver: REST
- kube-apiserver to etcd: read / write
- Controller manager to kube-apiserver: watch
- Scheduler to kube-apiserver
- kubelet and kube-apiserver
- kube-proxy to kube-apiserver
The API server is a stateless REST server. It keeps no data of its own: every object you have ever applied is in etcd, and the API server is the gatekeeper and translator in front of it. Four jobs are worth knowing by name:
- It serves the Kubernetes API. Every resource is a URL. Core resources live under
/api/v1, everything else under/apis/<group>/<version>.kubectl get pods -n shopis aGETto/api/v1/namespaces/shop/pods, andkubectl get deployments -n shopis aGETto/apis/apps/v1/namespaces/shop/deployments. - It guards the door. Each request is authenticated, authorized and passed through admission control before it can touch the data (the next section follows one through).
- It stores and versions objects. It validates an object, writes it to etcd, and stamps it with a
resourceVersionso that two writers cannot silently overwrite each other. - It notifies watchers. Clients can keep a
watchrequest open and receive every change as it happens. This is how the scheduler learns about a new Pod and how the kubelet learns it must start one.
That last point explains how the whole cluster cooperates. No component gives another component orders. When you create a Deployment, the Deployment controller sees it through its watch and creates a ReplicaSet; the ReplicaSet controller sees that and creates Pods; the scheduler sees Pods without a node and writes a node name into each; the kubelet on that node sees a Pod assigned to it and starts the containers. Every step is "watch the API server, do one small thing, write the result back".
How a request travels through it
Parts:
- kubectl apply (HTTPS to :6443)
- 1. Authentication (Who are you? 401)
- 2. Authorization (May you? 403)
- 3. Mutating (admission: defaults)
- 4. Schema check (valid object? 422)
- 5. Validating (admission: policy)
- 6. etcd (stored: 201 Created)
- Watchers (controllers, kubelet)
Connections:
- kubectl apply to 1. Authentication
- 1. Authentication to 2. Authorization
- 2. Authorization to 3. Mutating
- 3. Mutating to 4. Schema check
- 4. Schema check to 5. Validating
- 5. Validating to 6. etcd
- 6. etcd to Watchers: watch event
Every request passes the same stations, in the same order. Knowing the order tells you where an error comes from.
- Authentication: who are you? The API server tries its authenticators in turn: client certificates (the
certificate's common name is the user name, its organizations are the groups), bearer tokens such as
ServiceAccount tokens, and others such as OIDC. Kubernetes has no User object: a user is whatever name a
trusted credential carries. If nothing accepts the credential, the answer is
401 Unauthorized. - Authorization: may you do this? The request is reduced to a few facts (user, groups, verb, resource,
namespace) and shown to the authorizers listed in
--authorization-mode, in order. kubeadm setsNode,RBAC: the Node authorizer covers what kubelets need, and RBAC covers everything else. If no authorizer allows the request, the answer is403 Forbidden. - Mutating admission. Admission plugins that may change the object run first. This is where a Pod gets its default ServiceAccount and token volume, where a LimitRange fills in missing requests and limits, and where a PersistentVolumeClaim gets the default StorageClass.
- Schema validation. The object, as it now stands, is checked against the API's schema: required fields, types,
allowed values. A failure is
422 Unprocessable Entity, shown by kubectl asThe Pod "x" is invalid: .... - Validating admission. Plugins that may only say yes or no run last, so they judge the final object. ResourceQuota, Pod Security admission and validating webhooks live here.
- Persistence. The object is written to etcd, the client gets
201 Created, and every open watch for that resource receives an event.
Reads (get, list, watch) stop after step 2: admission only sees requests that create, update, delete or
connect. So admission can stop you from creating a Pod, but it can never hide one from you.
Under the hood · Details that explain odd behaviour
- Authorizers can pass. Each authorizer answers allow, deny or "no opinion". The first allow or deny wins; a request that nobody has an opinion about is denied. RBAC itself has no deny rules: it only allows, so a missing permission is always a missing Role or binding, never a rule that forbids.
- Anonymous requests. A request with no credentials at all is not rejected at once. It becomes the user
system:anonymousand is then judged by authorization, which is why an unauthenticatedcurlto most paths returns 403 rather than 401, while/livez,/readyzand/versionanswer. - Two kinds of "forbidden". RBAC and admission can both answer 403. Read the message: RBAC says
User "jane" cannot create resource "pods" in API group "" in the namespace "shop"; admission names the reason, for exampleexceeded quotaorviolates PodSecurity. - Conflicts. Every object carries a
resourceVersion. An update that is based on an old version is refused with409 Conflict(the "object has been modified" error), and the client must read the object again and retry. This is how many controllers can safely edit the same objects without locks. - Watches are cheap. The API server keeps a recent window of changes in memory (the watch cache) and serves most lists and watches from it, so thousands of watchers do not turn into thousands of etcd queries.
- High availability. Because it is stateless, you can run several API servers behind a load balancer and all of them serve traffic at once. The controller manager and the scheduler are different: several copies may run, but only the elected leader acts.
How it is installed and configured
How you configure the API server depends on how it was installed.
| Installed with | Runs as | Where the flags are |
|---|---|---|
| kubeadm (the exam's clusters) | A static Pod started by the kubelet | /etc/kubernetes/manifests/kube-apiserver.yaml |
| Binaries by hand ("the hard way") | A systemd service | /etc/systemd/system/kube-apiserver.service |
On any node you can also see the running flags with ps -ef | grep kube-apiserver.
Parts:
- kube-apiserver.yaml (/etc/kubernetes/manifests)
- kubelet (watches the folder)
- kube-apiserver (container, :6443)
- etcd (127.0.0.1:2379)
- Certificates (/etc/kubernetes/pki)
- Mirror Pod (kube-apiserver-cp1)
Connections:
- kube-apiserver.yaml to kubelet: reads
- kubelet to kube-apiserver: starts
- kube-apiserver to etcd: TLS
- Certificates to kube-apiserver: hostPath
- kubelet to Mirror Pod: reports
A static Pod solves a chicken-and-egg problem: Pods are normally created through the API, but the API server cannot
create itself. So the kubelet reads manifests from a folder on disk and runs them without asking anyone. For each
one it also reports a mirror Pod to the API, named after the node (kube-apiserver-cp1), so that you can see it
and read its logs with kubectl. The file is the truth; the mirror Pod is a reflection. kubectl delete pod on it
changes nothing, and kubectl edit cannot change the flags.
The flags you are most likely to read or change:
| Flag | What it controls | kubeadm sets |
|---|---|---|
--advertise-address | The IP other components use to reach this API server | The node's IP |
--secure-port | The HTTPS port | 6443 |
--etcd-servers | Where etcd is (client port) | https://127.0.0.1:2379 |
--etcd-cafile, --etcd-certfile, --etcd-keyfile | TLS towards etcd | Files under /etc/kubernetes/pki |
--client-ca-file | The CA whose client certificates are trusted | /etc/kubernetes/pki/ca.crt |
--tls-cert-file, --tls-private-key-file | The API server's own serving certificate | apiserver.crt, apiserver.key |
--kubelet-client-certificate, --kubelet-client-key | How it proves itself to kubelets (logs, exec) | apiserver-kubelet-client.* |
--authorization-mode | The authorizers, in order | Node,RBAC |
--enable-admission-plugins | Plugins to add to the default set | NodeRestriction |
--service-cluster-ip-range | The range Service IPs are taken from | 10.96.0.0/12 |
--service-node-port-range | The range for NodePorts | not set (default 30000-32767) |
--service-account-key-file, --service-account-issuer | Checking and issuing ServiceAccount tokens | sa.pub, the cluster's issuer URL |
Using it
Example · See what kubectl really sends
kubectl is only an HTTP client. Raise its verbosity to see the requests, or send one yourself.
kubectl get pods -n shop -v=6 # prints each URL and the response code
kubectl get --raw /api/v1/namespaces/shop/pods | head -c 300
kubectl get --raw '/readyz?verbose' # the API server's own health checks, one per line
kubectl api-resources # every resource, its API group, short name and whether it is namespaced
kubectl auth whoami # the user and groups the API server sees you as
kubectl auth can-i create deployments -n shop --as jane
/readyz?verbose is worth remembering: a line such as [-]etcd failed tells you in one command that the API
server is up but cannot reach its database.
Example · Change a flag safely on a kubeadm cluster
Task: allow NodePorts from 30000 to 34000. Work on the control plane node, as root.
cp /etc/kubernetes/manifests/kube-apiserver.yaml /root/kube-apiserver.yaml.bak # outside the manifests folder
vi /etc/kubernetes/manifests/kube-apiserver.yaml
Add one line to the command list, with the same indentation as its neighbours:
- --service-node-port-range=30000-34000
Save. The kubelet notices the change within about half a minute, stops the old container and starts a new one. kubectl answers "connection refused" in the meantime, so watch the runtime instead:
watch crictl ps --name kube-apiserver # wait for a new container in state Running
kubectl -n kube-system get pod -l component=kube-apiserver
ps -ef | grep -o 'service-node-port-range=[^ ]*'
Example · Fix an API server that will not start
kubectl get nodes answers The connection to the server 192.168.56.10:6443 was refused. kubectl is useless now,
because the thing it talks to is the thing that is broken. Go to the control plane node and ask the container
runtime.
crictl ps -a --name kube-apiserver # Exited, and the ATTEMPT number keeps growing
crictl logs <container-id> 2>&1 | tail # the last lines name the cause
What the logs usually say, and what it means:
| The log says | Look at |
|---|---|
unknown flag: --... | A misspelt flag in the manifest |
dial tcp 127.0.0.1:2379: connect: connection refused | Is etcd running? Is --etcd-servers right? |
open /etc/kubernetes/pki/...: no such file or directory | A wrong path, or a file that is not mounted |
x509: certificate signed by unknown authority | The wrong CA file in an --etcd-cafile or similar flag |
If crictl ps -a shows no kube-apiserver container at all, the kubelet could not even read the manifest:
journalctl -u kubelet --since "5 min ago" | grep -i -E "apiserver|manifest|yaml"
Fix the manifest (or restore your backup), then wait for the container and for kubectl get nodes to answer.
Tricks
Common mistakes
- Editing the mirror Pod.
kubectl edit pod kube-apiserver-cp1cannot change the flags, and deleting the Pod does not restart the server. Only the file in/etc/kubernetes/manifestscounts. - Breaking the YAML. A tab, a wrong indent or a missing dash and the kubelet cannot read the manifest: the old
container is stopped and no new one appears. The container logs are empty, so look in
journalctl -u kubelet. - Not waiting. After a change the API server needs up to a minute to come back, longer if the kubelet is backing
off after several crashes. "Connection refused" for the first seconds is normal; check
crictl psbefore you change anything else. - Pointing at the wrong etcd port. Clients use 2379. Port 2380 is for etcd members talking to each other.
- Reading 401 and 403 as the same thing. 401 means your credential was not accepted (an expired certificate, the wrong kubeconfig). 403 means you are known but not allowed.
- Blaming the API server for everything. If
kubectl getworks but new Pods stayPending, the API server is fine: look at the scheduler. If Pods are scheduled but never start, look at the kubelet on that node.
Recap
- The API server is the only door into the cluster and the only client of etcd; it is stateless.
- Every request goes through authentication → authorization → admission (mutating, schema check, validating) before it is stored. Reads skip admission.
- All other components work by watching the API server and writing results back to it.
- With kubeadm it is a static Pod: change it by editing
/etc/kubernetes/manifests/kube-apiserver.yaml. - When it is down, kubectl cannot help: use
crictl ps -a,crictl logsandjournalctl -u kubelet.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
The API server of a kubeadm cluster is down after somebody edited its manifest. Find both mistakes with the node's own tools, without kubectl, and bring the cluster back with nothing lost.
Fix · Level 3 · challenge · target 20 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.