【问题标题】:What API client is recommended for Kubernetes for NodeJS?Kubernetes for NodeJS 推荐什么 API 客户端?
【发布时间】:2018-10-22 13:34:34
【问题描述】:

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

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

我只需要这个功能,witch lib 最适合这个?

还请提供一些使用 lib 的示例。

谢谢

更新

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

【问题讨论】:

    标签: node.js kubernetes azure-aks


    【解决方案1】:

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

    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/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();
    

    【讨论】:

    • 什么代表'deploymentManifest'?YAML's?
    【解决方案2】:
    【解决方案3】:

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

    1. 根据我的经验,大多数时候我使用 kubernetes 客户端来完成运行 kubectl 命令可以获得结果的工作。该命令具有非常统一的格式和模式来操作不同的资源,例如 pod、namespace 等。k8s 内部提供的其余 API 也遵循与 kubectl 命令相同的模式。因此,关键代码本质上是高度可重用且组织良好的。
    2. 由于以上几点,在消费API时,很容易定义一个统一的模式。我们可以优雅地处理响应、错误处理、日志等所有事情。代码看起来简洁易懂,因此易于维护。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      相关资源
      最近更新 更多