javascript:TypeScript实现的Kubernetes客户端,支持Node.js服务端调用

JavaScript client

分支124Tags54
当前项目代码仓暂无内容

Javascript Kubernetes 客户端信息

Build Status Client Capabilities Client Support Level Build and Deploy Docs

Kubernetes 的 Javascript 客户端采用 typescript 实现,可通过 Javascript 或 Typescript 调用。该客户端专为 Node 环境下的服务端使用而设计。

安装

npm install @kubernetes/client-node

示例代码

列出所有 Pod

const k8s = require('@kubernetes/client-node');

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

k8sApi.listNamespacedPod({ namespace: 'default' }).then((res) => {
    console.log(res);
});

创建新命名空间

const k8s = require('@kubernetes/client-node');

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

var namespace = {
    metadata: {
        name: 'test',
    },
};

k8sApi.createNamespace({ body: namespace }).then(
    (response) => {
        console.log('Created namespace');
        console.log(response);
        k8sApi.readNamespace({ name: namespace.metadata.name }).then((response) => {
            console.log(response);
            k8sApi.deleteNamespace({ name: namespace.metadata.name });
        });
    },
    (err) => {
        console.log('Error!: ' + err);
    },
);

以编程方式创建集群配置

const k8s = require('@kubernetes/client-node');

const cluster = {
    name: 'my-server',
    server: 'http://server.com',
};

const user = {
    name: 'my-user',
    password: 'some-password',
};

const context = {
    name: 'my-context',
    user: user.name,
    cluster: cluster.name,
};

const kc = new k8s.KubeConfig();
kc.loadFromOptions({
    clusters: [cluster],
    users: [user],
    contexts: [context],
    currentContext: context.name,
});
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
...

文档

📖 查看文档

文档使用 Docusaurus 构建,包含以下内容:

  • SDK 参考(KubeConfig、Watch、Informer、Exec 等)
  • Kubernetes API 参考(所有 API 组)
  • 历史版本的版本选择器
  • Kubernetes API 参考 — 所有 Kubernetes 客户端库的事实来源

本地预览文档

# From the repo root — install the client library (needed by typedoc)
npm install

# Install docs dependencies and start the dev server
cd docs
npm install
npm start          # opens http://localhost:3000 with hot-reload

npm start 会自动运行 prestart 钩子,该钩子在启动开发服务器之前,从源代码生成 API 参考文档、SDK 文档和模型页面。手写文档(例如 docs/docs/examples/)的更改会即时反映;生成的源代码的更改则需要重启服务器。

若要执行完整的生产构建(这也会验证所有链接):

cd docs
npm run build      # generates + builds static site into docs/build/
npm run serve      # preview the production build at http://localhost:3000

示例目录中还有更多 JS 和 TS 示例。

兼容性

0.13.0 版本发布之前,发布版本并未跟踪 Kubernetes 版本。从 0.13.0 版本开始,每当我们更新此库所基于的 Kubernetes API 次要版本(例如 1.19.x)时,我们将递增次要版本号。

1.0.0 版本中,request 已迁移到 node-fetch 作为 HTTP(S) 后端,相关跟踪见 #754。

2.0.0 版本中,node-fetch 已迁移到 undici,这是 Node.js 的原生 fetch 包,相关跟踪见 #2306。

一般来说,较新的客户端可以与较旧的 Kubernetes 集群配合使用,但不能 100% 保证兼容性。

客户端版本 更旧版本 1.28 1.29 1.30 1.31 1.32 1.33 1.34
0.19.x - x x x x x x
0.20.x - + x x x x x
0.21.x - + + x x x x
0.22.x - + + + x x x
1.0.x - + + + + x x
1.1.x - + + + + x x
1.2.x - + + + + + x
1.3.x - + + + + + x
1.4.x - + + + + + +

关键说明:

  • javascript-client 与 Kubernetes 版本中的功能/API 对象完全相同。
  • + javascript-client 具有 Kubernetes 集群中可能不存在的功能或 API 对象,但它们共有的所有功能都可以正常工作。
  • - Kubernetes 集群具有 javascript-client 库无法使用的功能(额外的 API 对象等)。
  • x Kubernetes 集群不保证支持此版本的 API 客户端,因为它仅承诺支持 n-2 版本。此组合未经过测试,并且使用在较新服务器版本中已被弃用和移除的 API 版本的操作将无法正常运行。

已知问题

  • 不完全支持多个 kubeconfig。 凭据基于 kubeconfig 用户名进行缓存,不同配置间可能会发生冲突。 相关问题请参见 issue

  • 在请求中需要多个相同键的标头(例如 Impersonate-Group)时,避免使用 fetch。Fetch 会将这些值合并到单个标头键中,使值成为单个字符串而非字符串列表,例如 Impersonate-Group: "group1,group2"。解决方法是使用 https 等底层库来发起请求。更多详情请参考 issue #2474

贡献

有关开发设置、测试和贡献指南,请参见 CONTRIBUTING.md

项目介绍

JavaScript客户端【此简介由AI生成】

定制我的领域
242.27 K568访问 GitHub