如何在K8s ReadinessProbe和LivenessProbe中的套接字文件中使用gevent检查uwsgi

时间:2019-07-20 06:31:16

标签: kubernetes uwsgi gevent

我有一个带有uwsgi和gevent的flask应用。
这是我的app.ini 如何在kubernetes上编写readinessProbe和livenessProbe来检查烧瓶应用程序?

[uwsgi]
socket = /tmp/uwsgi.sock
chdir = /usr/src/app/
chmod-socket = 666
module = flasky
callable = app
master = false
processes = 1
vacuum = true
die-on-term = true
gevent = 1000
listen = 1024

2 个答案:

答案 0 :(得分:2)

我认为您真正要问的是“如何对uWSGI应用程序进行健康检查”。有一些示例工具可以执行此操作。特别是:

uwsgi-tools项目似乎在https://github.com/andreif/uwsgi-tools/issues/2#issuecomment-345195583上有最完整的示例。在Kubernetes Pod规范上下文中,结果可能看起来像这样:

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: myapp
    image: myimage
    livenessProbe:
      exec:
        command:
        - uwsgi_curl
        - -H
        - Host:host.name
        - /path/to/unix/socket
        - /health
      initialDelaySeconds: 5
      periodSeconds: 5

这还将假定您的应用程序对/health作为运行状况终结点做出了响应。

答案 1 :(得分:0)

您可以将uWSGI配置为同时在uwsgi-socket侧投放http-socket,并且仅将uwsgi-socket暴露给k8s service

在这种情况下,您的uwsgi.ini类似于:

[uwsgi]
socket = /tmp/uwsgi.sock
chdir = /usr/src/app/
chmod-socket = 666
module = flasky
callable = app
master = false
processes = 1
vacuum = true
die-on-term = true
gevent = 1000
listen = 1024

http-socket = 0.0.0.0:5050

假设您的应用中有/health个终结点,则k8s manifest可能类似于:

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: myapp
    image: myimage
    livenessProbe:
      httpGet:
        path: /health
        port: 5050
        httpHeaders:
        - name: Custom-Header
          value: Awesome
      initialDelaySeconds: 5
      periodSeconds: 5

在这种情况下,您的服务将通过socket = /tmp/uwsgi.sock作为上游k8s service的位置而到达,而k8s healthcheck服务可以到达http-socket: 5050的容器。