我最新最全的文章都在 南瓜慢说 www.pkslow.com ,欢送大家来喝茶!

1 探针的作用

Kubernetes的容器生命周期治理中,有三种探针,首先要晓得,这探针是属于容器的,而不是Pod

  • 存活探针:Liveness
  • 就绪探针:Readiness
  • 启动探针:Startup

Liveness探针能够晓得什么时候要重启容器,如果发现容器不衰弱,就会杀死并从新创立新的容器。

Readniess探针能够晓得要不要拜访容器,如果发现容器不衰弱,就不会把申请路由到该容器。

Startup探针能够晓得应用程序容器什么时候启动了。 如果配置了这类探测器,就能够管制容器在启动胜利后再进行存活性和就绪查看, 确保这些存活、就绪探测器不会影响应用程序的启动。 这能够用于对慢启动容器进行存活性检测,防止它们在启动运行之前就被杀掉。

2 配置示例

2.1 存活Liveness

2.1.1 命令形式

apiVersion: v1kind: Podmetadata:  labels:    test: liveness  name: liveness-execspec:  containers:  - name: liveness    image: k8s.gcr.io/busybox    args:    - /bin/sh    - -c    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600    livenessProbe:      exec:        command:        - cat        - /tmp/healthy      initialDelaySeconds: 5      periodSeconds: 5

2.1.2 HTTP形式

apiVersion: v1kind: Podmetadata:  labels:    test: liveness  name: liveness-httpspec:  containers:  - name: liveness    image: k8s.gcr.io/liveness    args:    - /server    livenessProbe:      httpGet:        path: /healthz        port: 8080        httpHeaders:        - name: Custom-Header          value: Awesome      initialDelaySeconds: 3      periodSeconds: 3

2.1.3 TCP形式

apiVersion: v1kind: Podmetadata:  name: goproxy  labels:    app: goproxyspec:  containers:  - name: goproxy    image: k8s.gcr.io/goproxy:0.1    ports:    - containerPort: 8080    readinessProbe:      tcpSocket:        port: 8080      initialDelaySeconds: 5      periodSeconds: 10    livenessProbe:      tcpSocket:        port: 8080      initialDelaySeconds: 15      periodSeconds: 20

2.2 就绪Readiness

Liveness相似:

readinessProbe:  exec:    command:    - cat    - /tmp/healthy  initialDelaySeconds: 5  periodSeconds: 5

2.3 应用启动探测器爱护慢启动容器

幸好有启动探测,应用程序将会有最多 5 分钟(30 * 10 = 300s) 的工夫来实现它的启动。 一旦启动探测胜利一次,存活探测工作就会接管对容器的探测,对容器死锁能够疾速响应。 如果启动探测始终没有胜利,容器会在 300 秒后被杀死,并且依据 restartPolicy 来设置 Pod 状态。

ports:- name: liveness-port  containerPort: 8080  hostPort: 8080livenessProbe:  httpGet:    path: /healthz    port: liveness-port  failureThreshold: 1  periodSeconds: 10startupProbe:  httpGet:    path: /healthz    port: liveness-port  failureThreshold: 30  periodSeconds: 10

3 Springboot利用的配置

Springboot 2.3新增了探针,具体门路如下:

存活:/actuator/health/liveness

就绪:/actuator/health/readiness

须要通过增加actuator,并通过属性配置关上对应性能:

management.endpoints.web.exposure.include="*"management.health.livenessState.enabled=truemanagement.health.readinessState.enabled=truemanagement.endpoint.health.probes.enabled=truemanagement.endpoint.health.probes.show-details=always

欢送关注微信公众号<南瓜慢说>,将继续为你更新...

多读书,多分享;多写作,多整顿。