uwsgi master正常关机

时间:2019-01-31 11:49:18

标签: python kubernetes uwsgi

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

当我部署新的pod(新版本)时,现有的pod将获得SIGTERM。

这会导致主服务器同时停止停止接受新的连接,这是导致问题的原因,因为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

2 个答案:

答案 0 :(得分:2)

使用uwsgi似乎无法实现这样的目的:

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

解决方案-(如本kubernetes问题所述):

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

是使用prestop挂钩,非常难看,但是将有助于实现零停机时间:

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

答案 1 :(得分:0)

另一种选择是使用CLI选项:

--hook-master-start "unix_signal:15 gracefully_kill_them_all"

或.ini文件中的

hook-master-start = "unix_signal:15 gracefully_kill_them_all"

这将在收到SIGTERM(信号15)后正常终止工作人员。

有关reference,请参见以下内容。

不过,当我尝试上述操作时,它在Docker容器中无法按预期工作。相反,您也可以使用uWSGI的Master FIFO文件。可以按以下方式指定主FIFO文件:

--master-fifo <filename>

master-fifo = /tmp/master-fifo

然后,您只需在文件中写入一个q字符,它将在退出之前正常关闭您的工作程序。

相关问题