Alex Cohen
Alex Cohen

Reputation: 6206

How to use SERVICE_CHECK_HTTP with progrium/consul check-http script?

I am running the progrium/consul container with the gliderlabs/registrator container. I would like to be able to automatically create health checks for any container that is registered to consul with the registrator. Using this I would like to use consul health checks to know if any container has stopped running. I have read that there is a way to do this by adding environmental variables, but everything I have read has been far too vague, such as the post below:

how to define HTTP health check in a consul container for a service on the same host?

So I am supposed to set some environmental variables:

ENV SERVICE_CHECK_HTTP=/howareyou
ENV SERVICE_CHECK_INTERVAL=5s

Do I set them inside of my progrium/consul container or my gliderlabs/registrator? Would I set them by just adding the following tags inside my docker run command like this?

docker run ...... -e SERVICE_CHECK_HTTP=howareyou -e SERVICE_CHECK_INTERVAL=5s ......

Note: for some reason adding the above environmental variables to the docker run commands of my registrator just caused consul to think my nodes are failing from no acks received

Upvotes: 1

Views: 1285

Answers (1)

dma
dma

Reputation: 99

I got Consul Health Checks and Gliderlabs Registrator working in three ways with my Spring Boot apps:

  • Put the environment variables in the Dockerfile with ENV or LABEL

  • Put the environment variables using -e with docker run

  • Put the environment variables into docker-compose.yml under "environment" or "labels"

Dockerfile

In your Dockerfile-file:

ENV SERVICE_NAME MyApp
ENV SERVICE_8080_CHECK_HTTP /health
ENV SERVICE_8080_CHECK_INTERVAL 60s

The /health endpoint here is coming from the Spring Boot Actuator lib that I simply put in my pom.xml file in my Spring Boot application. You can however use any other endpoint as well.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

docker run

docker run -d -e "SERVICE_NAME=myapp" -e "SERVICE_8080_CHECK_HTTP=/health" -e "SERVICE_8080_CHECK_INTERVAL=10s" -p 8080:8080 --name MyApp myapp

Make sure that you are using the correct HTTP server port and that it is accessible. In my case, Spring Boot uses 8080 by default.

Docker Compose

Add the health check information under either the "environment" or "labels" properties:

myapp:
    image: apps/myapp
    restart: always
    environment:
     - SERVICE_NAME=MyApp
     - SERVICE_8080_CHECK_HTTP=/health
     - SERVICE_8080_CHECK_INTERVAL=60s
    ports:
     - "8080:8080"

Starting Consul Server

docker run -d -p "8500:8500" -h "consul" --name consul gliderlabs/consul-server -server -bootstrap

The "gliderlabs/consul-server" image activates the Consul UI by default. So you don't have to specify any other parameters.

Then start Registrator

docker run -d \
    --name=registrator \
    -h $(docker-machine ip dockervm) \
    -v=/var/run/docker.sock:/tmp/docker.sock \
    gliderlabs/registrator:v6 -resync 120 -deregister on-success \
    consul://$(docker-machine ip dockervm):8500

The "resync" and "deregister" parameters will ensure that Consul and Registrator will be in synch.

Upvotes: 2

Related Questions