Basic Operations
Common patterns for listing, creating, and managing Kubernetes resources.
Prerequisites
Install the client: npm install @kubernetes/client-node. These examples require a valid kubeconfig (default: ~/.kube/config) or in-cluster service account. Run with Node.js 18+.
List Pods
examples/example.js
import * as k8s from '@kubernetes/client-node';
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const namespace = 'default';
try {
const res = await k8sApi.listNamespacedPod({ namespace });
console.log(res);
} catch (err) {
console.error(err);
}
Related: KubeConfig · CoreV1Api · Pod operations
Create a Namespace
examples/namespace.js
import * as k8s from '@kubernetes/client-node';
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
var namespace = {
metadata: {
name: 'test',
},
};
try {
const response = await k8sApi.createNamespace({ body: namespace });
console.log('Created namespace');
console.log(response);
const res = await k8sApi.readNamespace({ name: namespace.metadata.name });
console.log(res);
await k8sApi.deleteNamespace({ name: namespace.metadata.name });
} catch (err) {
console.error('Error!: ' + err);
}
Related: KubeConfig · CoreV1Api · Namespace operations
In-Cluster Configuration
When running inside a Kubernetes cluster (e.g. in a Pod), use loadFromCluster() to authenticate via the service account.
examples/in-cluster.js
import * as k8s from '@kubernetes/client-node';
const kc = new k8s.KubeConfig();
kc.loadFromCluster();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const namespace = 'default';
try {
const res = await k8sApi.listNamespacedPod({ namespace });
console.log(res);
} catch (err) {
console.error(err);
}
Related: KubeConfig · CoreV1Api · Pod operations
Create a Job from a CronJob (In-Cluster)
examples/in-cluster-create-job-from-cronjob.js
import * as k8s from '@kubernetes/client-node';
const kc = new k8s.KubeConfig();
kc.loadFromCluster();
const batchV1Api = kc.makeApiClient(k8s.BatchV1Api);
const cronJobName = 'cronjob';
const jobName = 'myjob';
const namespace = 'default';
const job = new k8s.V1Job();
const metadata = new k8s.V1ObjectMeta();
job.apiVersion = 'batch/v1';
job.kind = 'Job';
metadata.name = jobName;
metadata.annotations = {
'cronjob.kubernetes.io/instantiate': 'manual',
};
job.metadata = metadata;
try {
const cronJobRes = await batchV1Api.readNamespacedCronJob({ name: cronJobName, namespace });
job.spec = cronJobRes?.spec?.jobTemplate.spec;
const res = await batchV1Api.createNamespacedJob({ namespace, body: job });
console.log(res);
} catch (err) {
console.error(err);
}
Related: KubeConfig · BatchV1Api · CronJob operations · Job operations
TypeScript — Basic Example
examples/typescript/simple/example.ts
import * as k8s from '@kubernetes/client-node';
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const namespace = 'default';
const res = await k8sApi.listNamespacedPod({ namespace });
console.log(res);
// Example of instantiating a Pod object.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const pod = {} as k8s.V1Pod;
Related: KubeConfig · CoreV1Api · Pod operations