个人技术分享

1 sysctl参数

1.1 参数调优实践

# Kubernetes Settings
vm.max_map_count = 262144
kernel.softlockup_panic = 1
kernel.softlockup_all_cpu_backtrace = 1
net.ipv4.ip_local_reserved_ports = 30000-32767

# Increase the number of connections
net.core.somaxconn = 32768

# Maximum Socket Receive Buffer
net.core.rmem_max = 16777216

# Maximum Socket Send Buffer
net.core.wmem_max = 16777216

# Increase the maximum total buffer-space allocatable
net.ipv4.tcp_wmem = 4096 87380 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216

# Increase the number of outstanding syn requests allowed
net.ipv4.tcp_max_syn_backlog = 8096


# For persistent HTTP connections
net.ipv4.tcp_slow_start_after_idle = 0

# Allow to reuse TIME_WAIT sockets for new connections
# when it is safe from protocol viewpoint
net.ipv4.tcp_tw_reuse = 1

# Max number of packets that can be queued on interface input
# If kernel is receiving packets faster than can be processed
# this queue increases
net.core.netdev_max_backlog = 16384

# Increase size of file handles and inode cache
fs.file-max = 2097152

# Max number of inotify instances and watches for a user
# Since dockerd runs as a single user, the default instances value of 128 per user is too low
# e.g. uses of inotify: nginx ingress controller, kubectl logs -f
fs.inotify.max_user_instances = 8192
fs.inotify.max_user_watches = 524288

# Additional sysctl flags that kubelet expects
vm.overcommit_memory = 1
kernel.panic = 10
kernel.panic_on_oops = 1

# Prevent docker from changing iptables: https://github.com/kubernetes/kubernetes/issues/40182
net.ipv4.ip_forward=1

如果是 AWS,额外增加如下:

# AWS settings
# Issue #23395
net.ipv4.neigh.default.gc_thresh1=0

如果启用了 IPv6,额外增加如下:

# Enable IPv6 forwarding for network plugins that don't do it themselves
net.ipv6.conf.all.forwarding=1

1.2 参数解释

分类 内核参数 说明 参考链接
Kubernetes vm.max_map_count = 262144 限制一个进程可以拥有的VMA(虚拟内存区域)的数量,
一个更大的值对于 elasticsearch、mongo 或其他 mmap 用户来说非常有用
ES Configuration
Kubernetes kernel.softlockup_panic = 1 用于解决 K8S 内核软锁相关 bug root cause kernel soft lockups · Issue #37853 · kubernetes/kubernetes (github.com)
Kubernetes kernel.softlockup_all_cpu_backtrace = 1 用于解决 K8S 内核软锁相关 bug root cause kernel soft lockups · Issue #37853 · kubernetes/kubernetes (github.com)
Kubernetes net.ipv4.ip_local_reserved_ports = 30000-32767 默认 K8S Nodport 端口 service-node-port-range and ip_local_port_range collision · Issue #6342 · kubernetes/kops (github.com)
网络 net.core.somaxconn = 32768 表示socket监听(listen)的backlog上限。什么是backlog?backlog就是socket的监听队列,当一个请求(request)尚未被处理或建立时,他会进入backlog。
增加连接数.
Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
网络 net.core.rmem_max = 16777216 接收套接字缓冲区大小的最大值(以字节为单位)。
最大化 Socket Receive Buffer
Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
网络 net.core.wmem_max = 16777216 发送套接字缓冲区大小的最大值(以字节为单位)。
最大化 Socket Send Buffer
Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
网络 net.ipv4.tcp_wmem = 4096 87380 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
增加总的可分配的 buffer 空间的最大值 Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
网络 net.ipv4.tcp_max_syn_backlog = 8096 表示那些尚未收到客户端确认信息的连接(SYN消息)队列的长度,默认为1024
增加未完成的syn请求的数量
Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
网络 net.ipv4.tcp_slow_start_after_idle = 0 持久化 HTTP 连接 Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
网络 net.ipv4.tcp_tw_reuse = 1 表示允许重用TIME_WAIT状态的套接字用于新的TCP连接,默认为0,表示关闭。
允许在协议安全的情况下重用TIME_WAIT 套接字用于新的连接
Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
网络 net.core.netdev_max_backlog = 16384 当网卡接收数据包的速度大于内核处理的速度时,会有一个队列保存这些数据包。这个参数表示该队列的最大值
如果内核接收数据包的速度超过了可以处理的速度,这个队列就会增加
Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
文件系统 fs.file-max = 2097152 该参数决定了系统中所允许的文件句柄最大数目,文件句柄设置代表linux系统中可以打开的文件的数量。
增加文件句柄和inode缓存的大小
Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
文件系统 fs.inotify.max_user_instances = 8192
fs.inotify.max_user_watches = 524288
一个用户的inotify实例和watch的最大数量
由于dockerd作为单个用户运行,每个用户的默认实例值128太低了
例如使用inotify: nginx ingress controller, kubectl logs -f
Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
kubelet vm.overcommit_memory = 1 对内存分配的一种策略
=1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何
Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
kubelet kernel.panic = 10 panic错误中自动重启,等待时间为10秒 Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
kubelet kernel.panic_on_oops = 1 在Oops发生时会进行panic()操作 Image: We should tweak our sysctls · Issue #261 · kubernetes-retired/kube-deploy (github.com)
网络 net.ipv4.ip_forward=1 启用ip转发
另外也防止docker改变iptables
Upgrading docker 1.13 on nodes causes outbound container traffic to stop working · Issue #40182 · kubernetes/kubernetes (github.com)
网络 net.ipv4.neigh.default.gc_thresh1=0 修复 AWS arp_cache: neighbor table overflow! 报错 arp_cache: neighbor table overflow! · Issue #4533 · kubernetes/kops (github.com)

2  K8s Node参数调优

2.1 核心参数

控制可以为 K8S Node 调度的最大 pod 数量的两个参数: 

  • podsPerCore 
  • maxPods

当两个参数都被设置时,其中较小的值限制了节点上的 pod 数量。超过这些值可导致:

  • CPU 使用率增加。
  • 减慢 pod 调度的速度。
  • 根据节点中的内存数量,可能出现内存耗尽的问题。
  • 耗尽 IP 地址池。
  • 资源过量使用,导致用户应用程序性能变差。

在 Kubernetes 中,包含单个容器的 pod 实际使用两个容器。第二个容器用来在实际容器启动前设置联网(pause)。因此,运行 10 个 pod 的系统实际上会运行 20 个容器。

podsPerCore 根据节点中的处理器内核数来设置节点可运行的 pod 数量。例如:在一个有 4 个处理器内核的节点上将 podsPerCore 设为 10 ,则该节点上允许的最大 pod 数量为 40。

kubeletConfig:
  podsPerCore: 10

将 podsPerCore 设置为 0 可禁用这个限制。默认为 0podsPerCore 不能超过 maxPods

maxPods 把节点可以运行的 pod 数量设置为一个固定值,而不需要考虑节点的属性。

 kubeletConfig:
    maxPods: 250

3  K8s ApiServer调优

kube-apiserver 推荐优化的参数如下:

  • --default-watch-cache-size:默认值 100;用于 List-Watch 的缓存池;建议 1000 或更多;
  • --delete-collection-workers:默认值 1;用于提升 namesapce 清理速度,有利于多租户场景;建议 10;
  • --event-ttl: 默认值 1h0m0s;用于控制保留 events 的时长;集群 events 较多时建议 30m,以避免 etcd 增长过快;
  • --max-mutating-requests-inflight: 默认值 200;用于 write 请求的访问频率限制;建议 800 或更高;
  • --max-requests-inflight: 默认值 400;用于 read 请求的访问频率限制;建议 1600 或更高;
  • --watch-cache-sizes: 系统根据环境启发式的设定;用于 pods/nodes/endpoints 等核心资源,其他资源参考 default-watch-cache-size 的设定; K8s v1.19 开始,该参数为动态设定,建议使用该版本。

4 大型集群 CIDR 配置最佳实践

4.1 CIDR 配置

在安装大型集群或将现有的集群扩展到较大规模时,在安装集群设置集群网络 cidr 时,如果集群的节点数超过 500 个,则可能无法使用常用的集群网络 cidr /14(这里假设的是一个 Node 的 hostPrefix 是 /23,那么理论上机器超过 512 台,集群网络 IP 就不够用了)。在这种情况下,必须将其设置为 /12 或 /10,以支持超过 500 个节点的环境。

4.2 详细说明 

前提:Overlay 网络

  1. 假定 1 个 Node 上 hostPrefix 是 /24,那么这台机器理论上有 254 个 pod IP。
    1. 集群网络 cidr 是 /21, 理论上集群最多 8 台机器;
    2. 集群网络 cidr 是 /20, 16 台;
    3. /18, 64 台;
    4. /17, 128 台;
    5. /16, 256 台;
    6. /15, 512 台;
  2. 假定 1 个 Node 上 hostPrefix 是 /23,那么这台机器理论上有 510 个 pod IP。
    1. 集群网络 cidr 是 /20, 理论上集群最多 8 台机器;
    2. 集群网络 cidr 是 /19, 16 台;
    3. /17, 64 台;
    4. /16, 128 台;
    5. /15, 256 台;
    6. /14, 512 台;(出于谨慎,如果集群规模超过 500 台,建议将其设置为 /12 或 /10)。