使用 REST API 访问 Kubernetes API [英] Access Kubernetes API using REST APIs
问题描述
我正在尝试从 NodeJS 访问 Kubernetes API.在没有 kubectl 或 gcloud 命令行工具可用的环境中运行.使用 https://github.com/kubernetes-client/javascript 客户端 SDK.问题是如何认证和访问远程集群.对于在 GCP 中运行的 K8s,我需要这个.
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 container clusters get-credentials"所做的事情,但没有gcloud"cli.
In other words trying to do what "gcloud container clusters get-credentials" does, but without "gcloud" cli.
推荐答案
我认为你可以使用这个 kubernetes-client 由 GoDaddy 推广.Kubernetes-client
for NodeJS
简化了如何调整 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屋!