我使用和uWSGI运行我的后端。我们最近将它迁移到Kubernetes (GKE),我们的豆荚消耗了大量内存,集群的其余部分都急需资源。我们认为这可能与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我们使用与迁移前相同的uWSGI配置(在VM中执行后端时)。
是否有在uWSGI中运行K8s的最佳实践配置?或者我在这个特定的配置中做错了什么?
发布于 2020-04-18 06:27:21
您在uwsgi中激活了5名工作人员,如果应用程序使用延迟加载技术,这意味着需要5倍的内存(我的建议是:在启动时加载所有内容,并信任预分叉看看这个)。但是,您可以尝试减少工作人员的数量,而不是增加线程的数量。
另外,您应该删除最大请求,这使得您的应用程序每10个请求重新加载一次,这在生产环境(医生)中是没有意义的。如果您在内存泄漏方面有问题,请使用rss上的重新加载。
我会这样做,根据应用程序的使用方式(根据生产中每个荚的cpu使用率/可用性进行调整),每个工作人员的线程数可能更少或更多:
command: ["uwsgi", "--http", ":8000", "--wsgi-file", "onyo/wsgi.py", "--workers", "2", "--threads", "10", "--master", "--vacuum", "--enable-threads"]
ps:正如虫格在评论中说的那样,你当然应该确保你的应用程序没有运行调试模式,同时日志记录输出也很低。
https://stackoverflow.com/questions/57661119
复制相似问题