简体   繁体   English

使用@ kubernetes / client-node NPM连接到K8S

[英]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.

相关问题 使用 @kubernetes/client-node 修补 K8s 自定义资源 - Patch K8s Custom Resource with @kubernetes/client-node 使用kubernetes / client-node lib - Using kubernetes/client-node lib 使用@kubernetes/client-node nodejs api 部署虚拟服务 - Deployment of Virtual Service using @kubernetes/client-node nodejs api 我如何使用服务器帐户(@kubernetes/client-node)连接到 API kube - how i may connect to API kube with server account (@kubernetes/client-node) 无法使用 @kubernetes/client-node 获取集群内的所有作业 - Unable to get all the jobs inside a cluster using @kubernetes/client-node 无法将 GRPC 微服务与客户端连接 - 在 k8s 上嵌套 JS 节点 - Unable to connect GRPC microservice with client - Nest JS Node on k8s 我如何获得所有命名空间的名称(@kubernetes/client-node) - how i may get name of all namespaces (@kubernetes/client-node) 如何在内部将 mysql kubernetes 容器与 nodejs k8s 容器连接? - How to connect mysql kubernetes container internally with nodejs k8s container? 如何将前端连接到 K8S 中的后端? - How to connect frontend to backed in K8S? 在 k8s 集群上使用 proxy_pass 部署 nginx 和节点时出现随机/间歇性 502 网关错误 - Random/Intermittent 502 gateway errors with nginx and node deployments using proxy_pass on a k8s cluster
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM