持续集成

Spring on Kubernetes 最佳实践(三) 探针配

2020-04-18  本文已影响0人  yongaliang

Liveness 和 Readiness

/actuator/health/readiness 和 /actuator/health/liveness endpoint只有在Spring Boot 2.3.x 版本活以上才有,低于此版本的使用/actuator/health endpoint

配置Readiness探针

apiVersion: apps/v1
kind: Deployment
metadata:
  ...
  name: k8s-demo-app
spec:
...
  template:
    ...
    spec:
      containers:
        ...
        readinessProbe:
          httpGet:
            port: 8080
            path: /actuator/health/readiness

配置Liveness探针

apiVersion: apps/v1
kind: Deployment
metadata:
  ...
  name: k8s-demo-app
spec:
...
  template:
    ...
    spec:
      containers:
        ...
        livenessProbe:
          httpGet:
            port: 8080
            path: /actuator/health/liveness

优雅关闭应用程序

由于Kubnernetes以非同步方式关闭应用程序,因此在停止前请求还是有可能路由到即将关闭的应用程序。
为了解决这个问题,我们可以配置应用停止前先睡眠10秒,以便在终止请求请求不会路由到应用程序。
将preStop命令添加到您的deploy.yaml的podspec中

apiVersion: apps/v1
kind: Deployment
metadata:
  ...
  name: k8s-demo-app
spec:
...
  template:
    ...
    spec:
      containers:
        ...
        lifecycle:
          preStop:
            exec:
              command: ["sh", "-c", "sleep 10"]

server.shutdown.grace-period=30s

server.shutdown.grace-period 配置只有在Spring Boot 2.3.x 版本活以上才生效

重新构建容器和使新的k8s描述文件生效

./mvnw clean package
kubectl apply -f ./k8s

示例代码地址

上一篇 下一篇

猜你喜欢

热点阅读