1
votes

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
"Running in an environment ..." that is inside the cluster, or do you mean from your workstation (or something)?mdaniel
Running outside the cluster, on. A workstation. Think it as a custom orchestrator that runs on top of kubernetes.rubenhak

2 Answers

1
votes

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.

0
votes

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.