Kubernetes Service 在大规模场景下的优化实战 杜军 (Github: @m1093782566) 华为云高级工程师 - PaaS 开源组 Kubernetes upstream, feature maintainer
Kubernetes 的 Service 机制 Iptables 实现 Service 负载均衡 目录 CONTENTS 当前 Iptables 实现存在的问题 IPVS 实现 Service 负载均衡 Iptables VS. IPVS IPSet 与 IPVS 协同
- 多个后端实例, 如何做到负载均衡? - 如何保持会话亲和性? - 容器迁移,IP 发生变化如何访问? - 健康检查怎么做? - 怎么通过域名访问? Kubernetes 的 Service 但, 简单的生活总是暂时的 :
Kubernetes Service 与 Endpoints
Service 与 Endpoints 定义
Service 内部逻辑
Kubernetes 的 Service 机制 Iptables 实现 Service 负载均衡 目录 CONTENTS 当前 Iptables 实现存在的问题 IPVS 实现 Service 负载均衡 Iptables VS. IPVS IPSet 与 IPVS 协同
Iptables 是什么? 用户空间应用程序, 通过配置 Netfilter 规则表 ( Xtables ) 来构建 linux 内核防火墙
网络包通过 Netfilter 全过程
Iptables 实现流量转发与负载均衡 Iptables 如何做流量转发? DNAT 实现 IP 地址和端口映射 iptables -t nat -A PREROUTING -d 1.2.3.4/32 -- dport 80 -j DNAT --to-destination 10.20.30.40:8080 Iptables 如何做负载均衡? statistic 模块为每个后端设置权重 iptables -t nat -A PREROUTING -d 1.2.3.4 --dport 80 -m statistic --mode random --probability.25 -j DNAT --to-destination 10.20.30.40:8080 Iptables 如何做会话保持? recent 模块设置会话保持时间 iptables -t nat A FOO -m recent --rcheck -- seconds 3600 --reap -- name BAR -j BAR
Iptables 在 Kubernetes 的应用举例 VIP:Port -> PREROUTING(OUTPUT) -> KUBE-SERVICES -> KUBE-SVC-XXX -> KUBE-SEP-XXX -> RIP:Port 1 1 2 3 4 2 3 4
Kubernetes 的 Service 机制 Iptables 实现 Service 负载均衡 目录 CONTENTS 当前 Iptables 实现存在的问题 IPVS 实现 Service 负载均衡 Iptables VS. IPVS IPSet 与 IPVS 协同
Iptables 做负载均衡的问题 规则线性匹配时延 规则更新时延 可扩展性 可用性 KUBE- SERVICES 链挂了一长串 KUBE- SVC-* 链 ; 访问每个 service, 要遍历每条链直到匹配, 时间复杂度 O(N) 非增量式 当系统存在大量 iptables 规则链时, 增加 / 删除规则会出现 kernel lock Another app is currently holding the xtables lock. Perhaps you want to use the -w option? 后端实例扩容, 服务会话保持时间更新等都会导致连接断开
Iptables 规则匹配时延 注 : 上面测试中, 每个 service 在 kube-services 对应 1 条 chain
更新 Iptables 规则的时延 时延出现在哪? 非增量式, 即使加上 no-flush(iptables-restore) 选项 Kube-proxy 定期同步 iptables 状态 : ü ü ü ü 拷贝所有规则 iptables-save 在内存中更新规则在内核中修改规则 iptables-restore 规则更新期间存在 kernel lock K service(40k 规则 ), 增加一条 iptables 规则, 耗时 11min 20K service(160k 规则 ), 增加一条 iptables 规则, 耗时 5h
Iptables 周期性刷新导致 TPS 抖动
K8S Scalability 5000 Nodes 1000 Services??
优化方案 使用树形结构组织 iptables 规则 IPVS
树形结构的 iptables 规则
Kubernetes 的 Service 机制 Iptables 实现 Service 负载均衡 目录 CONTENTS 当前 Iptables 实现存在的问题 IPVS 实现 Service 负载均衡 Iptables VS. IPVS IPSet 与 IPVS 协同
什么是 IPVS(IP Virtual Server) Linux 内核实现的 L4 LB,LVS 负载均衡的实现 基于 netfilter, hash table 支持 TCP, UDP,SCTP 协议,IPV4,IPV6 支持多种负载均衡策略 rr, wrr, lc, wlc, sh, dh, lblc 支持会话保持 persistent connection 调度算法
IPVS 工作流
IPVS 三种转发模式 支持三种 LB 模式 : Direct Routing(DR), Tunneling, NAT DR 模式工作在 L2, 最快, 但不支持端口映射 Tunneling 模式用 IP 包封装 IP 包, 不支持端口映射 DR 和 Tunneling 模式, 回程报文不会经过 IPVS Director NAT 模式支持端口映射, 回程报文经过 IPVS Director - 内核原生版本只做 DNAT, 不做 SNAT
DR
Tunneling
NAT
IPVS 做流量转发 绑定 VIP ü dummy 网卡 # ip link add dev dummy0 type dummy # ip addr add 192.168.2.2/32 dev dummy0 ü 本地路由表 # ip route add to local 192.168.2.2/32 dev eth0 proto kernel ü 网卡别名 # ifconfig eth0:1 192.168.2.2 netmask 255.255.255.255 up IPVS Virtual Server # ipvsadm -A -t 192.168.60.200:80 -s rr -p 600 IPVS Real Server # ipvsadm -a -t 192.168.60.200:80 -r 172.17.1.2:80 m # ipvsadm -a -t 192.168.60.200:80 -r 172.17.2.3:80 m
IPVS 实现 Kubernetes Service
Kubernetes 支持 IPVS 模式 社区 1.8 Alpha 特性, Owner @m1093782566 社区 1.9 进 beta, Owner @m1093782566 支持 ClusterIP,NodePort,External IP,Load Balancer 类型 Service iptables 模式的特性,IPVS 模式都支持! 兼容 Network Policy 依赖 iptables 做 SNAT 和访问控制
Kubernetes 的 Service 机制 Iptables 实现 Service 负载均衡 目录 CONTENTS 当前 Iptables 实现存在的问题 IPVS 实现 Service 负载均衡 Iptables VS. IPVS IPSet 与 IPVS 协同
Iptables vs. IPVS 增加规则时延 Service 基数 1 5000 20000 Rules 基数 8 40000 160000 增加 1 条 Iptables 规则 50 us 11 min 5 hours 增加 1 条 IPVS 规则 30 us 50 us 70 us 增加一条 Iptables 的时延, 随着规则数的增加 指数 上升 增加一条 IPVS 的时延, 规则基数对其几乎没影响
使用 iperf 测量 每个 Service 暴露 4 个端口 (KUBE-SERVICES 下挂 4 条 KUBE-SVC-*) service 数 1 1000 5000 10000 25000 50000 带宽,iptables,first 66.6 64 50 15 0 0 带宽,iptables,last 66.6 56 38.6 6 0 0 带宽,IPVS,first 65.3 61.7 53.5 43 30 24 带宽,IPVS,last 65.3 55.3 53.8 43.5 28.5 23.8
Iptables vs. IPVS CPU/ 内存消耗 Metrics number of service IPVS Iptables Memory Usage 1000 386 MB 1.1 G 5000 N/A 1.9 G 10000 542 MB 2.3 G 15000 N/A OOM 50000 1272 MB OOM CPU Usage 1000 N/A 5000 50% - 85% 10000 0% 50%-100% 15000 N/A 50000 N/A
Iptables vs. IPVS Iptables ü 灵活, 功能强大 ü 在 prerouting, postrouting, forward, input, output 不同阶段都能对包进行操作 IPVS ü 更好的性能 (hash vs. chain) ü 更多的负载均衡算法 - rr, wrr, lc, wlc, ip hash ü 连接保持 - IPVS service 更新期间, 保持连接不断开 ü 预先加载内核模 - nf_conntrack_ipv4, ip_vs, ip_vs_rr, ip_vs_wrr, ipvs_sh ü# echo 1 > /proc/sys/net/ipv4/vs/conntrack ü 不支持 127.0.0.1 作为 VIP
Why we still need iptables? 自己访问自己
从 Host 发起跨节点访问
Iptables: Masquerade
但, 不想要太多 iptables
Kubernetes 的 Service 机制 Iptables 实现 Service 负载均衡 目录 CONTENTS 当前 Iptables 实现存在的问题 IPVS 实现 Service 负载均衡 Iptables VS. IPVS IPSet 与 IPVS 协同
IPSet - 把 O(N) 的 iptables 规则降为 O(1) pset create KUBE-LOOP-BACK hash:ip,port,ip ipset add KUBE-LOOP-BACK 192.168.1.1,udp:53,192.168.1.1 ipset add KUBE-LOOP-BACK 192.168.1.2,tcp:80,192.168.1.2 O(N) iptables -t nat -A POSTROUTING -m set --match-set KUBE-LOOP- BACK dst,dst,src -j MASQUERADEOUTING O(1) 而且 : ipset 支持 增量 式增 / 删 / 改, 而非 iptables 式全量更新
欢迎关注, 华为云!
感谢聆听 Thanks!