Home > Net >  Installing packages in a Kubernetes Pod
Installing packages in a Kubernetes Pod

Time:08-10

I am experimenting with running jenkins on kubernetes cluster. I have achieved running jenkins on the cluster using helm chart. However, I'm unable to run any test cases since my code base requires python, mongodb

In my JenkinsFile, I have tried the following

1.

withPythonEnv('python3.9') {
     pysh 'pip3 install pytest'
}
stage('Test') {
    sh 'python --version'
}

But it says java.io.IOException: error=2, No such file or directory. It is not feasible to always run the python install command and have it hardcoded into the JenkinsFile. After some research I found out that I have to declare kube to install python while the pod is being provisioned but there seems to be no PreStart hook/lifecycle for the pod, there is only PostStart and PreStop.

I'm not sure how to install python and mongodb use it as a template for kube pods.

This is the default YAML file that I used for the helm chart - jenkins-values.yaml Also I'm not sure if I need to use helm.

CodePudding user response:

You should create a new container image with the packages installed. In this case, the Dockerfile could look something like this:

FROM jenkins/jenkins
RUN apt install -y appname

Then build the container, push it to a container registry, and replace the "Image: jenkins/jenkins" in your helm chart with the name of the container image you built plus the container registry you uploaded it to. With this, your applications are installed on your container every time it runs.

The second way, which works but isn't perfect, is to run environment commands, with something like what is described here: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/ the issue with this method is that some deployments already use the startup commands, and by redefining the entrypoint, you can stop the starting command of the container from ever running, thus causing the container to fail. (This should work if added to the helm chart in the deployment section, as they should share roughly the same format)

Otherwise, there's a really improper way of installing programs in a running pod - use kubectl exec -it deployment.apps/jenkins -- bash then run your installation commands in the pod itself.

That being said, it's a poor idea to do this because if the pod restarts, it will revert back to the original image without the required applications installed. If you build a new container image, your apps will remain installed each time the pod restarts. This should basically never be used, unless it is a temporary pod as a testing environment.

  • Related