What if your apps could fix themselves before you even notice a problem?
Why Health checks in containers in Microservices? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine running many small apps (containers) on your computer or server. You have to check each one manually to see if it is working fine. You open each app, look for errors, and try to guess if it will crash soon.
This manual checking is slow and tiring. You might miss problems because you can't watch all apps at once. If an app stops working, you only find out after users complain. Fixing issues late causes downtime and unhappy users.
Health checks in containers automatically test if each app is alive and working well. The system asks each container simple questions regularly. If a container does not answer correctly, it is restarted or fixed immediately without waiting for a human.
Check app status by logging into each container and running commands.Define a health check endpoint that the system pings regularly to verify container health.
It enables automatic detection and recovery from failures, keeping apps running smoothly without manual effort.
A popular online store uses health checks to restart any container that stops responding, ensuring customers always see a working website.
Manual checks are slow and unreliable for many containers.
Health checks automate monitoring and recovery.
This keeps services available and users happy.
Practice
Solution
Step 1: Understand container health checks
Health checks are used to confirm if a container is alive and working properly.Step 2: Identify the main goal
The main goal is to detect if the container is responsive and healthy, so it can be restarted if needed.Final Answer:
To verify if the container is running and responsive -> Option DQuick Check:
Health checks = verify container health [OK]
- Confusing health checks with resource allocation
- Thinking health checks update software
- Assuming health checks log network data
Solution
Step 1: Recall Docker health check syntax
The correct Dockerfile syntax uses HEALTHCHECK CMD followed by a command that returns 0 on success.Step 2: Identify the correct command
HEALTHCHECK CMD curl -f http://localhost/ || exit 1 uses 'curl -f' which fails on HTTP errors and 'exit 1' on failure, matching best practice.Final Answer:
HEALTHCHECK CMD curl -f http://localhost/ || exit 1 -> Option CQuick Check:
Docker healthcheck syntax = HEALTHCHECK CMD [OK]
- Using RUN instead of CMD in HEALTHCHECK
- Using EXECUTE or CHECK which are invalid keywords
- Not handling failure with exit codes
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
What happens if the container's /health endpoint returns HTTP 500 continuously?Solution
Step 1: Understand liveness probe behavior
Liveness probes check if a container is alive; failure triggers a restart of that container.Step 2: Analyze the HTTP 500 response effect
HTTP 500 means the endpoint is unhealthy, so Kubernetes marks the probe as failed and restarts the container.Final Answer:
Kubernetes restarts the container after failing the liveness probe -> Option AQuick Check:
Liveness probe failure = container restart [OK]
- Thinking Kubernetes ignores liveness failures
- Confusing liveness probe with scaling behavior
- Assuming pod shutdown instead of container restart
HEALTHCHECK CMD curl -f http://localhost:5000/health || exit 1But the container never restarts even when the service is down. What is the likely issue?
Solution
Step 1: Check health check command correctness
The command syntax is correct and uses curl -f with exit 1 on failure.Step 2: Consider container restart policy
If the container restart policy is not set to restart on failure, the container won't restart despite health check failures.Final Answer:
The container restart policy is not set to restart on failure -> Option AQuick Check:
Restart policy controls container restart on health failure [OK]
- Assuming health check command syntax is wrong
- Ignoring restart policy settings
- Thinking missing --interval causes no restart
Solution
Step 1: Understand liveness probe role
Liveness probes detect if a container is alive; failure triggers container restart.Step 2: Understand readiness probe role
Readiness probes check if a container is ready to serve traffic; failure removes it from load balancer routing.Step 3: Combine their functions
Liveness restarts unhealthy containers; readiness controls traffic flow to only healthy containers.Final Answer:
Liveness probe restarts unhealthy containers; readiness probe controls traffic routing to only ready containers -> Option BQuick Check:
Liveness = restart, Readiness = traffic control [OK]
- Mixing up readiness and liveness roles
- Thinking readiness probe restarts containers
- Assuming probes only log status without action
