华为-Kubernetes Service在大规模场景下的优化
2020-02-27 185浏览
- 1.Kubernetes Service在大 规模场景下的优化实战 杜军 (Github:@m1093782566) 华为云 高级工程师 - PaaS开源组 Kubernetes upstream, feature maintainer
- 2.Kubernetes的Service机制 Iptables实现Service负载均衡 当前Iptables实现存在的问题 IPVS实现Service负载均衡 Iptables VS. IPVS IPSet与IPVS协同 目录 CONTENTS
- 3.Kubernetes的Service 但,简单的生活总是暂时的: - 多个后端实例,如何做到负载均 衡? - 如何保持会话亲和性? - 容器迁移,IP发生变化如何访问? - 健康检查怎么做? - 怎么通过域名访问?
- 4.Kubernetes Service与Endpoints
- 5.Service与Endpoints定义
- 6.Service 内部逻辑
- 7.Kubernetes的Service机制 Iptables实现Service负载均衡 当前Iptables实现存在的问题 IPVS实现Service负载均衡 Iptables VS. IPVS IPSet与IPVS协同 目录 CONTENTS
- 8.Iptables是什么? 用户空间应用程序,通过配 置Netfilter规则表 ( Xtables )来构建linux 内核防火墙。
- 9.网络包通过Netfilter全过程
- 10.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
- 11.Iptables在Kubernetes的 应用举例VIP:Port-> PREROUTING(OUTPUT) -> KUBE-SERVICES -> KUBE-SVC-XXX -> KUBE-SEP-XXX ->RIP:Port11 22 3 3 4 4
- 12.Kubernetes的Service机制 Iptables实现Service负载均衡 当前Iptables实现存在的问题 IPVS实现Service负载均衡 Iptables VS. IPVS IPSet与IPVS协同 目录 CONTENTS
- 13.Iptables做负载均衡的问题 规则线性匹配 时延 规则更新时延 可扩展性 可用性 KUBESERVICES链挂了 一长串KUBESVC-*链;访问 每个service,要 遍历每条链直到 匹配,时间复杂 度O(N) 非增量式 当系统存在大量 iptables规则链时,增 加/删除规则会出现 kernel lock Another app is currently holding the xtables lock. Perhaps you want to use the -w option? 后端实例扩容, 服务会话保持时 间更新等都会导 致连接断开
- 14.Iptables规则匹配时延 注:上面测试中,每个service在kube-services对应1条chain
- 15.更新Iptables规则的时延 时延出现在哪? Ø 非增量式,即使加上—no-flush(iptables-restore)选项 Ø Kube-proxy定期同步iptables状态: ü 拷贝所有规则 iptables-save ü 在内存中更新规则 ü 在内核中修改规则 iptables-restore ü 规则更新期间存在kernel lock K service(40K 规则),增加一条iptables规则,耗时11min 20K service(160K 规则),增加一条iptables规则,耗时5h
- 16.Iptables周期性刷新导致 TPS抖动
- 17.K8S Scalability ▌5000 Nodes ▌1000 Services ??
- 18.优化方案 ▌使用树形结构组织iptables规则 ▌IPVS
- 19.树形结构的iptables规则
- 20.Kubernetes的Service机制 Iptables实现Service负载均衡 当前Iptables实现存在的问题 IPVS实现Service负载均衡 Iptables VS. IPVS IPSet与IPVS协同 目录 CONTENTS
- 21.什么是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调度算法
- 22.IPVS工作流
- 23.IPVS三种转发模式 支持三种LB模式: Direct Routing(DR), Tunneling, NAT ØDR模式工作在L2,最快,但不支持端口映射 ØTunneling模式用IP包封装IP包,不支持端口映射 ØDR和Tunneling模式,回程报文不会经过IPVS Director ØNAT模式支持端口映射,回程报文经过IPVS Director 内核原生版本只做DNAT,不做SNAT
- 24.DR
- 25.Tunneling
- 26.NAT
- 27.IPVS做流量转发 l 绑定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 ü 网卡别名 # ifconfigeth0:1192.168.2.2 netmask 255.255.255.255 up l IPVS Virtual Server # ipvsadm -A -t 192.168.60.200:80 -s rr -p 600 l 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
- 28.IPVS实现Kubernetes Service
- 29.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和访问控制
- 30.Kubernetes的Service机制 Iptables实现Service负载均衡 当前Iptables实现存在的问题 IPVS实现Service负载均衡 Iptables VS. IPVS IPSet与IPVS协同 目录 CONTENTS
- 31.Iptables vs. IPVS 增加规则时延 Service基数 1 Rules基数 8 增加1条Iptables规则 50 us 增加1条IPVS规则 30 us 5000 40000 11 min 50 us 20000 160000 5 hours 70 us 增加一条Iptables的时延,随着规则数的增加“指数”上升 增加一条IPVS的时延,规则基数对其几乎没影响
- 32.▌使用iperf测量 ▌每个Service暴露4个端口(KUBE-SERVICES下挂4条KUBE-SVC-*) service数 带宽,iptables,first 带宽,iptables,last 带宽,IPVS,first 带宽,IPVS,last 1 66.6 66.6 65.3 65.3 1000 64 56 61.7 55.3 5000 50 38.6 53.5 53.8 10000 15 6 43 43.5 25000 0 0 30 28.5 50000 0 0 24 23.8
- 33.Iptables vs. IPVS CPU/内 存消耗 Metrics Memory Usage number of service 1000 5000 10000 15000 IPVS 386 MB N/A 542 MB N/A CPU Usage 50000 1000 5000 10000 15000 50000 1272 MB 0% Iptables 1.1 G 1.9 G 2.3 G OOM OOM N/A 50% - 85% 50%-100% N/A N/A
- 34.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
- 35.Why we still need iptables? 自己访问自己
- 36.从Host发起跨节点访问
- 37.Iptables:Masquerade
- 38.但,不想要太多iptables…
- 39.Kubernetes的Service机制 Iptables实现Service负载均衡 当前Iptables实现存在的问题 IPVS实现Service负载均衡 Iptables VS. IPVS IPSet与IPVS协同 目录 CONTENTS
- 40.IPSet - 把O(N)的iptables 规则降为O(1) pset create KUBE-LOOP-BACKhash:ip,port,ipipset add KUBE-LOOP-BACK 192.168.1.1,udp:53,192.168.1.1ipset add KUBE-LOOP-BACK 192.168.1.2,tcp:80,192.168.1.2O(N) iptables -t nat -A POSTROUTING -m set --match-set KUBE-LOOP- BACK dst,dst,src -j MASQUERADEOUTING O(1) 而且: ipset支持“增量”式增/删/改,而非iptables式全量更新
- 41.欢迎关注,华为云!
- 42.感谢聆听 Thanks!