How to gracefully shutdown sidecar container when main container is completed

3.5k Views Asked by At

I have a kubernetes manifest file which has 2 containers. Main container is collecting data at a certain time period and other (logstash) container reading logs for the main container and shipping it to the ELK stack. Once the main container completed its job, what is the best way to gracefully signal or shutdown the logstash container?

1

There are 1 best solutions below

1
On

Currently, there is no way to explicitly mark a container as "Sidecar" (though there is a long-lasting KEP to implement it).

It means that the best you can do is to define Container Lifecycle Hook with the command to gracefully shutdown your sidecar container. Something like:

  containers:
  - name: logstash-container
    image: logstash-image
  - name: your-main-container
    image: your-main-container-image
    lifecycle:
      preStop:
        exec:
          command: ["/bin/sh", "-c", "gracefully terminate logstash"]done"]