kubectl is HTTP!
it's HTTP!
Introduction
kubectl is the command-line interface you use when you interact with a Kubernetes cluster. The kube-apiserver exposes the Kubernetes API, which is a REST interface, and kubectl is an HTTP client like cURL or a browser like Firefox.
kubectl sends HTTP requests to the Kubernetes API, and knowing the underlying mechanics of how kubectl translates its commands into HTTP requests will help you better understand how Kubernetes is organized. In this article, I'll walk you through the details.
Prerequisites
In order to follow along with this article, you will need access to a Kubernetes cluster using kubectl. If you don't have a cluster easily available, you can find one from your browser at the Killer Coda CKAD Scenario .
You will need to sign in to Killer Coda (you can use your Github, Gitlab, Google Account, or email), select the CKAD Certification option, and then choose Playground. Click the START button, and you'll see this command prompt:
controlplane $
Run your commands there.
Seeing the API Endpoints
Every object controlled by Kubernetes has an API endpoint that you, or an automated system, can interact with through HTTP Methods. The most common HTTP methods are GET, POST, PUT and DELETE.
To see the API paths available in your cluster, run the command:
kubectl get --raw / .
If you've got jq installed, make it pretty with:
kubectl get --raw / | jq .
Either way, you'll see a longer version of this JSON object:
The --raw flag allows you to look at the response from the Kubernetes API as JSON instead of the formatted tables that you're used to seeing with kubectl.
If you'd like to see the the supported API versions in your cluster, run the command:
领英推荐
kubectl api-versions
Each line shows you the api group, then a forward slash /, then the API version. For example, in node.k8s.io/v1 , node.k8s.io is the api group, and v1 is the version of the API that supports this feature.
Finally, if you'd prefer to see the Kubernetes resources and their API version organized in columns, run the command:
kubectl api-resources
You will see a longer version of this table:
Translating kubectl commands to HTTP Requests
When you run a kubectl command, kubectl sends an HTTP request as a string of JSON to the Kubernetes API.
If you need to review (or learn for the first time!) how HTTP requests are formatted, I recommend you check out HTTP Requests by Sematext.
Here's a table showing a few kubectl commands and how they map to HTTP methods and the Kubernetes API paths.
HTTP Clients
The most common HTTP clients we use to communicate with the Kubernetes API are kubectl and kubeadm. You can also setup an HTTP proxy so you can make requests to the Kubernetes API using cURL, wget or your browser. You run this command:
kubectl proxy --port=4000
Start exploring! You can find more detailed instructions K8s Docs: HTTP Proxy Access
Conclusion
Kubernetes can be an intimidating system to start learning, especially if you're a web app developer who is new to the DevOps side of the tech industry. Exploring the Kubernetes API through the different endpoints might be a good place to start. Good luck!
Workforce Development Strategist, Computer Science Researcher, Computational Artist, Educator
6 个月I must have more Kubernetes in my life.
Software Developer | Educator | Ruby on Rails Developer | Backend Developer | Full Stack Developer
7 个月Love it! Thanks Kim!
I always refer to this page: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/