A production-ready Go microservice with comprehensive health checks, designed for Kubernetes deployment and local development.
- Environment-Based Debug Access: Debug endpoints (
/debug/memory
) are automatically disabled in production - Input Validation: Whitelist validation for all debug actions
- Secure Error Handling: Generic error messages prevent information disclosure
- No File System Exposure: Limited system information access
# Set production environment
export ENVIRONMENT=prod
export APP_VERSION=1.0.0
# Debug endpoints will be automatically disabled
# Only essential health check endpoints available
# Enhanced security measures active
# In your Kubernetes ingress or load balancer
annotations:
nginx.ingress.kubernetes.io/configuration-snippet: |
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
- Production-ready health checks optimized for Kubernetes (tested on GKE, designed for any cluster)
- Graceful shutdown handling with 30-second timeout for zero-downtime deployments
- Environment-aware responses for different deployment stages
- Memory monitoring with system and Go runtime checks
- Local testing capabilities for simulating warning conditions
- JSON API with environment info, version, hostname, and timestamp
Platform Compatibility: This service uses standard Kubernetes health check endpoints (
/health
,/ready
) and Docker containers, making it compatible with any Kubernetes cluster (GKE, EKS, AKS, minikube, etc.) and any cloud provider. Currently tested on Google Kubernetes Engine (GKE).
Environment | Message | Emoji |
---|---|---|
dev |
Development environment - safe for debugging! | π οΈ |
test |
Test environment - safe for validation! | 𧬠|
stage |
Stage environment - safe for testing! | π§ͺ |
prod |
Live environment - handle with care! | π |
/
- Main endpoint with environment info/health
- Health check (status: "healthy" or "degraded")/ready
- Readiness check (status: "ready" or "not ready")/debug/memory
- Debug endpoint for testing memory warnings
The service implements graceful shutdown handling for clean termination in containerized environments:
- Listens for
SIGTERM
andSIGINT
signals (Kubernetes pod termination, Ctrl+C) - Stops accepting new requests immediately
- Allows existing requests to complete within 30-second timeout
- Logs shutdown process for monitoring and debugging
# In your deployment manifest
spec:
template:
spec:
terminationGracePeriodSeconds: 30 # Matches app timeout
containers:
- name: health-check-service
# ... other config
- Zero dropped connections during pod restarts/scaling
- Clean deployment rollouts without service interruption
- Load balancer friendly - proper connection cleanup
- Production reliability - prevents abrupt termination errors
- Signal received (SIGTERM from Kubernetes)
- Server stops accepting new requests
- Existing requests complete (up to 30 seconds)
- Clean shutdown with status logging
- Container exits with proper code
# Check current memory status
curl "http://localhost:8080/debug/memory?action=status"
# Allocate 150MB to trigger warning
curl "http://localhost:8080/debug/memory?action=allocate"
# Check health endpoint (should show "degraded")
curl -s http://localhost:8080/health | jq '.'
# Free memory and return to normal
curl "http://localhost:8080/debug/memory?action=free"
# Check health endpoint (should show "healthy")
curl -s http://localhost:8080/health | jq '.'
Normal (Healthy):
{
"status": "healthy",
"timestamp": "2025-08-28T22:16:56.312486885Z"
}
Warning (Degraded):
{
"status": "degraded",
"details": {
"go_memory": "WARNING: 159 MB"
},
"timestamp": "2025-08-28T22:17:15.486873192Z"
}
# Copy to start a new microservice
cp -r app/ my-new-service/
cd my-new-service
docker-compose up -d # Works immediately!
# Use with any Helm chart or manifest
apiVersion: apps/v1
kind: Deployment
metadata:
name: health-check-service
spec:
template:
spec:
containers:
- name: app
image: your-registry/health-check-service:latest
livenessProbe:
httpGet:
path: /health
port: 8080
readinessProbe:
httpGet:
path: /ready
port: 8080
# Configure load balancer to check /health endpoint
# Returns 200 for healthy, 503 for degraded
curl -I http://your-service/health
# Test health before deployment
curl -f http://localhost:8080/health || exit 1
curl -f http://localhost:8080/ready || exit 1
# Prometheus can scrape /health for metrics
# Grafana can use the JSON response for dashboards
# AlertManager can trigger on 503 responses
- New developers get working health checks immediately
- QA teams can test warning conditions locally
- DevOps have production-ready monitoring endpoints
- Architects can use as a reference implementation
- Docker Compose - Local development
- Kubernetes - Any cluster (tested on GKE, should work on EKS, AKS, minikube, etc.)
- Cloud Run - Serverless container deployment
- VM/Server - Traditional deployment
- Edge Computing - Lightweight health monitoring
ENVIRONMENT
- Set todev
,test
,stage
, orprod
(defaults todev
)APP_VERSION
- Application version (defaults todev
)PORT
- Server port (defaults to8080
)LOG_LEVEL
- Logging level (defaults todebug
)
- Docker Compose - Runs app on port 8080
- Health checks - Built-in container health monitoring
# Start the app
cd app
docker-compose up -d
# Test endpoints
curl http://localhost:8080/
curl http://localhost:8080/health
curl http://localhost:8080/ready
# Test memory simulation
curl "http://localhost:8080/debug/memory?action=allocate"
curl -s http://localhost:8080/health | jq '.'
- Kubernetes-ready health and readiness probes
- Graceful shutdown handling with 30-second timeout for clean container termination
- Memory leak detection with configurable thresholds
- System resource monitoring for production environments
- Fast response times optimized for high-frequency health checks
- Clean JSON responses for monitoring systems
- Docker and Docker Compose
- Go 1.24+ (for local development)
This service is designed to work with any deployment method:
- Local Development - Use docker-compose
- Kubernetes - Deploy with any Helm chart or manifest
- Cloud Run - Deploy as a container (Google Cloud)
- VM/Server - Run as a binary
- App Documentation - Detailed setup, testing, and production features
This repository is designed to be copied and reused in other projects:
# Clone the template repository
git clone https://github.com/userravs/go-health-check-service.git my-new-service
cd my-new-service
# Remove git history and start fresh
rm -rf .git
git init
git add .
git commit -m "Initial commit: health-check-service implementation"
# Start the service
docker-compose up -d # Works immediately!
# Copy source files (excluding git history)
cp -r app/* my-new-service/
cd my-new-service
# Initialize as new git repository
git init
git add .
git commit -m "Initial commit: health-check-service template"
# Start the service
docker-compose up -d # Works immediately!
go.mod
- Change module name to match new projectdocker-compose.yml
- Update service names if neededREADME.md
- Update project-specific information- Environment variables - Adjust for new project needs
Found a bug or have an idea? Contributions are welcome!
- Report bugs using the bug report template
- Suggest features using the feature request template
- Keep it simple - this is a focused utility app
- Be respectful in all interactions
Perfect for teams that want a production-ready health check service without building from scratch!