CptDolphin
CptDolphin

Reputation: 474

Is it possible to execute kubectl commands via curl?

is it possible to execute kubectl commands as a curl by simply hitting GKE kube master api for some resources and get json back ?

Upvotes: 0

Views: 1215

Answers (2)

Arghya Sadhu
Arghya Sadhu

Reputation: 44707

Kubernetes is REST API based and can be called via curl.

https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/#accessing-the-kubernetes-api

Kubectl internally does curl to Kubernetes API which can be verified via running below command and searching for curl and you can execute the same curl command. In the below example kubectl is using certificate for authentication and executing curl against Kubernetes API.

kubectl get nodes --v=10

curl -k -v -XGET  -H "Accept: application/json;as=Table;v=v1beta1;g=meta.k8s.io, application/json" -H "User-Agent: kubectl/v1.17.0 (darwin/amd64) kubernetes/70132b0" 'https://127.0.0.1:32768/api/v1/nodes?limit=500'

But to call Kubernetes REST API you can either use a client certificate or a JWT bearer token. A service account which has a bearer token is the recommended way to communicate to Kubernetes API from a pod.

Kubernetes API.

Upvotes: 1

dejanualex
dejanualex

Reputation: 4356

Kubernetes is an entirely API-based system ,to interact with the Kubernetes API you need a ServiceAccount (obtained through a Cluster Role and a RoleBinding).

Here you can find the documentation for Google Kubernetes Engine API: https://cloud.google.com/kubernetes-engine/docs/reference/rest

Also as side note, might be usefully: https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/#accessing-the-kubernetes-api

Upvotes: 2

Related Questions