The Kubernetes API and kubectl
The Kubernetes API and kubectl
Everything in Kubernetes is a REST resource behind the API server. kubectl is
just an HTTP client with good manners.
Groups, versions, resources
/api/v1/namespaces/dev/pods/web core group ("") -> apiVersion: v1
/apis/apps/v1/namespaces/dev/deployments group apps -> apiVersion: apps/v1
/apis/networking.k8s.io/v1/ingresses group networking.k8s.io
| Verb | HTTP | kubectl |
|---|---|---|
| create | POST | create, apply |
| get / list / watch | GET | get, describe, get -w |
| update / patch | PUT / PATCH | apply, edit, patch, scale |
| delete | DELETE | delete |
See the real calls: kubectl get pods -v=6.
kubeconfig
~/.kube/config holds three lists and a pointer:
graph LR
CTX[context] --> CL[cluster: API URL + CA]
CTX --> US[user: client cert or token]
CTX --> NS[default namespace]
CUR[current-context] --> CTX
kubectl config get-contexts
kubectl config use-context <name>
kubectl config set-context --current --namespace=dev
The cluster describes itself
graph LR
A["api-resources<br/>which kinds?"] --> B["explain<br/>which fields?"]
B --> C["get -o yaml<br/>what is stored?"]
C --> D["describe<br/>what happened?"]
D --> E["logs<br/>what did the app say?"]
kubectl api-resources # kinds, short names, group, namespaced?
kubectl api-versions # group/version pairs served
kubectl explain deploy.spec.strategy # field reference from the live server
kubectl create deploy --help # examples at the bottom
kubectl get pod web -o yaml # stored object incl. defaults
kubectl get pod web -o jsonpath='{.status.podIP}'
kubectl describe pod web # object + events
kubectl auth can-i delete nodes # am I allowed?
Talking to the API directly
kubectl get --raw /apis/apps/v1 | head # raw GET, authenticated by kubectl
kubectl proxy --port 8001 & # local unauthenticated tunnel
curl -s localhost:8001/api/v1/namespaces/default/pods | head
Output formats worth knowing
| Flag | Use |
|---|---|
-o wide | node and IP |
-o yaml / -o json | full object |
-o jsonpath='{...}' | one field, for scripts |
-o custom-columns=NAME:.metadata.name,... | your own table |
--show-labels, -L app | labels as columns |