簡體   English   中英

Kubernetes for NodeJS 推薦使用什么 API 客戶端?

[英]What API client is recommended for Kubernetes for NodeJS?

我正在使用 AKS(Azure k8),此選項需要 k8s node.js 客戶端

按名稱殺死 pod
更改部署 pod 計數
重新啟動所有部署 pod

我只需要這個功能,女巫 lib 最適合這個?

還請提供一些使用 lib 的示例。

謝謝

更新

我喜歡這個Node.js (TypeScript) github.com/Goyoo/node-k8s-client ,你能提供更多關於服務帳戶和訪問的信息嗎?

這是所有客戶端庫的完整列表。

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

我推薦kubernetes-client/javascript 在我最喜歡原始 API 方法的所有 API 中,您可以在此處查看示例。 原因是

  1. 根據我的經驗,大多數時候我使用 kubernetes 客戶端來完成我可以通過運行 kubectl 命令獲得結果的工作。 該命令具有非常統一的格式和模式來操作不同的資源,例如 pods、namespace 等。 k8s 內部提供的其余 API 也遵循與 kubectl 命令相同的模式。 所以本質上的關鍵代碼是高度可重用和組織良好的。
  2. 因為上面這點,當 API 被消費時,定義一個統一的模式是很容易的。 我們可以以優雅的方式處理響應、錯誤處理、日志等所有事情。 代碼看起來簡潔易懂,因此易於維護。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM