paketo-buildpacks / health-checker

A Cloud Native Buildpack that provides a health check binary compatible with Docker health checks
Apache License 2.0
6 stars 0 forks source link

health-check process linking to launcher #30

Closed feigi closed 1 year ago

feigi commented 1 year ago

Hi!

First of all thanks for creating this buildpack. I'm a bit new to CNBs and am trying this out for a Spring-Boot application right now but ran into an issue.

In my build.gradle, I set the following Buildpacks and environment:

bootBuildImage {
    buildpacks = ['docker://gcr.io/paketo-buildpacks/amazon-corretto', 'urn:cnb:builder:paketo-buildpacks/java', 'docker://gcr.io/paketo-buildpacks/health-checker']
    environment = ['BP_HEALTH_CHECKER_ENABLED': 'true', 'THC_PATH': '/actuator/health']
}

The build yields:

    [creator]     Paketo Buildpack for Health Checkers 1.2.0
    [creator]       https://github.com/paketo-buildpacks/health-checker
    [creator]       Build Configuration:
    [creator]         $BP_HEALTH_CHECKER_DEPENDENCY  thc   which health checker to contribute
    [creator]         $BP_HEALTH_CHECKER_ENABLED     true  contributes a health checker if enabled
    [creator]       Tiny Health Checker 0.8.0: Contributing to layer
    [creator]         Downloading from https://github.com/dmikusa/tiny-health-checker/releases/download/v0.8.0/thc-x86_64-unknown-linux-musl
    [creator]         Verifying checksum
    [creator]         Copying from /tmp/eca042851d6dc8c2158a923aaf0736d8b9141a10ee8b77a2a45c9528ef74799b/thc-x86_64-unknown-linux-musl to /layers/paketo-buildpacks_health-checker/thc/bin
    [creator]         Writing env.launch/health-check/THC_PATH.default
    [creator]       Process types:
    [creator]         health-check: thc (direct)

So, looks like to me it's correctly added. I can navigate down the layers dir and run the the 'thc' command successfully, however when I navigate to /cnb/process I see the following:

cnb@74a530064fc2:/cnb/process$ ls -al
total 8
drwxr-xr-x 2 root root 4096 Jan  1  1980 .
drwxr-xr-x 1 root root 4096 Jan  1  1980 ..
lrwxrwxrwx 1 root root   23 Jan  1  1980 executable-jar -> /cnb/lifecycle/launcher
lrwxrwxrwx 1 root root   23 Jan  1  1980 health-check -> /cnb/lifecycle/launcher
lrwxrwxrwx 1 root root   23 Jan  1  1980 task -> /cnb/lifecycle/launcher
lrwxrwxrwx 1 root root   23 Jan  1  1980 web -> /cnb/lifecycle/launcher
cnb@74a530064fc2:/cnb/process$

Is it correct, that the health-check process runs 'launcher'? I'd expect 'thc' here if I interpret https://github.com/paketo-buildpacks/health-checker/blob/ad173c6b5f3c0906cbc055de68fe05f86d2c9f1b/hc/tiny_health_checker.go#L78 correctly.

Thanks in advance!

dmikusa commented 1 year ago

Yes, that's correct. launcher is part of the buildpack spec. It is what ensure the buildpack contract is honored at runtime. Most notably, it loads environment variables set by buildpacks and kicks off the actual process. After the process starts, it steps out of the way.

You can see more about process types here.

feigi commented 1 year ago

Thanks for the quick response and the link. I skipped through this already but haven’t fully grasped it I guess. Still learning 😄. So, how would I execute the health check then. It does not seem to be in PATH. Is the idea to run the health check using the absolute path then or am I missing something?

dmikusa commented 1 year ago

/cnb/process will usually be on the path so if you want to run it, you just run health-check. Launcher will run the process and make sure that everything just works.

If you are going to enter the container and run it manually, you need to run /cnb/process/health-check or you need to enter the container where launcher kicks off your shell, then it'll set up PATH correctly. There's an example of entering the container using launcher and bash in the link I sent previously.

feigi commented 1 year ago

I can run health-check, but what I get is what looks to me like some startup logs from my spring app. I thought it would call thc. I‘ll just dive into the docs again. Thanks for your help!

feigi commented 1 year ago

Your explanations in conjunction with properly reading did the trick 🫣. Thanks again.

62mkv commented 11 months ago

just because I've been also struggling with this for a while, thought I could leave my solution to it:

(relevant for Spring Boot 2.7 with Gradle bootBuildImage task):

in the build.gradle, configure the buildpacks similar to:

tasks.named("bootBuildImage") {
    buildpacks = [
            'paketo-buildpacks/ca-certificates',
            'paketo-buildpacks/bellsoft-liberica',
            'paketo-buildpacks/syft',
            'paketo-buildpacks/executable-jar',
            'paketo-buildpacks/dist-zip',
            'paketo-buildpacks/spring-boot',
            'gcr.io/paketo-buildpacks/health-checker'
    ]
    environment('BP_HEALTH_CHECKER_ENABLED', 'true')
}

then, in the docker-compose.yml file, configure the Spring Boot entry with something like

  spring-boot-app:
    image: my-cool-app:latest
    environment:
      THC_PATH: /actuator/health
      THC_PORT: 8080
    healthcheck:
      test: ["CMD", "health-check"]
      start_interval: 30s
      interval: 5s
      retries: 10

and then, for the service you want to depend on the Spring Boot app being healthy, configure it like

    depends_on:
      spring-boot-app:
        condition: service_healthy

Hope it will save someone a minute :)