Kubernetes中的uWSGI配置

问题描述 投票:0回答:1

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

这是我们的豆荚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

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

是否有用于在K8s中运行uWSGI的最佳实践配置?还是在此特定配置中我做错了什么?

python django kubernetes uwsgi
1个回答
0
投票

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

此外,您应该删除最大请求数,这会使您的应用每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:就像虫族在评论中说的那样,您当然应该确保您的应用程序未运行DEBUG模式,并且日志输出较低。

© www.soinside.com 2019 - 2024. All rights reserved.