[英]Connect to K8S using @kubernetes/client-node NPM
The documentation for the official @kubernetes/client-node NPM does not exist. 官方@ kubernetes / client-node NPM的文档不存在。 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. 我无法在本地运行它使用
kubectl proxy
监听端口8081.我应该以某种方式配置loadFromDefault
。 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. 因此,如果
kubectl get pods
没有任何其他参数的kubectl get pods
在本地工作,那么您提供的代码片段也应该可以正常工作。
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. 通常,您不需要执行任何端口转发即可到达Kubernetes master,因此请仔细检查您的kubeconfig是否与
kubectl
配合kubectl
而无需端口转发。 If not, then your kubeconfig is likely incorrect. 如果没有,那么你的kubeconfig可能不正确。 This page provides more details on kubeconfig files: https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/
此页面提供了有关kubeconfig文件的更多详细信息: 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. 如果
kubectl get pods
工作但你的代码片段没有,请提供你得到的确切错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.