简体   繁体   中英

Connect to K8S using @kubernetes/client-node NPM

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM