mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-01 04:35:40 +09:00
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 20545d00a5)
Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
abf6b3fbb7
commit
7a3d0b0a55
2 files changed
+43
No files matched your search
@@ -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
|
||||
|
||||
@@ -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$'
|
||||
Reference in new issue
Block a user