How to check uwsgi with gevent at socket file in k8s readinessProbe and livenessProbe

Solution 1:

I think what you are really asking is "How to health check a uWSGI application". There are some example tools to do this. Particularly:

  • https://github.com/andreif/uwsgi-tools
  • https://github.com/che0/uwping
  • https://github.com/m-messiah/uwget

The uwsgi-tools project seems to have the most complete example at https://github.com/andreif/uwsgi-tools/issues/2#issuecomment-345195583. In a Kubernetes Pod spec context this might end up looking like:

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

This would also assume your application responded to /health as a health endpoint.

Solution 2:

You can configure uWSGI to serve both uwsgi-socket along side http-socket, and only expose the uwsgi-socket to the k8s service.

In this case your uwsgi.ini would looks something like:

[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

And assuming you have /health endpoint in your app, your k8s manifest can be something like:

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

In this case, your service will be reachable sitting as the upstream at socket = /tmp/uwsgi.sock via your k8s service and the k8s healthcheck service can reach your container at http-socket: 5050.