feat: add bounded observability and verified SLO incident alerts

This commit is contained in:
Vladislav Agapov
2026-10-29 18:01:11 +03:00
parent 470d9d6f1e
commit 6c7601802d
325 changed files with 24988 additions and 44 deletions
+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 -}}
+140
View File
@@ -0,0 +1,140 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: messenger-config
namespace: {{ .Release.Namespace }}
data:
APP_PORT: '8080'
{{- if .Values.observability.enabled }}
OTEL_EXPORTER_OTLP_ENDPOINT: http://messenger-collector:4318
OTEL_SAMPLE_RATIO: '1'
APP_VERSION: lab6
{{- end }}
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
@@ -0,0 +1,124 @@
{{- if .Values.observability.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
name: messenger-observability
labels: {app: messenger-observability}
data:
{{- range $path, $_ := .Files.Glob "files/observability/*" }}
{{ base $path }}: |
{{ $.Files.Get $path | indent 4 }}
{{- end }}
{{- $components := list
(dict "name" "collector" "image" .Values.observability.collectorImage "port" 4318 "args" (list "--config=/config/collector.yaml") "memory" "256Mi" "uid" 10001)
(dict "name" "prometheus" "image" .Values.observability.prometheusImage "port" 9090 "args" (list "--config.file=/config/prometheus.yaml" "--storage.tsdb.path=/data" "--storage.tsdb.retention.time=24h" "--storage.tsdb.retention.size=1GB") "memory" "384Mi" "uid" 65534)
(dict "name" "loki" "image" .Values.observability.lokiImage "port" 3100 "args" (list "-config.file=/config/loki.yaml") "memory" "384Mi" "uid" 10001)
(dict "name" "tempo" "image" .Values.observability.tempoImage "port" 3200 "args" (list "-config.file=/config/tempo.yaml") "memory" "384Mi" "uid" 10001)
(dict "name" "grafana" "image" .Values.observability.grafanaImage "port" 3000 "args" list "memory" "256Mi" "uid" 472)
}}
{{- range $component := $components }}
---
apiVersion: v1
kind: Service
metadata:
name: messenger-{{ $component.name }}
spec:
selector: {app: 'messenger-{{ $component.name }}'}
ports:
- {name: http, port: {{ $component.port }}, targetPort: {{ $component.port }}}
{{- if eq $component.name "collector" }}
- {name: metrics, port: 8889, targetPort: 8889}
- {name: self-metrics, port: 8888, targetPort: 8888}
{{- end }}
{{- if eq $component.name "tempo" }}
- {name: otlp, port: 4318, targetPort: 4318}
{{- end }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: messenger-{{ $component.name }}
spec:
serviceName: messenger-{{ $component.name }}
replicas: 1
selector:
matchLabels: {app: 'messenger-{{ $component.name }}'}
template:
metadata:
labels: {app: 'messenger-{{ $component.name }}'}
annotations:
checksum/config: {{ ($.Files.Glob "files/observability/*").AsConfig | sha256sum }}
spec:
securityContext:
runAsNonRoot: true
runAsUser: {{ $component.uid }}
fsGroup: {{ $component.uid }}
containers:
- name: {{ $component.name }}
image: {{ $component.image }}
args: {{ $component.args | toJson }}
ports:
- {containerPort: {{ $component.port }}}
resources:
requests: {cpu: 50m, memory: 96Mi}
limits: {cpu: '1', memory: {{ $component.memory }}}
readinessProbe:
tcpSocket: {port: {{ $component.port }}}
{{- if eq $component.name "grafana" }}
env:
- {name: GF_PATHS_DATA, value: /data}
- {name: GF_AUTH_ANONYMOUS_ENABLED, value: 'false'}
- {name: GF_USERS_ALLOW_SIGN_UP, value: 'false'}
- {name: GF_ANALYTICS_REPORTING_ENABLED, value: 'false'}
- name: GF_SECURITY_ADMIN_USER
valueFrom:
secretKeyRef: {name: {{ $.Values.observability.secret }}, key: username}
- name: GF_SECURITY_ADMIN_PASSWORD
valueFrom:
secretKeyRef: {name: {{ $.Values.observability.secret }}, key: password}
{{- end }}
volumeMounts:
- {name: data, mountPath: /data}
- {name: config, mountPath: /config, readOnly: true}
{{- if eq $component.name "grafana" }}
- {name: config, mountPath: /etc/grafana/provisioning/datasources/datasources.yaml, subPath: datasources.yaml, readOnly: true}
- {name: config, mountPath: /etc/grafana/provisioning/dashboards/dashboards.yaml, subPath: dashboards.yaml, readOnly: true}
- {name: config, mountPath: /dashboards/messenger.json, subPath: messenger.json, readOnly: true}
{{- end }}
volumes:
- name: config
configMap: {name: messenger-observability}
volumeClaimTemplates:
- metadata: {name: data}
spec:
accessModes: [ReadWriteOnce]
{{- if $.Values.storageClass }}
storageClassName: {{ $.Values.storageClass | quote }}
{{- end }}
resources:
requests: {storage: 2Gi}
{{- end }}
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: private-observability
spec:
podSelector:
matchExpressions:
- {key: app, operator: In, values: [messenger-collector, messenger-prometheus, messenger-loki, messenger-tempo, messenger-grafana]}
policyTypes: [Ingress]
ingress:
- from:
- podSelector:
matchExpressions:
- {key: app, operator: In, values: [api, worker, messenger-collector, messenger-prometheus, messenger-loki, messenger-tempo, messenger-grafana]}
ports:
- {protocol: TCP, port: 4318}
- {protocol: TCP, port: 8888}
- {protocol: TCP, port: 8889}
- {protocol: TCP, port: 9090}
- {protocol: TCP, port: 3100}
- {protocol: TCP, port: 3200}
- {protocol: TCP, port: 3000}
{{- end }}
+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=32]
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}