使用REST API访问Kubernetes API [英] Access Kubernetes API using REST APIs

查看:570
本文介绍了使用REST API访问Kubernetes API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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.

推荐答案

我认为您可以使用NodeJS调用Kubernetes API. rel ="nofollow noreferrer"> kubernetes-client 由 GoDaddy 推广. Kubernetes-clientKubernetes-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆