---

title: Nginx Ingress零停机发布与连接保持实践

keywords:

  • Nginx Ingress
  • 零停机
  • 连接保持
  • keepalive
  • 缓慢启动
  • upstream

description: 通过Nginx Ingress与后端配置连接保持与缓慢启动,提供可验证注解与命令,实现零停机发布与稳定回源。

date: 2025-11-26

categories:

  • 文章资讯
  • 技术教程

---

概述

  • 目标:在发布期间保持现有连接与会话,缓慢引入新版本后端,避免突发负载与连接中断。
  • 适用:需要平滑发布与长连接业务(WebSocket、HTTP/2)。

核心与实战

  • Ingress注解(连接保持与缓慢启动):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web
  namespace: prod
  annotations:
    nginx.ingress.kubernetes.io/keepalive: "64"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "30"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "30"
    nginx.ingress.kubernetes.io/slow-start: "true"
spec:
  rules:
    - host: app.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web-svc
                port:
                  number: 8080
  • Service与Deployment(就绪探针保障):
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: prod
spec:
  replicas: 3
  selector:
    matchLabels: { app: web }
  template:
    metadata:
      labels: { app: web }
    spec:
      containers:
        - name: web
          image: repo/web:2.0.0
          readinessProbe:
            httpGet: { path: /healthz, port: 8080 }
            initialDelaySeconds: 5
            periodSeconds: 5
          livenessProbe:
            httpGet: { path: /livez, port: 8080 }
            initialDelaySeconds: 10
            periodSeconds: 10

示例

  • 滚动发布与验证连接保持:
kubectl -n prod rollout restart deployment/web
watch -n1 kubectl -n prod get pods -l app=web
  • 监控Nginx Ingress连接:
kubectl -n ingress-nginx exec deploy/ingress-nginx-controller -- curl -s http://127.0.0.1:10254/metrics | grep nginx_ingress_controller_connections

验证与监控

  • 会话与长连接:
  • 确保就绪探针通过后才接入流量;缓慢启动减少新Pod瞬时压力。
  • 回源稳定性:
  • 观察keepalive连接数量与超时;根据后端能力调整。
  • 错误与回滚:
  • 发布异常时回滚镜像版本;确保Ingress与Service不需改动即可回退。

常见误区

  • 未设置就绪探针导致未准备就绪的Pod接收流量;需合理探针。
  • 关闭keepalive导致每次回源重建连接;增加延迟与负载。
  • 忽视缓慢启动在高并发场景的作用;新版本易被瞬时压垮。

结语

  • 通过Ingress注解与探针、缓慢启动与连接保持,可实现零停机发布并维持回源稳定性与用户体验。

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部