
Kubernetes Has a Free API — Here's How to Manage Clusters Programmatically
Kubernetes provides a powerful REST API for managing containers, deployments, services, and more. Every kubectl command is just an API call — and you can use it directly. API Access # Start a proxy to the API server kubectl proxy --port = 8001 # List pods curl http://localhost:8001/api/v1/namespaces/default/pods | jq .items[].metadata.name # Get deployments curl http://localhost:8001/apis/apps/v1/namespaces/default/deployments Node.js Client npm install @kubernetes/client-node import * as k8s from " @kubernetes/client-node " ; const kc = new k8s . KubeConfig (); kc . loadFromDefault (); const k8sApi = kc . makeApiClient ( k8s . CoreV1Api ); const appsApi = kc . makeApiClient ( k8s . AppsV1Api ); // List pods const { body } = await k8sApi . listNamespacedPod ( " default " ); body . items . forEach ( pod => { console . log ( ` ${ pod . metadata . name } : ${ pod . status . phase } ` ); }); Create a Deployment const deployment = { metadata : { name : " my-app " }, spec : { replicas : 3 ,
Continue reading on Dev.to DevOps
Opens in a new tab


