In Kubernetes, how can I detect that my pod is ready?

3/19/2019

I have a Kubernetes pod using a readiness probe, and tied with the service this ensures that I don't receive traffic until I'm ready.

I'm using Spring Actuator as the health endpoint for this readiness probe.

But i'd like to trigger some actions whenever the pod is deemed ready by the kubelet.

What would be the simplest way to do this?

-- Asgeir S. Nilsen
kubernetes
spring-boot

3 Answers

3/19/2019

As part of the pod lifecycle events, you may want to attach additional handlers such as podStart and build your custom logic to manipulate the event happening as need be.

Alternatively, you can also run your code to read REST response from GET /api/v1/namespaces/{namespace}/pods/{name}/log build any downstream logic to get pod status

Note, in controlled environments, it's good to not base any of your conditional logic on pods (individual pods) rather rely on deployments. The REST endpoint which you should rather focus on is

GET /apis/apps/v1beta2/namespaces/{namespace}/deployments/{name}/status
-- Raunak Jhawar
Source: StackOverflow

3/19/2019

You can use a post-install hook provided by helm charts (in case you are using helm to deploy your application). This will perform necessary actions/script/jobs after the pod becomes up and running.

-- Vinod Kumar
Source: StackOverflow

3/19/2019

Perhaps implement your own HealthCheck. When you find that everything is ok for the first time, run your code.

I use a static variable firstHealthCheckOK is checked. Your logic should run only once.

I am assuming you are running Spring-boot 2.x and are calling a readiness probe on http://localhost:8080/actuator/health

The health() method below is called when Kubernetes calls http://localhost:8080/actuator/health

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class HealthCheck implements HealthIndicator {

    static boolean firstHealthCheckOK = false;

    @Override
    public Health health() {
        int errorCode = check(); // perform health check
        if (errorCode != 0) {
            return Health.down()
              .withDetail("Error Code", errorCode).build();
        }
        if (firstHealthCheckOK == false){
            firstHealthCheckOK = true;
            doStartUpLogic();
        }
        return Health.up().build();
    }

    private int check() {
        //some logic
        return 0;
    }

    private void doStartUpLogic() {
        //some startup logic
    }
}
-- rjdkolb
Source: StackOverflow