How to keep a Kubernetes pod running when main process is not blocking?

9/7/2017

I have a postfix pod that I want to keep running for as long as I have my whole cluster up. The problem is that the "start" command:

postfix start

is non-blocking, meaning it exits as soon as it successfully fires off the command. As far as I know, this is the only way to start postfix.

The way Docker works is that it will exit the container as soon as the main process exits. So, when postfix start returns, Docker will assume the whole container is finished doing its thing and close it, even though this command fired off other processes.

How do I prevent this from happening? The only workaround I can think of is postfix start && sleep infinity but this seems hacky and weird. I'd like to avoid a sleep infinity if possible. Is there an alternative way to start postfix that is blocking?

-- Lindsay Landry
blocking
docker
kubernetes
postfix
process

1 Answer

9/7/2017

You need to use a custom CMD script that keeps staying in foreground after the postfix start command.

For instance, you can use this one, took from here:

#!/bin/bash

# Wait before postfix is really started.

function get_state {
    echo $(script -c 'postfix status' | grep postfix/postfix-script)
}

postfix start
echo $(get_state)

while true; do
    state=$(get_state)
    if [[ "$state" != "${state/is running/}" ]]; then
        PID=${state//[^0-9]/}
        if [[ -z $PID ]]; then
            continue
        fi
        if [[ ! -d "/proc/$PID" ]]; then
            echo "Postfix proces $PID does not exist."
            break
        fi
    else
        echo "Postfix is not running."
        break
    fi
done

This script keeps running a while loop until the postfix process is up, and it exits when the process exits. This way you'll have your container correctly stop if postfix dies for any reason.

-- whites11
Source: StackOverflow