Feedback

Chat Icon

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Best Practices for Microservices: Health Checks
37%

Probing Mechanisms: HTTP, TCP, and Exec

Kubernetes supports three types of probing mechanisms for health checks:

  • HTTP probes: Used to determine if a container is alive or ready by sending an HTTP request to a specific endpoint. If the endpoint returns any code greater than or equal to 200 and less than 400, the probe succeeds. If the endpoint returns any other code, the probe fails.

  • Exec probes: Used to determine if a container is alive or ready by executing a command inside the container. If the command returns a zero exit code, the probe succeeds. If the command returns a non-zero exit code, the probe fails.

  • TCP probes: Used to determine if a container is alive or ready by trying to establish a TCP connection to a specific port. If the connection is established, the probe succeeds. If the connection fails, the probe fails.

The HTTP probe is, in most web applications, the most suitable one since it allows us to create a programmatic endpoint that can perform complex health checks. Imagine that a microservice relies on 5 different factors that determine its health, for example:

  • The MySQL database is reachable and responsive
  • The database migrations and new schemas were applied successfully
  • The Redis cache is reachable and responsive
  • The static files are accessible and the new ones were copied to S3
  • The third-party API is reachable and responsive

To check all of these, the developer would create a specific endpoint, for example, /healthz, that performs all these checks:

@app.route('/healthz', methods=['GET'])
def health_check():
    if not check_mysql_connection():
        return "MySQL is not reachable", 500
    if not check_database_migrations():
        return "Database migrations failed", 500
    if not check_redis_connection():
        return "Redis is not reachable", 500
    if not check_static_files():
        return "Static files are not accessible", 500
    if not check_third_party_api():
        return "Third-party API is not reachable", 500
    return

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Enroll now to unlock all content and receive all future updates for free.