k8s优雅重启-简介

时间:2025-01-25 20:04:55

使用kubernetes启动容器时,一般都会配置一些探针来保证pod的健康,并通过terminationGracePeriodSeconds控制pod 在接收到终止信号后等待完成清理的最大时间。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: my-app-container
        image: my-app:latest
        ports:
        - containerPort: 8080
        readinessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 10
          timeoutSeconds: 2
          successThreshold: 1
          failureThreshold: 3
        livenessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 2
          successThreshold: 1
          failureThreshold: 10

通过就绪探针存活探针,使得容器启动就绪后才会有流量转发进来,容器故障后也能自动重启。
但对于请求成功率要求较为严格的应用,这种方式存在一个较为严重问题:
pod滚动发布的过程中,虽然terminationGracePeriodSeconds让容器在一定时间后再退出,给了执行中的请求一些处理时间。但是terminating的过程中还是不断会有新请求进来,最终还是会有些请求受影响。