'createDeployment() with Kubernetes JavaScript client

I am trying to create a deployment or replicaSet with the Kubernetes Javascript client. The Kubernetes javascript client documentation is virtually non-existent.

Is there any way to achieve this?



Solution 1:[1]

Assuming that by:

  • createDeployment()
  • you are referring to: createNamespacedDeployment()

You can use below code snippet to create a Deployment using Javascript client library:

const k8s = require('@kubernetes/client-node');

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

const k8sApi = kc.makeApiClient(k8s.AppsV1Api); // <-- notice the AppsV1Api

// Definition of the deployment
var amazingDeployment = {
    metadata: {
       name: 'nginx-deployment'
    },
    spec: {
       selector: {
          matchLabels: {
             app: 'nginx'
          }
       },
       replicas: 3,
       template: {
          metadata: {
             labels: {
                app: 'nginx'
             }
          },
          spec: {
             containers: [
                {
                   name: 'nginx',
                   image: 'nginx'
                   
                } ]
          }
       }
    }
 };

// Sending the request to the API
k8sApi.createNamespacedDeployment('default', amazingDeployment).then(
  (response) => {
    console.log('Yay! \nYou spawned: ' + amazingDeployment.metadata.name);
  },
  (err) => {
    console.log('Oh no. Something went wrong :(');
    // console.log(err) <-- Get the full output!
  }
);

Disclaimer!

This code assumes that you have your ~/.kube/config already configured!

Running this code for the first time with:

  • $ node deploy.js

should output:

Yay! 
You spawned: nginx-deployment

You can check if the Deployment exists by:

  • $ kubectl get deployment nginx-deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           6m57s

Running this code once again will output (deployment already exists!):

Oh no. Something went wrong :(

Additional resources:

Solution 2:[2]

Be careful when you try to deploy a different kinds of resources such as deployment or service.

You need to correctly specify the API version.

const k8sApi = kc.makeApiClient(k8s.AppsV1Api) or (k8s.CoreV1Api) for namespace and etc.

Solution 3:[3]

First, you create a kube config object and then create the associated API type. I.e,

import k8s from '@kubernetes/client-node';

const kubeConfig = new k8s.KubeConfig();
kubeConfig.loadFromCluster(); // Or whatever method you choose

const api = kubeConfig.makeApiClient(k8s.CoreV1Api); // Or whatever API 
                                                           // you'd like to 
                                                           // use.

const namespace = 'default';
const manifest = new k8s.V1ConfigMap();

// ... additional manifest setup code...

await api.createNamespacedConfigMap(namespace, manifest);

This is the gist of it. If you'd like, I recently created a library with the intention of simplifying interactions with the kubernetes javascript api and it can be found here:

https://github.com/ThinkDeepTech/k8s

If it doesn't help you directly, perhaps it can serve as an example of how to interact with the API. I hope that helps!

Also, make sure the application executing this code has the necessary permissions (i.e, the K8s Role, RoleBinding and ServiceAccount configs) necessary to perform the actions you're attempting. Otherwise, it'll error out.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dawid Kruk
Solution 2 zikster
Solution 3 Hayden McParlane