首页 > 解决方案 > Kubernetes 中的 uWSGI 配置

问题描述

我正在使用带有 uWSGI 的 Python 和 Django 运行我的后端。我们最近将它迁移到了 Kubernetes (GKE),我们的 pod 正在消耗大量内存,而集群的其余部分正在匮乏资源。我们认为这可能与 uWSGI 配置有关。

这是我们用于 pod 的 yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-pod
  namespace: my-namespace
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 10
      maxUnavailable: 10
  selector:
    matchLabels:
      app: my-pod
  template:
    metadata:
      labels:
        app: my-pod
    spec:
      containers:
      - name: web
        image: my-img:{{VERSION}}
        imagePullPolicy: IfNotPresent
        ports:
          - containerPort: 8000
            protocol: TCP
        command: ["uwsgi", "--http", ":8000", "--wsgi-file", "onyo/wsgi.py", "--workers", "5", "--max-requests", "10", "--master", "--vacuum", "--enable-threads"]
        resources:
          requests:
            memory: "300Mi"
            cpu: 150m
          limits:
            memory: "2Gi"
            cpu: 1
        livenessProbe:
          httpGet:
            httpHeaders:
              - name: Accept
                value: application/json
            path: "/healthcheck"
            port: 8000
          initialDelaySeconds: 15
          timeoutSeconds: 5
          periodSeconds: 30
        readinessProbe:
          httpGet:
            httpHeaders:
              - name: Accept
                value: application/json
            path: "/healthcheck"
            port: 8000
          initialDelaySeconds: 15
          timeoutSeconds: 5
          periodSeconds: 30
        envFrom:
          - configMapRef:
              name: configmap
          - secretRef:
              name: secrets
        volumeMounts:
        - name: service-account-storage-credentials-volume
          mountPath: /credentials
          readOnly: true
      - name: csql-proxy
        image: gcr.io/cloudsql-docker/gce-proxy:1.11
        command: ["/cloud_sql_proxy",
                  "-instances=my-project:region:backend=tcp:1234",
                  "-credential_file=/secrets/credentials.json"]
        ports:
          - containerPort: 1234
            name: sql
        securityContext:
          runAsUser: 2  # non-root user
          allowPrivilegeEscalation: false
        volumeMounts:
          - name: credentials
            mountPath: /secrets/sql
            readOnly: true
      volumes:
        - name: credentials
          secret:
            secretName: credentials
        - name: volume
          secret:
            secretName: production
            items:
            - key: APPLICATION_CREDENTIALS_CONTENT
              path: key.json

我们使用与迁移之前相同的 uWSGI 配置(当后端在 VM 中执行时)。

是否有在 K8s 中运行 uWSGI 的最佳实践配置?或者也许我在这个特定的配置中做错了什么?

标签: pythondjangokubernetesuwsgi

解决方案


您在 uwsgi 中激活了 5 个工作程序,如果您的应用程序使用延迟加载技术,这可能意味着内存需求的 5 倍(我的建议:在启动时加载所有内容并信任 pre-fork检查这个)。但是,您可以尝试减少工作人员的数量,而不是增加线程的数量。

此外,您应该删除 max-requests,这会使您的应用每 10 个请求重新加载一次,这在生产环境(doc)中是没有意义的。如果您遇到内存泄漏问题,请改用 reload-on-rss。

我会做这样的事情,根据您的应用程序使用它的方式,每个工作人员可能更少或更多线程(根据生产中每个 pod 的 cpu 使用/可用性进行调整):

command: ["uwsgi", "--http", ":8000", "--wsgi-file", "onyo/wsgi.py", "--workers", "2", "--threads", "10", "--master", "--vacuum", "--enable-threads"]

ps:正如 zerg 在评论中所说,你当然应该确保你的应用程序没有运行 DEBUG 模式,以及低日志输出。


推荐阅读