Kubernetes. How can you start a container with two processes and associate them with them?

I need a deployment where each container has one container and each container has two java processes. Since the container is started with a process (P1), and if that particular process (P1) is killed, the module is reloaded. Is it possible that I start a container with two processes, and even if one of them is killed, the container is reloaded (or a block in our case, since each container only has one container)? I couldn't find any documentation related to this that says this can / cannot be done. Also, how can I start a container with two processes? If I try something like this (javaProcess is a java file) in my docker image, it only starts the first process:

java -jar abc.jar
java javaProcess
or 
java javaProcess
java -jar abc.jar

      

If I start a container with one process (P1) and start another process (P2) after the container is up, the container will not bind to P2, and therefore if P2 exits, the container will not restart. But I need to restart it!

+2


source to share


3 answers


You can do this using supervisord . Your main process should be linked to a supervisor in a docker image and two java processes should be managed with supervisord.

The main task of supervisord is to create and manage processes based on the data in the configuration file. It does this by creating subprocesses. Every subprocess created by a supervisor is managed for its entire life by a supervisor (supervisord is the parent process of every process it creates). When the child dies, the supervisor is notified of his death through the SIGCHLD signal and he performs the appropriate operation.

Below is an example of a supervisor config file that starts two java processes. (Supervisord.conf)



[supervisord]
nodaemon=true

[program:java1]
user=root
startsecs = 120
autorestart = true
command=java javaProcess1

[program:java2]
user=root
startsecs = 120
autorestart = true
command=java javaProcess2

      

In your dockerfile, you should do something like this:

RUN apt-get update && apt-get install -y supervisor
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["/usr/bin/supervisord"]

      

+3


source


You can add '&' to run the process in the background.

java javaProcess &
java -jar abc.jar

      



This will give you two processes running inside your module. But your container will be associated with the foreground process!

0


source


Start supervisor from Kubernetes config

To add to @AnuruddhaLankaLiyanarachchi's answer, you can also start supervisor from Kubernetes settings by specifying the keys command

and args

in the yaml file:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-example
spec:
  containers:
    - name: nginx-pod-example
      image: library/nginx
      command: ["/usr/bin/supervisord"]
      args: ["-n", "-c", "/etc/supervisor/supervisord.conf"]

      

0


source







All Articles