Sithys
Sithys

Reputation: 3783

How to restart kubernetes deployment via nodejs library

I'm trying to restart my kubernetes deployment via the kubernetes api using the @kubernetes/client-node Library. I'm not using deployment scale because i only need one deployment (db and service container) per app.

I also tried to restart a single container inside the deployment via exec (/sbin/reboot or kill), but it seems to not work with the nodejs library because it fails to upgrade to websocket connection, what is needed by the kubernetes exec endpoint as it seems. The other idea was to restart the whole deployment by setting the scale to 0 and then 1 again. But I dont get it working via the nodejs library. I tried to find an example for that, but was not successful.

The rolling restart is not working for me, becuase my application doesnt support multiple instances.

i tried it like this to scale

await k8sApi.patchNamespacedDeploymentScale(`mydeployment-name`, 'default', {
spec: { replicas: 0 },
});

await k8sApi.patchNamespacedDeploymentScale(`mydeployment-name`, 'default', {
spec: { replicas: 1 },
});

and to reboot the container i tried this

await coreV1Api.connectPostNamespacedPodExec(
podName,
'default',
'/sbin/reboot',
'web',
false,
false,
false,
false
);

Extra input:

When trying to use patchNamespacedDeployment i get the following error back by kubernetes api:

statusCode: 415,
statusMessage: 'Unsupported Media Type',

And response body:

V1Scale {
apiVersion: 'v1',
kind: 'Status',
metadata: V1ObjectMeta {
annotations: undefined,
clusterName: undefined,
creationTimestamp: undefined,
deletionGracePeriodSeconds: undefined,
deletionTimestamp: undefined,
finalizers: undefined,
generateName: undefined,
generation: undefined,
labels: undefined,
managedFields: undefined,
name: undefined,
namespace: undefined,
ownerReferences: undefined,
resourceVersion: undefined,
selfLink: undefined,
uid: undefined
},
spec: undefined,
status: V1ScaleStatus { replicas: undefined, selector: undefined }

when trying the exec approach i get the following response:

kind: 'Status',
apiVersion: 'v1',
metadata: {},
status: 'Failure',
message: 'Upgrade request required',
reason: 'BadRequest',
code: 400

i already looked the upgrade request error up, and it seems like the library isnt aware of this, because the library was generated from function footprints or something, so it is not aware of websockets.

Upvotes: 6

Views: 1960

Answers (1)

arneee
arneee

Reputation: 86

Really seems like there is a bug in the node Kubernetes client library. On PATCH requests it should set the content type to "application/json-patch+json" but instead it sends the content type as "application/json". Thats why you get unsupported media type back by the api.

Furthermore you need to use the JSON Patch format for the body you send: http://jsonpatch.com

To manually set the content type you can pass custom headers to the function call.

This worked for me:

const patch = [
    {
      op: 'replace',
      path: '/spec/replicas',
      value: 0,
    },
  ];
    
await k8sApi.patchNamespacedDeployment(
    `mydeployment-name`,
    'default',
    patch,
    undefined,
    undefined,
    undefined,
    undefined,
    { headers: { 'content-type': 'application/json-patch+json' } }
  );

After some google searching I found that this problem is already existing since 2018: https://github.com/kubernetes-client/javascript/issues/19

Upvotes: 7

Related Questions