Search code examples
kuberneteskubernetes-pod

Is there a way to send SIGTERM from one container to another container within a Kubernetes pod


I have a K8s job that spins up a pod with two containers. Those containers are client and a server. After the client did all it needs to, it sends a special stop signal to the service after which the service exits; then client exits. The job succeeds.

The client and the service containers use jetty (see "Startup / Shutdown Command Line"), which has that signaling capability. I am looking for something more portable. It would be nice, to be able to send a SIGTERM from the client to the service, then the client would not need to use jetty for signaling. Is there a way to send SIGTERM from the client container to the server container. The client and the server processes are PID 1 in their respective containers.


Solution

  • Yes, enable shareProcessNamespace on the pod, for example:

    apiVersion: v1
    kind: Pod
    metadata:
      name: app
    spec:
      shareProcessNamespace: true
    

    Your containers can now send signals to one another. They will no longer use PID 1 anymore though.

    Here are the docs that explain it all in detail:

    https://kubernetes.io/docs/tasks/configure-pod-container/share-process-namespace/