I wanted to access and control the Kubernetes API using certificates. I looked around online but did not find a single good write-up, so I collected a few articles and put them together. It is a bit messy, but the general idea is clear. Feel free to take what is useful.
API Server
kube-apiserver is one of the most important core components of Kubernetes. Its main responsibilities are:
Providing the REST API for cluster management, including authentication, authorization, data validation, and cluster state changes
Acting as the hub for data exchange and communication between other modules (other modules query or modify data through the API Server; only the API Server directly operates on etcd)
REST API
kube-apiserver can serve both HTTPS (listening on port 6443 by default) and HTTP (listening on 127.0.0.1:8080 by default). The HTTP API is insecure and performs no authentication or authorization; do not enable it in production. Both endpoints expose the same REST API format. See the Kubernetes API Reference for the call format of every API.
Local port
Receives HTTP requests
Defaults to 8080; change it with the API Server’s --insecure-port startup flag
Defaults to the localhost IP; change it with the --insecure-bind-address startup flag
Unauthenticated and unauthorized HTTP requests reach the API Server through this port
Secure port
Defaults to 6443; change it with the --secure-port startup flag
The default IP is a non-local host network address; set it with the --bind-address startup flag
Receives HTTPS requests
Used for authentication based on token files, client certificates, and HTTP Basic auth
The REST path for Node-related endpoints is /api/v1/proxy/nodes/{name}, where {name} is the node name or IP address.
1
2
3
/api/v1/proxy/nodes/{name}/pods/ # List information about all Pods on the specified node/api/v1/proxy/nodes/{name}/stats/ # List statistics about physical resources on the specified node/api/v1/prxoy/nodes/{name}/spec/ # List summary information about the specified node
The Pod information obtained here comes from the Node rather than the etcd database, so the two may differ in time. If you start the kubelet process with --enable-debugging-handles=true, the Kubernetes Proxy API also exposes:
1
2
3
4
5
6
7
/api/v1/proxy/nodes/{name}/run # Run a container on the node/api/v1/proxy/nodes/{name}/exec # Run a command in a container on the node/api/v1/proxy/nodes/{name}/attach # Attach to a container on the node/api/v1/proxy/nodes/{name}/portForward # Forward ports for Pods on the node/api/v1/proxy/nodes/{name}/logs # List various logs for the node/api/v1/proxy/nodes/{name}/metrics # List metrics related to the node/api/v1/proxy/nodes/{name}/runningpods # List running Pods on the node
Pod-related endpoints
1
2
/api/v1/proxy/namespaces/{namespace}/pods/{name}/{path:*}# Access a service endpoint of a pod/api/v1/proxy/namespaces/{namespace}/pods/{name}# Access a Pod
Use kubectl api-versions and kubectl api-resources to query the API versions and resource objects supported by the Kubernetes API.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
kubectl get nodes -o yaml
kubectl describe nodes silas-thinkpad-l460 -o yaml
# Describe a nodekubectl describe nodes kubernetes-node-emt8.c.myproject.internal
# Describe a podkubectl describe pods/nginx
kubectl get pods -n <namespace> <pod-name>
## out pod info to yamlkubectl get pods -n kube-system coredns-565d847f94-fhmwh -o yaml
$ kubectl cluster-info
Kubernetes control plane is running at https://192.168.58.2:8443
kubectl config view
curl --cacert /etc/kubernetes/pki/ca.crt $KUBE_API/version
# Get pod info by http curl token$ JWT_TOKEN_DEFAULT_DEFAULT=$(kubectl create token default)curl $KUBE_API/api/v1/namespaces/kube-system/pods/coredns-565d847f94-fhmwh \
--cacert /etc/kubernetes/pki/ca.crt \
--header "Authorization: Bearer $JWT_TOKEN_DEFAULT_DEFAULT"\
# Get pod info by http curl sslcurl $KUBE_API/api/v1/namespaces/kube-system/pods/coredns-565d847f94-fhmwh \
--cacert /etc/kubernetes/pki/ca.crt \
--cert /etc/kubernetes/pki/apiserver-kubelet-client.crt \
--key /etc/kubernetes/pki/apiserver-kubelet-client.key
kubectl get --raw /api/v1/namespaces/kube-system/pods/coredns-565d847f94-fhmwh
For a typical application, the user provides a username and password, the server checks the database to see whether they exist and are valid, and authentication succeeds or fails accordingly.
How does Kubernetes do it?
Although ordinary users cannot be created through API calls, Kubernetes cleverly authenticates users via certificates. In other words, any user who can present a valid certificate is authenticated by Kubernetes.
After authentication, Kubernetes treats the certificate’s CN as the username (for example, /CN=joker means the username is Joker) and the Organization as the user’s group. A Role bound to that username and group then determines the user’s permissions.