How to authenticate against Kubernetes clusters running on Google Container Engine using REST API?

9/28/2016

I'm writing an application to monitor a kubernetes cluster running on Google Container Engine. On the host where my application deployed, there are no kubectl and gcloud CLI nor are they allowed to be installed. So I am trying to do everything through REST API.

For creating the cluster through REST, I can use GCE Rest API with bearer token retrieved from Google OAuth Playground. Something like:

curl -i -X GET -H "Accept: application/json" -H "Content-Type: application/json" -H "Content-Length: 0" -H "Authorization: Bearer $MyBearerToken  https://container.googleapis.com/v1/projects/$PROJECT_ID/zones/$ZONE/serverconfig

I can also find Kubernetes REST API reference here. So my question is: How do I retrieve, say pod information, from my GCE Kubernetes cluster, using REST api and REST api only?

I tried with kubectl get pods --v=8, and it's using GET https://${Kubenetes_IP}/api/v1/namespaces/default/pods. But when I use the same api endpoint to curl with my GCE bearer. It gives me Unzuthorized error message.

# curl --insecure -H "Authorization: Bearer $MyBearerToken" https://${Kubenetes_IP}/api/v1/namespaces/default/pods
Unauthorized

I am guessing because I need to use a different bearer token, or some other authentication method. I am wondering if anyone got a quick programtic one-liner? (Without resorting to kubectl or gcloud)


Reference

This answer affirms that there is a way using bearer token, but didn't give a pointer or example

This answer also seems promising, but all the link provided are broken (and api are deprecated as well)

This answer assumes kubectl and gcloud are installed, which is not allowed in my current use case.

-- cookieisaac
bearer-token
google-kubernetes-engine
google-oauth
kubernetes
rest

2 Answers

9/30/2016

When you authorize the OAuth2 playground to give you a token, it exchanges an Authorization Code for a Refresh Token and an Access Token.

The Access Token (from the OAuth2 playground) is valid for 1 hour.

The Refresh Token is a long-lived credential that is used to obtain new Access Tokens from the Authorization Server.

If you try to authenticate to the "Resource Owner" (in this case, Kubernetes) with an expired access token, it will respond with an HTTP 401 error.

-- CJ Cullen
Source: StackOverflow

9/29/2016

Token can be retrieve from Google OAuth Playground

Kubernetes can be reached by the following curl command via REST API

# curl --insecure -H "Authorization: Bearer $MyBearerToken" https://${Kubenetes_IP}/api/v1/namespaces/default/pods

Kubernetes Master IP can be retrieved with kubectl get pods --v=8 and it could probably be retrieved somewhere from GCE Web GUI as well.

Full Kubernetes REST API can be found here

Make sure the token has not yet expired, and I think right now the default TTL is 1 hour.

-- cookieisaac
Source: StackOverflow