Middle
What did you do in the service for proper operation in Kubernetes (Graceful Shutdown, Readiness Probe)?
sobes.tech AI
Answer from AI
For the correct operation of the service in Kubernetes, it is important to implement:
- Graceful Shutdown — proper termination of the application when stopping the container. This helps avoid data loss and incorrect states. In Go, this is usually done by handling OS signals (
SIGTERM) to finish current operations and free resources before exiting.
Example of signal handling in Go:
import (
"os"
"os/signal"
"syscall"
"context"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
// Start graceful shutdown
cancel()
}()
// Run service with ctx
runServer(ctx)
}
- Readiness Probe — check if the application is ready to accept traffic. Kubernetes uses it to avoid sending requests to the pod before the service is initialized. Usually, this is an HTTP endpoint that returns a success status when the service is ready.
Example readiness probe in Kubernetes manifest:
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
Thus, the service starts correctly, operates, and terminates in Kubernetes, ensuring stability and reliability.