使用REST API访问Kubernetes API [英] Access Kubernetes API using REST APIs
问题描述
我正在尝试从NodeJS访问Kubernetes API.在没有kubectl或gcloud命令行工具可用的环境中运行.使用 https://github.com/kubernetes-client/javascript 客户端SDK.问题是如何验证和访问远程群集.对于在GCP中运行的K8,我需要它.
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.
换句话说,尝试执行"gcloud容器群集获取凭据"的操作,但是没有"gcloud" cli.
In other words trying to do what "gcloud container clusters get-credentials" does, but without "gcloud" cli.
推荐答案
我认为您可以使用Kubernetes-client
的Kubernetes-client
简化了如何调整Kubernetes API接口的方法.
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.
下面的示例演示了一种方法,该方法如何使用小片段提取默认Kubernetes Namespace
中的所有Deployments
:
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();
此处,您可以找到更多使用kubernetes-client
的代码段
Here you can find more snippets for using kubernetes-client
.
这篇关于使用REST API访问Kubernetes API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!