browsertrix/chart/templates/backend.yaml
Ilya Kreymer 90197b2a85
Backend mem usage fix - use fixed MOTOR_MAX_WORKERS + switch to gunicorn (#1468)
Refactors backend deployment to:
- Use MOTOR_MAX_WORKERS (defaulting to 1) to reduce threads used by
mongodb connections
- Also sets backend workers to 1 by default to reduce default memory
usage
- Switches to gunicorn with uvloop worker for production use instead of
uvicorn (as recommended by uvicorn)

Lower thread count should address memory leak/increased usage, which
resulted in 5x thread x cpus x workers, eg. potentially 20 or 40 threads
just for mongodb connections. Lower default number of workers should
make it easier to scale backend with HPA if additional capacity.

Fixes #1467
2024-01-16 15:32:42 -08:00

236 lines
5.7 KiB
YAML

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.name }}-backend
namespace: {{ .Release.Namespace }}
spec:
selector:
matchLabels:
app: {{ .Values.name }}
role: backend
replicas: {{ .Values.backend_num_replicas }}
template:
metadata:
labels:
app: {{ .Values.name }}
role: backend
annotations:
# force helm to update the deployment each time
{{- if not .Values.frontend_only }}
"helm.update": {{ randAlphaNum 5 | quote }}
{{- end }}
spec:
{{- if .Values.main_node_type }}
nodeSelector:
nodeType: {{ .Values.main_node_type }}
{{- end }}
volumes:
- name: config-volume
configMap:
name: shared-job-config
items:
- key: config.yaml
path: config.yaml
- name: ops-configs
secret:
secretName: ops-configs
- name: app-templates
configMap:
name: app-templates
- name: email-templates
configMap:
name: email-templates
containers:
- name: api
image: {{ .Values.backend_image }}
imagePullPolicy: {{ .Values.backend_pull_policy }}
command:
- gunicorn
- btrixcloud.main:app_root
- --bind
- "0.0.0.0:8000"
- --access-logfile
- "-"
- --workers
- "{{ .Values.backend_workers | default 1 }}"
- --worker-class
- uvicorn.workers.UvicornWorker
envFrom:
- configMapRef:
name: backend-env-config
- secretRef:
name: backend-auth
- secretRef:
name: mongo-auth
env:
- name: MOTOR_MAX_WORKERS
value: "{{ .Values.backend_mongodb_workers | default 1 }}"
volumeMounts:
- name: ops-configs
mountPath: /ops-configs/
- name: app-templates
mountPath: /app/btrixcloud/templates/
- name: email-templates
mountPath: /app/btrixcloud/email-templates/
resources:
limits:
memory: {{ .Values.backend_memory }}
requests:
cpu: {{ .Values.backend_cpu }}
memory: {{ .Values.backend_memory }}
startupProbe:
httpGet:
path: /healthz
port: 8000
initialDelaySeconds: 5
periodSeconds: 5
failureThreshold: 30
successThreshold: 1
readinessProbe:
httpGet:
path: /healthz
port: 8000
initialDelaySeconds: 5
periodSeconds: 30
failureThreshold: 5
successThreshold: 1
livenessProbe:
httpGet:
path: /healthz
port: 8000
initialDelaySeconds: 5
periodSeconds: 30
failureThreshold: 5
successThreshold: 1
- name: op
image: {{ .Values.backend_image }}
imagePullPolicy: {{ .Values.backend_pull_policy }}
command:
- gunicorn
- btrixcloud.main_op:app_root
- --bind
- "0.0.0.0:{{ .Values.opPort }}"
- --access-logfile
- "-"
- --workers
- "{{ .Values.backend_workers | default 1 }}"
- --worker-class
- uvicorn.workers.UvicornWorker
envFrom:
- configMapRef:
name: backend-env-config
- secretRef:
name: backend-auth
- secretRef:
name: mongo-auth
env:
- name: MOTOR_MAX_WORKERS
value: "{{ .Values.backend_mongodb_workers | default 1 }}"
volumeMounts:
- name: config-volume
mountPath: /config
- name: ops-configs
mountPath: /ops-configs/
- name: app-templates
mountPath: /app/btrixcloud/templates/
- name: email-templates
mountPath: /app/btrixcloud/email-templates/
resources:
limits:
memory: {{ .Values.backend_memory }}
requests:
cpu: {{ .Values.backend_cpu }}
memory: {{ .Values.backend_memory }}
startupProbe:
httpGet:
path: /healthz
port: {{ .Values.opPort }}
initialDelaySeconds: 5
periodSeconds: 5
failureThreshold: 30
successThreshold: 1
readinessProbe:
httpGet:
path: /healthz
port: {{ .Values.opPort }}
initialDelaySeconds: 5
periodSeconds: 30
failureThreshold: 5
successThreshold: 1
livenessProbe:
httpGet:
path: /healthz
port: {{ .Values.opPort }}
initialDelaySeconds: 5
periodSeconds: 30
failureThreshold: 5
successThreshold: 1
---
apiVersion: v1
kind: Service
metadata:
namespace: {{ .Release.Namespace }}
name: {{ .Values.name }}-backend
labels:
app: {{ .Values.name }}
role: backend
{{- if .Values.service }}
{{- if .Values.service.annotations }}
annotations:
{{- range $key, $val := .Values.service.annotations }}
{{ $key }}: {{ $val | quote }}
{{- end }}
{{- end }}
{{- end }}
spec:
selector:
app: {{ .Values.name }}
role: backend
ports:
- protocol: TCP
port: 8000
name: api
- protocol: TCP
port: {{ .Values.opPort }}
name: operator