Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

kubernetes

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!

like image 810
Dreams Avatar asked Apr 20 '17 05:04

Dreams


People also ask

When multiple containers are declared in a pod it implies?

All the containers in the pod share the same network namespace which means all containers can communicate with each other on the localhost. For example, We have two containers in the same pod listening on ports 3000 and 3001 respectively. container 1 can talk to container 2 on localhost:3000.

Can a Kubernetes pod have multiple services?

You can add multiple containers to the same pod, but that's only recommended if the services are tightly coupled, like if they need to communicate. For example, if you have a web server and a sql database, you would likely want them in the same pod.

How do I connect two services in Kubernetes?

Kubernetes enables inter service communication by allowing services communicate with other services using their service name. In your scenario, redis service should be accessible from other services on http://app-api-redis-svc.default:6379. Here default is the namespace under which your service is running.

Can we run two containers in a pod?

Creating a Pod that runs two Containers The mount path for the shared Volume is /usr/share/nginx/html . The second container is based on the debian image, and has a mount path of /pod-data . The second container runs the following command and then terminates. Notice that the second container writes the index.


2 Answers

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"]
like image 63
Anuruddha Lanka Liyanarachchi Avatar answered Nov 09 '22 15:11

Anuruddha Lanka Liyanarachchi


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"]
like image 34
Andrew Morris Avatar answered Nov 09 '22 16:11

Andrew Morris