简体   繁体   中英

How to use kubernetes api client from inside pod in nodejs

Description

I'm trying to build an app that at some point needs to create objects inside Kubernetes. I tried the ambassador container version mentioned in the answer to this post , it works but I would prefer to use the client API since I'm planning on doing several CRUD operations on kubernetes.

Problem

Since I'm using node found this two libraries:

The second one it seems that you need to use the username and password which I don't have because it should log in with the token inside /var/run/secrets/kubernetes.io/serviceaccount/ , so tried the first one. It includes some examples and the one that seems to fit this particular case doesn't work. Here is the code:

/* eslint no-console:0 */
//
// Create an API client using in cluster configuration.
//
const Client = require('kubernetes-client').Client
const Request = require('kubernetes-client/backends/request')

// kubernetes-client supports reading the service account credentials [1]
// from different locations by setting the
// `KUBERNETES_CLIENT_SERVICEACCOUNT_ROOT` environment variable. This is
// useful, for example, when running Telepresence [2].
//
// [1]: https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#accessing-the-api-from-a-pod
// [2]: https://www.telepresence.io/howto/volumes

async function main () {
  try {
    const backend = new Request(Request.config.getInCluster())
    const client = new Client({ backend })
    await client.loadSpec()

    //
    // Fetch all the pods
    const pods = await client.api.v1.pods.get()
    pods.body.items.forEach((item) => {
      console.log(item.metadata)
    })

    //
    // Fetch the Deployment from the kube-system namespace.
    //
    const deployment = await client.apis.apps.v1.namespaces('kube-system').deployments().get()
    deployment.body.items.forEach((item) => {
      console.log(item.metadata)
    })
  } catch (err) {
    console.error('Error: ', err)
  }
}

main()

Built the docker image and ran it with this manifest:

apiVersion: v1
kind: Pod
metadata:
  name: kubia-api-test
spec:
  containers:
    - image: itasahobby/kubiaapi
      name: kubia
      livenessProbe:
        httpGet:
          path: /
          port: 8080

However when I check the pod it crashed and the error doesn't look too declarative:

jusepe@ubuntu:~/Documents/kubernetes/test_app$ kubectl get pods | grep test
kubia-api-test         0/1     CrashLoopBackOff   7          13m
jusepe@ubuntu:~/Documents/kubernetes/test_app$ kubectl logs kubia-api-test 
/node_modules/ws/lib/websocket.js:354
      ...options
      ^^^
SyntaxError: Unexpected token ...
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/node_modules/ws/index.js:3:19)

As Matt suggested in the comments was the version of node, my dockerfile looked like

FROM node:7
ADD app.js /app.js
ADD package.json /package.json

RUN npm install
ENTRYPOINT ["node", "app.js"]

Changed to node latest and worked fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM