From 7a3d0b0a55598ae0222e5e29e7705d1e098296cc Mon Sep 17 00:00:00 2001 From: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Date: Tue, 20 Oct 2026 21:48:51 +0300 Subject: [PATCH] fix(tcp-balancer): cap HAProxy fds and threads by the pod's own limits Without a global section HAProxy 3.4 sizes itself from the node. maxconn comes from the nofile limit, cut to the built-in fd-hard-limit of 1048576, so about 524k, and the thread count is the number of CPUs the process may run on, whatever the CPU limit says. With one thread that alone idles at about 85 MiB, and every thread adds about 0.6 MiB, so a t1.nano replica with its 128Mi limit runs on a small node and is OOMKilled at startup on a node with many CPUs. fd-hard-limit is now 32 per MiB of the memory limit, from which HAProxy derives about 16 connections per MiB, 2035 for the t1.nano defaults. That leaves 64 KiB per connection against the roughly 35 KiB HAProxy budgets per connection itself when it is given a memory limit with -m, and the rest covers the process and the kernel socket buffers charged to the same cgroup. It caps the derived value instead of setting maxconn, because an explicit maxconn above half the hard nofile limit stops HAProxy at startup (strict-limits is on by default), while fd-hard-limit still follows a lower system limit. thread-hard-limit is the CPU limit rounded up, at least 1. It only caps the detected thread count, so a node with fewer CPUs is not forced to run more threads than it has. Assisted-by: LLM Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> (cherry picked from commit 20545d00a59f3be80a96f7457cab934a9f69a391) Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com> --- .../tcp-balancer/templates/configmap.yaml | 7 ++++ .../tests/haproxy-config_test.yaml | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/packages/apps/tcp-balancer/templates/configmap.yaml b/packages/apps/tcp-balancer/templates/configmap.yaml index 122c10254..327b8200e 100644 --- a/packages/apps/tcp-balancer/templates/configmap.yaml +++ b/packages/apps/tcp-balancer/templates/configmap.yaml @@ -11,6 +11,9 @@ default-server observe layer4 error-limit 10 on-error mark-down check send-proxy {{- fail (printf "mode %s is not supported" .) }} {{- end }} {{- end }} +{{- $resources := include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | fromYaml }} +{{- $memoryMi := div (include "cozy-lib.resources.toFloat" $resources.limits.memory | float64 | int64) 1048576 }} +{{- $cpu := include "cozy-lib.resources.toFloat" $resources.limits.cpu | float64 }} apiVersion: v1 kind: ConfigMap @@ -21,6 +24,10 @@ metadata: app.kubernetes.io/managed-by: {{ .Release.Service }} data: haproxy.cfg: | + global + fd-hard-limit {{ mul $memoryMi 32 }} + thread-hard-limit {{ max (ceil $cpu | int64) 1 }} + defaults mode tcp option dontlognull diff --git a/packages/apps/tcp-balancer/tests/haproxy-config_test.yaml b/packages/apps/tcp-balancer/tests/haproxy-config_test.yaml index 4c8844583..d1abbee44 100644 --- a/packages/apps/tcp-balancer/tests/haproxy-config_test.yaml +++ b/packages/apps/tcp-balancer/tests/haproxy-config_test.yaml @@ -130,3 +130,39 @@ tests: - matchRegex: path: data["haproxy.cfg"] pattern: '(?m)^frontend talos$\n(?:[ \t]+.*\n)*?[ \t]+acl whitelist src 10\.0\.0\.0/8\n[ \t]+acl all src 0\.0\.0\.0\n[ \t]+tcp-request content accept if whitelist\n[ \t]+tcp-request content reject$' + + - it: caps the automatic fd and thread counts by the default preset + asserts: + - matchRegex: + path: data["haproxy.cfg"] + pattern: '\Aglobal\n[ \t]+fd-hard-limit 4096\n[ \t]+thread-hard-limit 1\n\n' + - notMatchRegex: + path: data["haproxy.cfg"] + pattern: '\Aglobal\n(?:[ \t]+.*\n)*?[ \t]+(?:maxconn|nbthread)\b' + + - it: scales the caps with the preset + set: + resourcesPreset: t1.medium + asserts: + - matchRegex: + path: data["haproxy.cfg"] + pattern: '\Aglobal\n[ \t]+fd-hard-limit 32768\n[ \t]+thread-hard-limit 2\n\n' + + - it: takes the caps from explicit resources and rounds the CPU limit up + set: + resources: + cpu: 2500m + memory: 768Mi + asserts: + - matchRegex: + path: data["haproxy.cfg"] + pattern: '\Aglobal\n[ \t]+fd-hard-limit 24576\n[ \t]+thread-hard-limit 3\n\n' + + - it: never renders a thread cap of zero, which HAProxy refuses to parse + set: + resources: + cpu: "0" + asserts: + - matchRegex: + path: data["haproxy.cfg"] + pattern: '(?m)^[ \t]+thread-hard-limit 1$'