Home > Software engineering >  Node red instance in Kubernetes with custom settings.js and other files
Node red instance in Kubernetes with custom settings.js and other files

Time:01-04

I am building a service which creates on demand node red instance on Kubernetes. This service needs to have custom authentication, and some other service specific data in a JSON file.

Every instance of node red will have a Persistent Volume associated with it, so one way I though of doing this was to attach the PVC with a pod and copy the files into the PV, and then start the node red deployment over the modified PVC.

I use following script to accomplish this

def paste_file_into_pod(self, src_path, dest_path):
      dir_name= path.dirname(src_path)
      bname = path.basename(src_path)

      exec_command = ['/bin/sh', '-c', 'cd {src}; tar cf - {base}'.format(src=dir_name, base=bname)]

      with tempfile.TemporaryFile() as tar_buffer:
           resp = stream(self.k8_client.connect_get_namespaced_pod_exec, self.kube_methods.component_name, self.kube_methods.namespace,
                  command=exec_command,
                  stderr=True, stdin=True,
                  stdout=True, tty=False,
                  _preload_content=False)

           print(resp)


           while resp.is_open():
                resp.update(timeout=1)
                if resp.peek_stdout():
                     out = resp.read_stdout()
                     tar_buffer.write(out.encode('utf-8'))

                if resp.peek_stderr():
                     print('STDERR: {0}'.format(resp.read_stderr()))

           resp.close()

           tar_buffer.flush()
           tar_buffer.seek(0)

           with tarfile.open(fileobj=tar_buffer, mode='r:') as tar:
                subdir_and_files = [tarinfo for tarinfo in tar.getmembers()]
                tar.extractall(path=dest_path, members=subdir_and_files)

This seems like a very messy way to do this. Can someone suggest a quick and easy way to start node red in Kubernetes with custom settings.js and some additional files for config?

CodePudding user response:

The better approach is not to use a PV for flow storage, but to use a Storage Plugin to save flows in a central database. There are several already in existence using DBs like MongoDB

You can extend the existing Node-RED container to include a modified settings.js in /data that includes the details for the storage and authentication plugins and uses environment variables to set the instance specific at start up.

Examples here: https://www.hardill.me.uk/wordpress/tag/multi-tenant/

  •  Tags:  
  • Related