Kubernetes - How can a container be started with 2 processes and bound to both of them?

4/20/2017

I need a deployment where each pod has a single container and each container has 2 java processes running. Since, a container starts with a process(P1), and if that particular process(P1) is killed, the pod restarts. Is it possible, that I start the container starts with 2 processes and even if one of them is killed, the container(or pod in our case, since each pod has only one container) restarts? I could not find any documentation related to this which says it can/cannot be done. Also, how can I start the container with 2 processes? If I try something like this (javaProcess is a java file) in my docker image, it runs only the first process :

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

If I start the container with one process(P1) and start the other process(P2) after the container is up, the container would not be bound to P2 and hence if P2 terminates, the container won't restart. But, I need it to restart!

-- Dreams
kubernetes

3 Answers

4/20/2017

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

java javaProcess &
java -jar abc.jar

In this way you will get two processes running inside your pod. But, your container would be bound to the process which is in the foreground!

-- Nagireddy Hanisha
Source: StackOverflow

4/20/2017

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

supervisordā€˜s primary purpose is to create and manage processes based on data in its configuration file. It does this by creating subprocesses. Each subprocess spawned by supervisor is managed for the entirety of its lifetime by supervisord (supervisord is the parent process of each process it creates). When a child dies, supervisor is notified of its death via the SIGCHLD signal, and it performs the appropriate operation.

Following is a sample supervisord config file which start 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 docker file 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"]
-- Anuruddha Lanka Liyanarachchi
Source: StackOverflow

1/28/2019

Run supervisor from Kubernetes config

To add to @AnuruddhaLankaLiyanarachchi answer you can also run supervisor from your Kubernetes setup by supplying command and args keys 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"]
-- Andrew Morris
Source: StackOverflow