Home > Software design >  Use init container for running commands in the actual pod
Use init container for running commands in the actual pod

Time:12-05

I need to install some libraries in my pod before it starts working as expected.

My use case: I need some libraries that will support SMB (samba), and the image that I have to use does not have it installed.

Unfortunately, exec'ing into the actual pod and running commands do not seem to be a very good idea.

Is there a way by which I can use an init-container to install libsmbclient-dev in my ubuntu pod?

Edit: Some restrictions in my case.

  1. I use helm chart to install my app (nextcloud). So I guess I cannot use a custom image (as far as I know, we cannot use our own images in an existing helm chart). This would have been the best solution.
  2. I cannot run commands in kubernetes value.yaml since I do not use kubectl to install my app. Also I need to restart apache2 after I install the library, and unfortunately, restarting apache2 results in restarting the pod, effectively making the whole installation meaningless.

Since nextcloud helm allows the use of initcontainers, I wondered if that could be used, but as far as I understand the usability of initcontainers, this is not possible (?).

CodePudding user response:

You should build your own container image - e.g. with docker - and push it to a container repository that is suitable for your cluster, e.g. Docker Hub, AWS ECR, Google Artifact registry ...

First install docker (https://docs.docker.com/get-docker/)

create an empty directory and change into it. Then create a file Dockerfile with the following content:

FROM ubuntu:22.04
RUN apt-get update && apt-get install -y libsmbclient-dev \
  && rm -rf /var/lib/apt/lists/*

Execute

docker build -t myimage:latest .

This will download Ubuntu and build a new container image where the commands from the RUN statement will be executed. The image name will be myimage and the version will be latest.

Then push your image with docker push to your appropriate repository.

See also Docker best practices: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

CodePudding user response:

To install libsmbclient using a Kubernetes manifest file, you can specify the apt-get commands in the command or args fields in the container configuration. Here is an example YAML manifest file that installs libsmbclient when the main container starts up:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: main-app
    image: my-app-image
    command: ["/bin/sh", "-c"]
    args: ["apt-get update && apt-get install -y libsmbclient"]

In this example, the command and args fields are used to run the apt-get commands in the main-app container to install the libsmbclient library when the container starts up.

Note: This approach is not recommended, as it involves running commands manually and can be error-prone. It is better to include the necessary libraries and dependencies in the Docker image for your main container, as this ensures that everything is installed and configured properly when the container starts up.

  • Related