Home > Net >  How can I install additional dependencies to the container?
How can I install additional dependencies to the container?

Time:12-21

I've got a nodered server (automation tool) up and running in my cluster. But I would like to add support in nodered for Elasticsearch.

If I were onprem with a physical server I would run npm install node-red-contrib-elasticsearch-jd (https://flows.nodered.org/node/node-red-contrib-elasticsearch-jd) and bobs my uncle :-).

But I'm new to kubernetes, how do I do that? With a ConfigMap? The container would also require the npm tool installed i guess.

Any tips?

CodePudding user response:

If you want to add something to a running container, you can execute commands in your Pod, the same as you do on the physical server.

kubectl exec -it {target-pod-name} -- npm install node-red-contrib-elasticsearch-jd

However, the above command only adds the package to the container, so when you restart the container, you need the same procedure to install the dependencies again.

If you want to make it permanent, you need to create Dockerfile and re-build an image from the image you use.

Dockerfile:

FROM {your-image}
RUN npm install node-red-contrib-elasticsearch-jd 

Then execute docker build to build a new image. If you use the new image, it should work the same as before, but there is a new dependency installed.

CodePudding user response:

I think there are three good ways to deal with this issue.

First one is to use postStart handler in your pod / deployment file. In your case it will look like this:

...
containers:
  - name: your-pod
    image: your-image
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "npm install node-red-contrib-elasticsearch-jd"]

It will run the command from the command block immediately after a container is started:

Kubernetes sends the postStart event immediately after a Container is started

Example with NGINX deployment with postStart handler to install curl package:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx-deployment
spec:
  selector:
    matchLabels:
      app: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        lifecycle:
          postStart:
            exec:
              command: ["/bin/sh", "-c", "apt update && apt -y install curl"]

Second one it is to run command in the running container:

kubectl exec -it {your-pod-name} -- npm install node-red-contrib-elasticsearch-jd

This is temporary (after a restart there will be no package), if you want this package to be permanent after a restart either add a postStart handler (as explained before) or build a new Docker image (below).

Last one, as suggested in the comments is to build new Docker image from following Dockerfile:

FROM {your-image-name}
RUN npm install node-red-contrib-elasticsearch-jd

Then you can upload newly built image to the registry and pull it from there - if you are using a private registry you need to configure Kubernetes by adding credentials and refer to them in the pod / deployment file.

  • Related