Connect to K8S using @kubernetes/client-node NPM

6/14/2019

The documentation for the official @kubernetes/client-node NPM does not exist. I can only search the source code and try to understand the concept.

I havec this simple example:

const k8s = require('@kubernetes/client-node');

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

k8sApi.listNamespacedPod('default').then((res) => {
    console.log(res.body);
});

This works when I run this code on master node. I'm not able to run it locally usign kubectl proxy which listens on port 8081. I should probably configure the loadFromDefault somehow. Please explain with an example.

-- xpepermint
kubernetes
node.js
npm

1 Answer

6/24/2019

kc.loadFromDefault(); is correct and should be sufficient. So if kubectl get pods without any additional parameters works for you locally, the code snippet you provided should work as well.

Normally you do not need to do any port forwarding to reach Kubernetes master, so double-check that your kubeconfig works with kubectl without port forwarding. If not, then your kubeconfig is likely incorrect. This page provides more details on kubeconfig files: https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/

If kubectl get pods works but not your code snippet doesn't, please provide the exact error you're getting.

-- PjoterS
Source: StackOverflow