0
0
GCPcloud~5 mins

Health checks configuration in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Health checks help Google Cloud know if your app or service is working well. They check your app regularly and tell Google Cloud if it needs to restart or stop sending traffic to a broken app.
When you want to make sure your app is ready before sending users to it.
When you want to automatically restart an app that stops working.
When you want to balance traffic only to healthy app instances.
When you want to monitor the health of your backend services.
When you want to avoid downtime by detecting problems early.
Config File - health-check.yaml
health-check.yaml
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeHealthCheck
metadata:
  name: example-health-check
spec:
  checkIntervalSec: 10
  timeoutSec: 5
  healthyThreshold: 3
  unhealthyThreshold: 3
  httpHealthCheck:
    port: 8080
    requestPath: /health
  type: HTTP

This file creates a health check resource in Google Cloud.

checkIntervalSec sets how often Google Cloud checks your app.

timeoutSec is how long it waits for a response.

healthyThreshold is how many successful checks in a row make the app healthy.

unhealthyThreshold is how many failed checks in a row make the app unhealthy.

httpHealthCheck defines the port and path to check your app's health.

type is the protocol used for the check.

Commands
This command creates the health check in Google Cloud using the configuration file.
Terminal
gcloud deployment-manager deployments create example-health-check --config health-check.yaml
Expected OutputExpected
Waiting for create [operations/operation-1234567890abcdef]...done. Create operation operation-1234567890abcdef completed successfully.
--config - Specifies the configuration file to use for deployment.
This command shows the details of the health check to verify it was created correctly.
Terminal
gcloud compute health-checks describe example-health-check
Expected OutputExpected
checkIntervalSec: 10 timeoutSec: 5 healthyThreshold: 3 unhealthyThreshold: 3 httpHealthCheck: port: 8080 requestPath: /health type: HTTP name: example-health-check
This command attaches the health check to a backend service so Google Cloud can monitor its health.
Terminal
gcloud compute backend-services update example-backend-service --health-checks example-health-check --global
Expected OutputExpected
Updated [https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/example-backend-service].
--health-checks - Specifies which health check to attach.
--global - Indicates the backend service is global.
This command checks the current health status of the backend service using the attached health check.
Terminal
gcloud compute backend-services get-health example-backend-service --global
Expected OutputExpected
healthStatus: - healthState: HEALTHY instance: https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-instance ipAddress: 10.128.0.2 port: 8080
--global - Indicates the backend service is global.
Key Concept

If you remember nothing else from this pattern, remember: health checks tell Google Cloud if your app is working so it can manage traffic and restarts automatically.

Common Mistakes
Setting the health check path to a URL that does not return success.
Google Cloud will mark the app as unhealthy and stop sending traffic to it.
Make sure the path used in the health check returns a simple success response like HTTP 200.
Using too short timeout or check intervals.
This can cause false failures if the app is slow to respond occasionally.
Set reasonable timeout and interval values that match your app's response times.
Not attaching the health check to the backend service.
Google Cloud will not monitor the service health and may send traffic to broken instances.
Always update your backend service to use the health check after creating it.
Summary
Create a health check configuration file defining how Google Cloud checks your app's health.
Deploy the health check using gcloud deployment-manager.
Attach the health check to your backend service to monitor its health.
Use gcloud commands to verify the health check and backend service status.