Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple command in postStart hook of a container

Tags:

kubernetes

in a kubernetes Deployment yaml file is there a simple way to run multiple commands in the postStart hook of a container?

I'm trying to do something like this:

lifecycle:   postStart:     exec:       command: ["/bin/cp", "/webapps/myapp.war", "/apps/"]       command: ["/bin/mkdir", "-p", "/conf/myapp"]       command: ["touch", "/conf/myapp/ready.txt"] 

But it doesn't work. (looks like only the last command is executed)

I know I could embed a script in the container image and simply call it there... But I would like to be able to customize those commands in the yaml file without touching the container image.

thanks

like image 537
aregnier Avatar asked Sep 11 '16 13:09

aregnier


People also ask

What is preStop in Kubernetes?

PreStop. This hook is called immediately before a container is terminated due to an API request or management event such as a liveness/startup probe failure, preemption, resource contention and others.

What does the lifecycle Poststop do?

The idea is that poststop tasks can gives users the ability to catch and hande errors internally within an allocation. For some use cases (eg Artifact upload below), the poststop hook is a critical part of the overall allocation.

What is Terminationgraceperiodseconds in Kubernetes?

4 - Kubernetes waits for a grace period At this point, Kubernetes waits for a specified time called the termination grace period. By default, this is 30 seconds. It's important to note that this happens in parallel to the preStop hook and the SIGTERM signal. Kubernetes does not wait for the preStop hook to finish.

What is Handler in Kubernetes?

The postStart handler runs asynchronously relative to the Container's code, but Kubernetes' management of the container blocks until the postStart handler completes. The Container's status is not set to RUNNING until the postStart handler completes.


2 Answers

Only one command allowed, but you can use sh -c like this

  lifecycle:     postStart:       exec:         command:           - "sh"           - "-c"           - >             if [ -s /var/www/mybb/inc/config.php ]; then             rm -rf /var/www/mybb/install;             fi;             if [ ! -f /var/www/mybb/index.php ]; then             cp -rp /originroot/var/www/mybb/. /var/www/mybb/;             fi 
like image 150
aborilov Avatar answered Oct 02 '22 16:10

aborilov


You also can create a bash or make script to group all those commands.

like image 37
Bruno Quaresma Avatar answered Oct 02 '22 16:10

Bruno Quaresma