gpt4 book ai didi

node.js - 为 NodeJS 的 Kubernetes 建议使用什么 API 客户端?

转载 作者:太空宇宙 更新时间:2023-11-04 00:04:36 24 4
gpt4 key购买 nike

我正在使用 AKS(Azure k8),需要 k8s node.js 客户端来实现此选项

按名称杀死 Pod
更改部署 Pod 数量
重新启动所有部署 Pod

我只需要这个功能,女巫库最适合这个?

还请提供使用 lib 实现其中一些函数的示例。

谢谢

更新

我喜欢这个 Node.js (TypeScript) github.com/Goyoo/node-k8s-client,您能提供有关服务帐户和访问权限的更多信息吗?

最佳答案

这是所有客户端库的完整列表。

https://kubernetes.io/docs/reference/using-api/client-libraries/

您将需要创建一个服务帐户和角色绑定(bind)来配置适当的权限,以便从客户端库执行这些操作。

node.js 特定库:

Node.js (TypeScript) github.com/Goyoo/node-k8s-client

Node.js github.com/tenxcloud/node-kubernetes-client

Node.js github.com/godaddy/kubernetes-client

基本示例(使用 godaddy 客户端)

/* eslint no-console:0 */
//
// Demonstrate some of the basics.
//
const Client = require('kubernetes-client').Client;
const config = require('kubernetes-client').config;

const deploymentManifest = require('./nginx-deployment.json');

async function main() {
try {
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' });

//
// Get all the Namespaces.
//
const namespaces = await client.api.v1.namespaces.get();
console.log('Namespaces: ', namespaces);

//
// Create a new Deployment.
//
const create = await client.apis.apps.v1.namespaces('default').deployments.post({ body: deploymentManifest });
console.log('Create: ', create);

//
// Fetch the Deployment we just created.
//
const deployment = await client.apis.apps.v1.namespaces('default').deployments(deploymentManifest.metadata.name).get();
console.log('Deployment: ', deployment);

//
// Change the Deployment Replica count to 10
//

const replica = {
spec: {
replicas: 10
}
};

const replicaModify = await client.apis.apps.v1.namespaces('default').deployments(deploymentManifest.metadata.name).patch({ body: replica });
console.log('Replica Modification: ', replicaModify);

//
// Modify the image tag
//
const newImage = {
spec: {
template: {
spec: {
containers: [{
name: 'nginx',
image: 'nginx:1.8.1'
}]
}
}
}
};
const imageSet = await client.apis.apps.v1.namespaces('default').deployments(deploymentManifest.metadata.name).patch({ body: newImage });
console.log('New Image: ', imageSet);

//
// Remove the Deployment we created.
//
const removed = await client.apis.apps.v1.namespaces('default').deployments(deploymentManifest.metadata.name).delete();
console.log('Removed: ', removed);
} catch (err) {
console.error('Error: ', err);
}
}

main();

关于node.js - 为 NodeJS 的 Kubernetes 建议使用什么 API 客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52930720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com