首页 > 解决方案 > uwsgi master 优雅关机

问题描述

我正在运行 uwsgi+flask 应用程序,该应用程序作为 k8s pod 运行。

当我部署一个新的 pod(新版本)时,现有的 pod 会得到 SIGTERM。

这会导致 master在同一时刻停止接受新连接,这会导致问题,因为 LB 仍将请求传递给 pod(再持续几秒钟)。

我希望主人在停止接受新连接之前等待 30 秒(当获得 SIGTERM 时)但找不到方法,这可能吗?

我的 uwsgi.ini 文件:[uwsgi]

;https://uwsgi-docs.readthedocs.io/en/latest/HTTP.html
http = :8080
wsgi-file = main.py
callable = wsgi_application
processes = 2
enable-threads = true
master = true
reload-mercy = 30
worker-reload-mercy = 30
log-5xx = true
log-4xx = true
disable-logging = true
stats = 127.0.0.1:1717
stats-http = true
single-interpreter= true
;https://github.com/containous/traefik/issues/615
http-keepalive=true
add-header = Connection: Keep-Alive

标签: pythonkubernetesuwsgi

解决方案


使用uwsgi似乎无法实现:

https://github.com/unbit/uwsgi/issues/1974

解决方案 - (如this kubernetes issue中所述):

https://github.com/kubernetes/contrib/issues/1140

就是使用prestop hook,比较难看但是有助于实现零停机:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        lifecycle:
          preStop:
            exec:
              command: ["/bin/sleep","5"]

模板取自这个答案:https ://stackoverflow.com/a/39493421/3659858


推荐阅读