How to Use Google Kubernetes API Efficiently
The exact way to make things simpler
Recently, working on a project I was told to search and implement how to create namespaces and pods on GKE with an API call.
I knew right away that there should be a way in which we can use the API which we mentioned in our YAML file, like this

I began my search on the web as usual and found some concrete stuff that somewhat worked after minor changes here and there.
Finally, I created a perfect API call with a curl request which I tested from a local Ubuntu terminal and with a tool called postman that worked perfectly.
After completing my given task, I stumbled upon an idea that led me to write this guide which can help a lot of people in the future like me, who also want to implement this kind of stuff.
That is why I want to create this guide which shows a straight-away exact solution of implement GKE API.
The main reason behind writing this short and to the point article is that during my search on the internet I was not able to find a correct and working solution easily.
I had to look here and there. I had to do trial and error many times just to perform this small stuff.
And finally, here we are with an exact guide that will let you fire an API call to perform various kinds of stuff in GKE.
curl -k \
-X POST \
-d @- \
-H "Authorization: Bearer <token>" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
https://ClusterEndpoint/api/v1/resourcequotas <<'EOF'
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": "<insert-namespace-name-here>"
}
}
EOFIn the above snippet, you can see the exact curl request for creating a namespace in your GKE cluster.
Few things to do before applying this;
Token: For the token, you need to replace it with the value you’ll get after applying the below command
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')Before applying this command make sure you already have a service account created if not use the following command to create.
kubectl create serviceaccount demoLater you need to also create a ClusterRole and need to bind it with the service account. Here is the YAML file for that
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: demo
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]Then bind the role with this command
$ kubectl create clusterrolebinding demo-binding --clusterrole=demo --serviceaccount=demoCluster Endpoint: In your GKE cluster you can see your Endpoint by clicking on it in the details.

Namespace: Write the desired name in the field which will the name of your namespace.
Now you have to just apply this above curl request on your local terminal. After applying you’ll see a success message like this.
{
"kind": "Namespace",
"apiVersion": "v1",
"metadata": {
"name": "namedemo",
"selfLink": "/api/v1/namespaces/namedemo",
"uid": "f97c50e0-4610-4d5a-bebc-de96583de255",
"resourceVersion": "20080",
"creationTimestamp": "2021-02-24T05:29:42Z",
"managedFields": [
{
"manager": "curl",
"operation": "Update",
"apiVersion": "v1",
"time": "2021-02-24T05:29:42Z",
"fieldsType": "FieldsV1",
"fieldsV1": {"f:status":{"f:phase":{}}}
}
]
},
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"phase": "Active"
}Something More
Here are some bonus tips, now to modify this curl request in order to make pods, resource quota, etc you need to convert your YAML file into JSON and need to change the API link.
curl -k \
-X DELETE \
-d @- \
-H "Authorization: Bearer <token>" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
https://Endpoint/api/v1/namespaces/namespacename <<'EOF'
{
...
}
EOFThe above curl request will delete your Namespace which mentioned at the end of the URL.
What next?
The possibilities are infinite, you can look on the internet and find specific API for pod and other things and just modify the request accordingly.
Thank you.






