Core Kubernetes · 13
Quick Revision
1.Object map
apiVersion : v1 for most. apps/v1, discovery.k8s.io/v1, storage.k8s.io/v1, networking.k8s.io/v1. Ingress is GA but frozen, Gateway API is the way forward. Checked for Kubernetes v1.37 (supported : 1.37, 1.36, 1.35).
| Ch. | Object | Does what |
|---|---|---|
| 02 | Node | Machine that runs Pods |
| 03 | Pod | Containers, one IP, volumes |
| 04 | Deployment | Rolling updates over ReplicaSets |
| 04 | ReplicaSet | Keeps N Pods running |
| 05 | Service | Stable name + IP for its Pods |
| 05 | EndpointSlice | The live Pod IPs |
| 06 | ConfigMap | Plain settings, 1 MiB max |
| 06 | Secret | Sensitive values, 1 MiB max |
| 07 | PVC / PV | The claim / the disk |
| 07 | StorageClass | How new disks get made |
| 08 | Namespace | Name scope, quotas, defaults |
| 08 | Ingress | HTTP routes in (frozen) |
| add-on | Gateway API | Newer routing, as CRDs |
2.Triage : STATUS, then where to look
1. get podsSTATUS, RESTARTS
2. describe podEvents, Last State
3. logs --previousthe run that crashed
4. events--for pod/<pod>
5. debugno shell in the image
| STATUS | What it means | Run | Usual fix | Ch. |
|---|---|---|---|---|
| Pending | FailedScheduling : requests fit no node, an untolerated taint, an unbound PVC, a hostPort clash | kubectl describe pod <pod>kubectl get events --field-selector reason=FailedScheduling | Lower requests, add nodes, tolerate, fix the PVC | 03, 02 |
| ImagePullBackOff | Bad name or tag, image not pushed, or no imagePullSecret | kubectl describe pod <pod> | Fix the ref, push it, add a pull Secret | 03 |
| CrashLoopBackOff | Keeps exiting. Waits 10 s, 20 s, 40 s ... up to 300 s between restarts | kubectl logs <pod> --previous | Fix the app, config or probe timing | 03 |
| OOMKilled | Over the memory limit, Exit Code 137. RESTARTS climbs | kubectl describe pod <pod>kubectl top pod <pod> --containers | Raise the limit, or fix the leak | 03 |
Read the exit code
- 0Exited cleanly
- 1The app reported an error
- 137128 + 9, SIGKILL : OOM or killed
- 143128 + 15, SIGTERM : asked to stop
STATUS is not the phase
- STATUS shows a container reason when there is one, else
status.phase. - ImagePullBackOff : phase Pending. CrashLoopBackOff : usually Running.
3.Commands
| Look | What it is for |
|---|---|
| kubectl get pods -o wide | STATUS, node, Pod IP. 03 |
| kubectl describe pod <pod> | Events, Last State. 03 |
| kubectl logs <pod> --previous | The run that crashed. 03 |
| kubectl events --for pod/<pod> --types=Warning | Its warnings only, the same ones describe lists. 09 |
| kubectl top pod <pod> --containers | Live CPU and memory. Needs Metrics Server |
| kubectl debug -it <pod> --image=busybox:1.28 --target=<c> | No shell in the image ? Add an ephemeral one. 03 |
| Change | What it is for |
|---|---|
| kubectl apply -f <file> | Create or update. 01 |
| kubectl diff -f <file> | Preview an apply. 01 |
| kubectl exec -it <pod> -- sh | Shell in a container, via the kubelet. 02 |
| kubectl port-forward svc/<svc> 8080:80 | localhost:8080 to one Pod behind the Service. 02 |
| kubectl rollout status / undo deploy/<d> | Wait for a rollout, or go back one revision. 09 |
| kubectl scale deploy/<d> --replicas=3 | Set replicas by hand. An HPA overrides it. 04 |
In short : Each object does one job and points at the next one. When a Pod breaks, read its STATUS first, then work through get, describe, logs --previous and events.
1.Requests and limits
Set per container, for CPU and for memory.
- requestWhat kube-scheduler reserves. The Pod lands only where the sum of requests fits, even if real use is low.
- limitA ceiling the kernel enforces with cgroups.
- Going above the request is fine while the node has room.
- Limit only ? The API server copies it into the request, so a Pod with only CPU and memory limits is still Guaranteed.
- LimitRange : namespace defaults, min and max, applied at admission. Break them, get 403 Forbidden.
See : scheduler 02, HPA % of requests 04, LimitRange and ResourceQuota 08.
resources:
requests: { cpu: "250m", memory: "128Mi" }
limits: { cpu: "500m", memory: "256Mi" } # 0.5 CPU
2.QoS classes
Kubernetes derives status.qosClass from the resources. You never set it, and it is fixed once the Pod is created.
- Pod-level
spec.resources(Beta since v1.34, on by default) count too.
| Class | Rule | Under node pressure |
|---|---|---|
| Guaranteed | Every container sets CPU and memory, request = limit | Evicted last, never over its requests |
| Burstable | Some CPU or memory request or limit, not Guaranteed | Early if over its requests, late if under |
| BestEffort | No CPU or memory request or limit anywhere | Early, no request to stay under |
How the kubelet picks
- Using more than its requests ? Goes first.
- Then lower Priority first.
- Then the furthest over its requests.
- An in-place resize (stable since v1.35) that would change the class is rejected.
3.Labels vs annotations
| Labels | Annotations | Ch. | |
|---|---|---|---|
| Purpose | Identify and group objects | Describe objects for tools and people | 04, 09 |
| Selected by | Services, workloads, kubectl -l | Never | 05, 04 |
| Selectors | A Service matches on equality only. Deployment, ReplicaSet, Job and DaemonSet also take In, NotIn, Exists, DoesNotExist. A Deployment's selector is immutable and must match its Pod template labels. | 05, 04 | |
| Key | [prefix/]name, name 63 chars max. kubernetes.io/ and k8s.io/ are reserved. Same rules for both. | 04 | |
| Value | 63 chars max, may be empty | Any string, 256 KiB in total per object | 04 |
| Example | app: web | kubernetes.io/ | 04, 09 |
| Recommended | Prefix app. plus nameinstanceversioncomponentpart-ofmanaged-by | none | |
In short : Requests place Pods and decide eviction, limits cap them (CPU is throttled, memory is OOMKilled). Labels select, annotations only describe.