Access Kubernetes API using REST APIs

2019-08-01 23:25发布

I'm trying to access Kubernetes APIs from NodeJS. Running in environment where no kubectl or gcloud command line tools are available. Using https://github.com/kubernetes-client/javascript client side SDK. The question is how to authenticate and access the remote cluster. I need this for K8s running in GCP.

In other words trying to do what "gcloud container clusters get-credentials" does, but without "gcloud" cli.

2条回答
看我几分像从前
2楼-- · 2019-08-01 23:40

I think that you can invoke Kubernetes API from NodeJS using this kubernetes-client promoted by GoDaddy. Kubernetes-client for NodeJS simplifies a way how to adjust an interface to Kubernetes API.

Below example demonstrates a way how to use a small snippet to extract all the Deployments in the default Kubernetes Namespace:

const Client = require('kubernetes-client').Client;
const config = require('kubernetes-client').config;

const client = new Client({ config: config.fromKubeconfig(), version: '1.9' });
const deployments = await client.api.v1.namespaces('default').deployments.get();

Here you can find more snippets for using kubernetes-client.

查看更多
唯我独甜
3楼-- · 2019-08-02 00:00

If you want to use Kubernetes Master API from outside, you need to authorize in one of the ways described here.

In particular, if you want to use username/password and you use GKE (k8 on GCP), then you can do it in the following steps:

  1. Click on your cluster name and "Show credentials", there you can find Username and Password.
  2. Use header: "Authorization: Basic BASE64ENCODED(USER:PASSWORD)" for your REST calls.
查看更多
登录 后发表回答