Simplified Kubernetes API client for Node.js.
For the v4.X documentation, go HERE.
Install via npm:
npm i kubernetes-client --save
kubernetes-client generates a Kubernetes API client at runtime based on a Swagger / OpenAPI specification. You can generate a client using specifications included with kubernetes-client:
const Client = require('kubernetes-client').Client;
const config = require('kubernetes-client').config;
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' });
or from a file:
const Client = require('kubernetes-client').Client;
const config = require('kubernetes-client').config;
const spec = require('./swagger.json');
const client = new Client({ config: config.fromKubeconfig(), spec});
or from the /swagger.json
endpoint on your kube-apiserver:
const Client = require('kubernetes-client').Client;
const config = require('kubernetes-client').config;
const client = new Client({ config: config.fromKubeconfig() });
await client.loadSpec();
or using basic auth:
const Client = require('kubernetes-client').Client;
const client = new Client({
config: {
url: 'CLUSTER_URL',
auth: {
user: 'admin',
pass: 'YOUR_PASSWORD',
},
insecureSkipTlsVerify: true,
}
})
or from within a Pod:
const Client = require('kubernetes-client').Client;
const config = require('kubernetes-client').config;
const client = new Client({ config: config.getInCluster() });
await client.loadSpec();
kubernetes-client translates Path Item Objects [1] (e.g.,
/api/v1/namespaces
) to object chains ending in HTTP methods (e.g.,
api.v1.namespaces.get
).
So, to fetch all Namespaces:
const namespaces = await client.api.v1.namespaces.get();
kubernetes-client translates Path Templating [2] (e.g.,
/apis/apps/v1/namespaces/{namespace}/deployments
) to function calls (e.g.,
apis.apps.v1.namespaces('default').deployments
).
So, to create a new Deployment in the default Namespace:
const deploymentManifest = require('./nginx-deployment.json')
const create = await client.apis.apps.v1.namespaces('default').deployments.post({ body: deploymentManifest });
and then fetch your newly created Deployment:
const deployment = await client.apis.apps.v1.namespaces('default').deployments(deploymentManifest.metadata.name).get();
and finally, remove the Deployment:
await await client.apis.apps.v1.namespaces('default').deployments(deploymentManifest.metadata.name).delete();
kubernetes-client supports .delete
, .get
, .patch
, .post
, and .put
.
kubernetes-client generates documentation for the included specifications:
examples/ has snippets for using kubernetes-client:
- The basic usage example from above: basic.js
- Create a
client
from your kube-apiserver's swagger.json: client-from-apiserver-swagger.js - Create a
client
from one of the included Swagger specifications: sync-client-version.js - Using resource aliases supported by
kubectl
(e.g.,.po
vs.pods
): convenience-properties.js - Use watch enpodints to get a JSON stream of Deployment events: watch.js
- Extend the Kubernetes API and a
client
with a CustomerResourceDefinition: using-crds.js - An exended CustomResourceDefinition example that implements a controller to "notify" on changes to Deployment objects: deployment-notifier.js
- A basic canary controller that removes Pods from a Service if they log an error: canary-controller.js
- Create a
client
using basic-auth: basic-auth.js - Generate badges showing the status of your Deployments. Illustrates using the in-cluster config: kubernetes-badges
See the kubernetes-client Issues if you're interested in helping out; and look over the CONTRIBUTING.md before submitting new Issues and Pull Requests.
kubernetes-client includes unit tests and integration tests. Minikube is a tool that makes it easy to run integration tests locally.
Run the unit tests:
$ npm test
The integration tests use a running Kubernetes server. You specify the
Kubernetes server context via the CONTEXT
environment variable. For
example, run the integration tests with the minikube
context:
$ CONTEXT=minikube npm run test-integration