feat: process attachments through durable versioned jobs

This commit is contained in:
Vladislav Agapov
2026-10-29 16:56:06 +03:00
parent 276dbdad8a
commit e35c8f0b75
235 changed files with 17028 additions and 44 deletions
+6
View File
@@ -0,0 +1,6 @@
apiVersion: v2
name: messenger
description: Учебный мессенджер с PostgreSQL и шардированным Valkey
version: 0.1.0
appVersion: lab5
type: application
+7
View File
@@ -0,0 +1,7 @@
{{- define "messenger.shards" -}}
{{- $nodes := list -}}
{{- range $i := until (int .Values.valkey.shards) -}}
{{- $nodes = append $nodes (printf "shard%d=valkey-%d.valkey:6379" $i $i) -}}
{{- end -}}
{{- join "," $nodes -}}
{{- end -}}
+135
View File
@@ -0,0 +1,135 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: messenger-config
namespace: {{ .Release.Namespace }}
data:
APP_PORT: '8080'
VALKEY_COORDINATOR: shard0
VALKEY_SHARDS: {{ include "messenger.shards" . | quote }}
{{- range $key, $value := .Values.api.env }}
{{ $key }}: {{ $value | quote }}
{{- end }}
---
apiVersion: v1
kind: Service
metadata:
labels:
app: api
name: api
namespace: {{ .Release.Namespace }}
spec:
selector:
app: api
ports:
- name: http
port: 8080
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
namespace: {{ .Release.Namespace }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.api.replicas }}
{{- end }}
strategy:
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
selector:
matchLabels:
app: api
template:
metadata:
annotations:
checksum/config: {{ printf "%s%s" (include "messenger.shards" .) (toJson .Values.api.env) | sha256sum }}
labels:
app: api
spec:
{{- with .Values.api.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
imagePullSecrets:
- name: {{ .Values.imagePullSecret }}
terminationGracePeriodSeconds: 20
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: api
securityContext:
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
containers:
- name: api
image: "{{ .Values.image.repository }}@{{ .Values.image.digest }}"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
envFrom:
- configMapRef:
name: messenger-config
env:
- name: S3_ENDPOINT
value: s3:9000
- name: S3_BUCKET
value: {{ .Values.s3.bucket }}
- name: S3_TLS
value: "false"
- name: S3_ACCESS_KEY
valueFrom:
secretKeyRef: {name: {{ .Values.s3.secret }}, key: access-key}
- name: S3_SECRET_KEY
valueFrom:
secretKeyRef: {name: {{ .Values.s3.secret }}, key: secret-key}
- name: INSTANCE_ID
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: CURSOR_SIGNING_KEY
valueFrom:
secretKeyRef:
name: {{ .Values.databaseSecret }}
key: cursor-key
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: {{ .Values.databaseSecret }}
key: database-url
resources:
{{- toYaml .Values.api.resources | nindent 12 }}
lifecycle:
preStop:
sleep:
seconds: 3
startupProbe:
httpGet:
path: /health/ready
port: 8080
periodSeconds: 2
failureThreshold: 30
readinessProbe:
timeoutSeconds: {{ .Values.api.readiness.timeoutSeconds }}
failureThreshold: {{ .Values.api.readiness.failureThreshold }}
httpGet:
path: /health/ready
port: 8080
periodSeconds: 2
livenessProbe:
httpGet:
path: /health/live
port: 8080
periodSeconds: 5
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: [ALL]
+7
View File
@@ -0,0 +1,7 @@
{{- $live := lookup "v1" "ConfigMap" .Release.Namespace "messenger-config" -}}
{{- if and $live (ne (index $live.data "VALKEY_SHARDS") (include "messenger.shards" .)) -}}
{{- $api := lookup "apps/v1" "Deployment" .Release.Namespace "api" -}}
{{- if or (gt (int .Values.api.replicas) 0) (and $api (gt (int $api.spec.replicas) 0)) -}}
{{- fail "Changing shards requires drained API: use make reshard SHARDS=2 or SHARDS=3" -}}
{{- end -}}
{{- end -}}
+33
View File
@@ -0,0 +1,33 @@
{{- if .Values.autoscaling.enabled }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: {{ .Values.autoscaling.minReplicas }}
maxReplicas: {{ .Values.autoscaling.maxReplicas }}
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: {{ .Values.autoscaling.cpuPercent }}
behavior:
scaleUp:
stabilizationWindowSeconds: 15
policies:
- type: Pods
value: 1
periodSeconds: 30
scaleDown:
stabilizationWindowSeconds: 120
policies:
- type: Pods
value: 1
periodSeconds: 60
{{- end }}
+23
View File
@@ -0,0 +1,23 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: messenger
annotations:
cert-manager.io/cluster-issuer: {{ .Values.ingress.clusterIssuer | quote }}
spec:
ingressClassName: {{ .Values.ingress.className }}
tls:
- hosts:
- {{ .Values.ingress.host | quote }}
secretName: messenger-tls
rules:
- host: {{ .Values.ingress.host | quote }}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: api
port:
number: 8080
+39
View File
@@ -0,0 +1,39 @@
{{- if .Values.load.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
name: load-{{ .Values.load.run }}
spec:
backoffLimit: 0
activeDeadlineSeconds: 300
ttlSecondsAfterFinished: 86400
template:
metadata:
labels:
app: load
spec:
restartPolicy: Never
containers:
- name: k6
image: grafana/k6:1.3.0
args: [run, --quiet, /scripts/{{ .Values.load.script }}]
env:
- name: BASE_URL
value: "https://{{ .Values.ingress.host }}"
- name: TARGET_INSTANCE_ID
value: {{ .Values.load.target | default "" | quote }}
- name: DURATION
value: {{ .Values.load.duration | quote }}
- name: VUS
value: {{ .Values.load.vus | quote }}
resources:
requests: {cpu: 250m, memory: 128Mi}
limits: {cpu: '2', memory: 256Mi}
volumeMounts:
- name: scripts
mountPath: /scripts
volumes:
- name: scripts
configMap:
name: load-scripts
{{- end }}
+32
View File
@@ -0,0 +1,32 @@
apiVersion: batch/v1
kind: Job
metadata:
name: migrate-{{ .Release.Revision }}
spec:
activeDeadlineSeconds: 180
backoffLimit: 3
ttlSecondsAfterFinished: 86400
template:
metadata:
labels:
app: migrate
spec:
restartPolicy: OnFailure
imagePullSecrets:
- name: {{ .Values.imagePullSecret }}
containers:
- name: migrate
image: "{{ .Values.image.repository }}@{{ .Values.image.digest }}"
args: [migrate]
envFrom:
- configMapRef:
name: messenger-config
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: {{ .Values.databaseSecret }}
key: database-url
resources:
requests: {cpu: 50m, memory: 32Mi}
limits: {cpu: 500m, memory: 128Mi}
@@ -0,0 +1,19 @@
{{- if .Values.monitoring.serviceMonitor }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: api
labels:
app: api
spec:
namespaceSelector:
matchNames: [{{ .Release.Namespace }}]
selector:
matchLabels:
app: api
endpoints:
- port: http
path: /metrics
interval: 15s
scrapeTimeout: 3s
{{- end }}
@@ -0,0 +1,59 @@
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: private-databases
namespace: {{ .Release.Namespace }}
spec:
podSelector:
matchExpressions:
- key: app
operator: In
values: [postgres, valkey]
policyTypes: [Ingress]
ingress:
- from:
- podSelector:
matchLabels: {app: worker}
ports:
- {protocol: TCP, port: 5432}
- from:
- podSelector:
matchLabels:
app: api
ports:
- protocol: TCP
port: 5432
- protocol: TCP
port: 6379
- from:
- podSelector:
matchLabels:
app: migrate
ports:
- protocol: TCP
port: 5432
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: private-api
spec:
podSelector:
matchLabels:
app: api
policyTypes: [Ingress]
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: traefik
ports:
- protocol: TCP
port: 8080
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
ports:
- protocol: TCP
port: 8080
+66
View File
@@ -0,0 +1,66 @@
apiVersion: v1
kind: Service
metadata:
name: postgres
namespace: {{ .Release.Namespace }}
spec:
selector:
app: postgres
ports:
- port: 5432
targetPort: 5432
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
namespace: {{ .Release.Namespace }}
spec:
serviceName: postgres
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
terminationGracePeriodSeconds: 30
containers:
- name: postgres
image: {{ .Values.postgres.image | quote }}
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: messenger
- name: POSTGRES_USER
value: messenger
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.databaseSecret }}
key: postgres-password
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
resources:
{{- toYaml .Values.postgres.resources | nindent 12 }}
readinessProbe:
exec:
command: [pg_isready, -U, messenger, -d, messenger]
initialDelaySeconds: 3
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
{{- if .Values.storageClass }}
storageClassName: {{ .Values.storageClass | quote }}
{{- end }}
accessModes: [ReadWriteOnce]
resources:
requests:
storage: {{ .Values.postgres.storage }}
+97
View File
@@ -0,0 +1,97 @@
apiVersion: v1
kind: Service
metadata:
name: s3
spec:
selector: {app: s3}
ports:
- {name: s3, port: 9000, targetPort: 9000}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: s3
spec:
serviceName: s3
replicas: 1
selector:
matchLabels: {app: s3}
template:
metadata:
labels: {app: s3}
spec:
securityContext:
runAsUser: 1000
runAsNonRoot: true
fsGroup: 1000
containers:
- name: s3
image: {{ .Values.s3.image }}
args: [server, -dir=/data, -s3, -s3.port=9000, -s3.config=/identity/identities.json, -master.volumeSizeLimitMB=100, -volume.max=5]
ports: [{containerPort: 9000}]
readinessProbe:
tcpSocket: {port: 9000}
resources:
requests: {cpu: 100m, memory: 256Mi}
limits: {cpu: '1', memory: 512Mi}
securityContext:
allowPrivilegeEscalation: false
capabilities: {drop: [ALL]}
volumeMounts:
- {name: objects, mountPath: /data}
- {name: identity, mountPath: /identity, readOnly: true}
volumes:
- name: identity
secret: {secretName: {{ .Values.s3.secret }}, items: [{key: identities.json, path: identities.json}]}
volumeClaimTemplates:
- metadata: {name: objects}
spec:
accessModes: [ReadWriteOnce]
resources:
requests: {storage: {{ .Values.s3.storage }}}
---
apiVersion: batch/v1
kind: Job
metadata:
name: s3-bootstrap-{{ .Release.Revision }}
spec:
ttlSecondsAfterFinished: 3600
backoffLimit: 4
template:
metadata:
labels: {app: s3-bootstrap}
spec:
restartPolicy: OnFailure
imagePullSecrets: [{name: {{ .Values.imagePullSecret }}}]
containers:
- name: bootstrap
image: "{{ .Values.image.repository }}@{{ .Values.image.digest }}"
args: [s3-bootstrap]
env:
- {name: S3_ENDPOINT, value: 's3:9000'}
- {name: S3_TLS, value: 'false'}
- {name: S3_BUCKET, value: {{ .Values.s3.bucket }}}
- name: S3_ACCESS_KEY
valueFrom:
secretKeyRef: {name: {{ .Values.s3.secret }}, key: root-user}
- name: S3_SECRET_KEY
valueFrom:
secretKeyRef: {name: {{ .Values.s3.secret }}, key: root-password}
resources:
requests: {cpu: 50m, memory: 64Mi}
limits: {cpu: 200m, memory: 128Mi}
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: private-s3
spec:
podSelector:
matchLabels: {app: s3}
policyTypes: [Ingress]
ingress:
- from:
- podSelector:
matchExpressions:
- {key: app, operator: In, values: [api, worker, s3-bootstrap, reconcile]}
ports: [{protocol: TCP, port: 9000}]
+57
View File
@@ -0,0 +1,57 @@
apiVersion: v1
kind: Service
metadata:
name: valkey
namespace: {{ .Release.Namespace }}
spec:
clusterIP: None
selector:
app: valkey
ports:
- port: 6379
targetPort: 6379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: valkey
namespace: {{ .Release.Namespace }}
spec:
serviceName: valkey
replicas: {{ .Values.valkey.shards }}
podManagementPolicy: Parallel
selector:
matchLabels:
app: valkey
template:
metadata:
labels:
app: valkey
spec:
terminationGracePeriodSeconds: 30
containers:
- name: valkey
image: {{ .Values.valkey.image | quote }}
args: [valkey-server, --appendonly, 'yes', --appendfsync, always, --maxmemory, {{ .Values.valkey.maxmemory | quote }}, --maxmemory-policy, noeviction]
ports:
- containerPort: 6379
resources:
{{- toYaml .Values.valkey.resources | nindent 12 }}
readinessProbe:
exec:
command: [valkey-cli, ping]
initialDelaySeconds: 2
volumeMounts:
- name: data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: data
spec:
{{- if .Values.storageClass }}
storageClassName: {{ .Values.storageClass | quote }}
{{- end }}
accessModes: [ReadWriteOnce]
resources:
requests:
storage: {{ .Values.valkey.storage }}
+111
View File
@@ -0,0 +1,111 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: worker
spec:
replicas: {{ .Values.worker.replicas }}
selector:
matchLabels: {app: worker}
template:
metadata:
labels: {app: worker}
spec:
imagePullSecrets:
- name: {{ .Values.imagePullSecret }}
terminationGracePeriodSeconds: 60
securityContext:
runAsNonRoot: true
runAsUser: 65532
seccompProfile: {type: RuntimeDefault}
containers:
- name: worker
image: "{{ .Values.image.repository }}@{{ .Values.image.digest }}"
args: [worker]
resources:
requests: {cpu: 250m, memory: 256Mi}
limits: {cpu: '1', memory: 1Gi}
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities: {drop: [ALL]}
envFrom:
- configMapRef: {name: messenger-config}
env:
- {name: GOMEMLIMIT, value: 768MiB}
- {name: BROKER_ADDRESS, value: 'broker:6379'}
- {name: S3_ENDPOINT, value: 's3:9000'}
- {name: S3_TLS, value: 'false'}
- name: S3_BUCKET
value: {{ .Values.s3.bucket }}
- name: DATABASE_URL
valueFrom:
secretKeyRef: {name: {{ .Values.databaseSecret }}, key: database-url}
- name: CURSOR_SIGNING_KEY
valueFrom:
secretKeyRef: {name: {{ .Values.databaseSecret }}, key: cursor-key}
- name: S3_ACCESS_KEY
valueFrom:
secretKeyRef: {name: {{ .Values.s3.secret }}, key: access-key}
- name: S3_SECRET_KEY
valueFrom:
secretKeyRef: {name: {{ .Values.s3.secret }}, key: secret-key}
---
apiVersion: v1
kind: Service
metadata:
name: broker
spec:
selector: {app: broker}
ports:
- {name: streams, port: 6379}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: broker
spec:
serviceName: broker
replicas: 1
selector:
matchLabels: {app: broker}
template:
metadata:
labels: {app: broker}
spec:
containers:
- name: broker
image: {{ .Values.valkey.image }}
args: [valkey-server, --appendonly, 'yes', --appendfsync, always, --maxmemory, 128mb, --maxmemory-policy, noeviction]
resources:
requests: {cpu: 100m, memory: 160Mi}
limits: {cpu: '1', memory: 256Mi}
ports:
- {containerPort: 6379}
readinessProbe:
exec: {command: [valkey-cli, ping]}
volumeMounts:
- {name: data, mountPath: /data}
volumeClaimTemplates:
- metadata: {name: data}
spec:
accessModes: [ReadWriteOnce]
{{- if .Values.storageClass }}
storageClassName: {{ .Values.storageClass | quote }}
{{- end }}
resources:
requests: {storage: 1Gi}
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: private-broker
spec:
podSelector:
matchLabels: {app: broker}
policyTypes: [Ingress]
ingress:
- from:
- podSelector:
matchLabels: {app: worker}
ports:
- {protocol: TCP, port: 6379}
+9
View File
@@ -0,0 +1,9 @@
{
"$schema": "https://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"valkey": {"type": "object", "properties": {"shards": {"type": "integer", "minimum": 2, "maximum": 3}}},
"api": {"type": "object", "properties": {"replicas": {"type": "integer", "minimum": 0}}},
"image": {"type": "object", "properties": {"digest": {"type": "string", "pattern": "^sha256:[a-f0-9]{64}$"}}}
}
}
+67
View File
@@ -0,0 +1,67 @@
# Existing lab2 release; shared persistent stores and credentials.
image:
repository: asisti.registry.twcstorage.ru/mtusi2026lab5
digest: sha256:8da7c54c754acda38c8c2eca6b0c901515fb58f14bf5af9efed939f71ac81d6f
imagePullSecret: registry-pull
databaseSecret: messenger-secrets
api:
readiness:
timeoutSeconds: 2
failureThreshold: 2
replicas: 2
resources:
requests: {cpu: 250m, memory: 128Mi}
limits: {cpu: '1', memory: 768Mi}
env:
SESSION_TTL_SECONDS: '3600'
DEPENDENCY_TIMEOUT_MS: '2000'
DEPENDENCY_POOL_SIZE: '8'
REQUEST_TIMEOUT_SECONDS: '4'
SHUTDOWN_TIMEOUT_SECONDS: '5'
GOMEMLIMIT: 640MiB
MAX_CONNECTIONS: '16'
MAX_BODY_BYTES: '10485760'
postgres:
image: postgres:17.6-alpine
storage: 2Gi
resources:
requests: {cpu: 250m, memory: 256Mi}
limits: {cpu: '1', memory: 512Mi}
valkey:
image: valkey/valkey:8.1.3-alpine
shards: 3
storage: 1Gi
maxmemory: 64mb
resources:
requests: {cpu: 100m, memory: 96Mi}
limits: {cpu: 500m, memory: 128Mi}
# Пустое значение использует default StorageClass. Задать до первой установки.
storageClass: ''
autoscaling:
enabled: true
minReplicas: 2
maxReplicas: 4
cpuPercent: 60
monitoring:
serviceMonitor: false
load:
enabled: false
run: manual
script: scaling.js
duration: 30
vus: 32
ingress:
host: hl.agapov.space
className: traefik
clusterIssuer: letsencrypt-prod
s3:
image: chrislusf/seaweedfs:3.85@sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a
secret: messenger-s3
bucket: attachments
storage: 5Gi
worker:
replicas: 1