首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NodeJS中的Kubernetes推荐什么样的API客户端?

NodeJS中的Kubernetes推荐什么样的API客户端?
EN

Stack Overflow用户
提问于 2018-10-22 13:34:34
回答 3查看 3.8K关注 0票数 2

我使用AKS(Azure k8),需要k8s node.js客户端来选择这个选项

用名字杀死荚

更改部署吊舱数

重新启动所有部署吊舱

我只需要这个函数,女巫库是最好的吗?

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

谢谢

更新

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

EN

回答 3

Stack Overflow用户

发布于 2018-10-22 14:18:06

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

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

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

node.js专用库:

Node.js (TypeScript) github.com/Goyoo/node-k8s-client Node.js github.com/tenxcloud/节点-kubernetes-client Node.js github.com/godaddy/kubernetes-client

基本示例(使用godaddy )

代码语言:javascript
复制
/* 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();
票数 2
EN

Stack Overflow用户

发布于 2020-07-01 02:51:49

我推荐kubernetes-client/javascript。在我最喜欢原始API方法的所有API中,您可以看到一个示例这里。原因是

  1. 根据我的经验,大多数时候我使用kubernetes客户端来完成运行kubectl命令可以得到的结果。该命令有一个非常统一的格式和模式来操作不同的资源,如pods、命名空间等。k8s内部提供的rest API也遵循同样的kubectl命令模式。因此,关键代码本质上是高度可重用和组织良好的代码。
  2. 由于上述原因,在使用API时,很容易定义一个统一的模式。我们可以以优雅的方式处理响应、错误处理、日志等所有事情。代码看起来简洁易懂,因此易于维护。
票数 1
EN

Stack Overflow用户

发布于 2018-10-22 14:12:15

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52930720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档