Skip to main content

kubectl Equivalents

If you're familiar with kubectl, these examples show how to do the same things programmatically.

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+.

kubectl get pods

examples/kubectl/equivalents/get.js
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const client = k8s.KubernetesObjectApi.makeApiClient(kc);
const namespace = {
apiVersion: 'v1',
kind: 'Namespace',
metadata: {
name: 'test',
},
};

console.log(await client.read(namespace));

Related: KubeConfig · KubernetesObjectApi

kubectl create

examples/kubectl/equivalents/create.js
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const client = k8s.KubernetesObjectApi.makeApiClient(kc);
const namespace = {
kind: 'Namespace',
metadata: {
name: 'test',
},
};

client.create(namespace);

Related: KubeConfig · KubernetesObjectApi

kubectl apply

examples/kubectl/equivalents/apply.js
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const client = k8s.KubernetesObjectApi.makeApiClient(kc);

// update deployment "my-deployment" in namespace "my-namespace" to 3 replicas
const deployment = {
apiVersion: 'apps/v1',
kind: 'Deployment',
metadata: {
name: 'my-deployment',
namespace: 'my-namespace',
},
spec: {
replicas: 3,
},
};

client.patch(deployment);

Related: KubeConfig · KubernetesObjectApi

kubectl delete

examples/kubectl/equivalents/delete.js
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const client = k8s.KubernetesObjectApi.makeApiClient(kc);
const namespace = {
kind: 'Namespace',
metadata: {
name: 'test',
},
};

client.delete(namespace);

Related: KubeConfig · KubernetesObjectApi

kubectl create namespace

examples/kubectl/equivalents/namespace-create.js
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

// This code is the JavaScript equivalent of `kubectl create namespace test`.

try {
const namespace = {
metadata: {
name: 'test',
},
};
const createdNamespace = await k8sApi.createNamespace({ body: namespace });
console.log('New namespace created:', createdNamespace);
} catch (err) {
console.error(err);
}

Related: KubeConfig · CoreV1Api · Namespace operations

kubectl create namespace (from YAML)

examples/kubectl/equivalents/namespace-create-yaml.js
import * as k8s from '@kubernetes/client-node';
import { readFileSync } from 'node:fs';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

// This code is the JavaScript equivalent of `kubectl apply -f namespace.yaml`.

try {
const namespaceYaml = k8s.loadYaml(readFileSync('./namespace.yaml', 'utf8'));
const createdNamespace = await k8sApi.createNamespace({ body: namespaceYaml });
console.log('New namespace created:', createdNamespace);
} catch (err) {
console.error(err);
}

Related: KubeConfig · loadYaml · CoreV1Api · Namespace operations

kubectl get namespaces

examples/kubectl/equivalents/namespace-list.js
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

// This code is the JavaScript equivalent of `kubectl get ns`.

try {
const namespaces = await k8sApi.listNamespace();
namespaces.items.forEach((namespace) => console.log(namespace.metadata.name));
} catch (err) {
console.error(err);
}

Related: KubeConfig · CoreV1Api · Namespace operations

kubectl run (create pod)

examples/kubectl/equivalents/pod-create.js
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

// This code is the JavaScript equivalent of `kubectl run demo-pod --image=nginx --namespace=default`.

const pod = {
metadata: {
name: 'demo-pod',
},
spec: {
containers: [
{
name: 'nginx-container',
image: 'nginx',
},
],
},
};
const namespace = 'default';

try {
const createdPod = await k8sApi.createNamespacedPod({
namespace,
body: pod,
});
console.log('Created pod:', createdPod);
} catch (err) {
console.error(err);
}

Related: KubeConfig · CoreV1Api · Pod operations

kubectl get pods --namespace

examples/kubectl/equivalents/pod-filter-by-namespace.js
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

// This code is the JavaScript equivalent of `kubectl get pods --namespace=default`.

try {
const pods = await k8sApi.listNamespacedPod({ namespace: 'default' });

pods.items.forEach((pod) => console.log(pod.metadata.name));
} catch (err) {
console.error(err);
}

Related: KubeConfig · CoreV1Api · Pod operations

kubectl create resourcequota

examples/kubectl/equivalents/resourceQuota-create.js
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

// This code is the JavaScript equivalent of `kubectl create resourcequota my-quota --hard=pods=3`.

try {
const quota = {
metadata: {
name: 'my-quota',
},
spec: {
hard: {
pods: '3',
},
},
};
const createdQuota = await k8sApi.createNamespacedResourceQuota({
namespace: 'default',
body: quota,
});

console.log('Created quota:', createdQuota);
} catch (err) {
console.error(err);
}

Related: KubeConfig · CoreV1Api · ResourceQuota operations

kubectl get resourcequotas

examples/kubectl/equivalents/resourceQuota-list.js
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

// This code is the JavaScript equivalent of `kubectl get resourcequotas --all-namespaces`.

try {
const resourceQuotas = await k8sApi.listResourceQuotaForAllNamespaces();

resourceQuotas.items.forEach((quota) => console.log(quota.metadata.name));
} catch (err) {
console.error(err);
}

Related: KubeConfig · CoreV1Api · ResourceQuota operations