天天看点

戏说:负载均衡

lvs

Linux Virtual Server (lvs) 是Linux内核自带的负载均衡器,也是目前性能最好的软件负载均衡器之一。

<a href="https://s4.51cto.com/wyfs02/M02/9E/37/wKioL1mNeePAxZ4UAAG8sOx2HDg604.jpg-wh_500x0-wm_3-wmp_4-s_2556432551.jpg" target="_blank"></a>

轮叫调度(Round-Robin Scheduling)

加权轮叫调度(Weighted Round-Robin Scheduling)

最小连接调度(Least-Connection Scheduling)

加权最小连接调度(Weighted Least-Connection Scheduling)

基于局部性的最少链接(Locality-Based Least Connections Scheduling)

带复制的基于局部性最少链接(Locality-Based Least Connections with Replication Scheduling)

目标地址散列调度(Destination Hashing Scheduling)

源地址散列调度(Source Hashing Scheduling)

最短预期延时调度(Shortest Expected Delay Scheduling)

不排队调度(Never Queue Scheduling)

安装ipvs包并开启ip转发

<code>yum </code><code>-</code><code>y install ipvsadm keepalived</code>

<code>sysctl </code><code>-</code><code>w net.ipv4.ip_forward</code><code>=</code><code>1</code>

修改/etc/keepalived/keepalived.conf,增加vip和lvs的配置

<code>vrrp_instance VI_3 {</code>

<code>    </code><code>state MASTER   </code><code># 另一节点为BACKUP</code>

<code>    </code><code>interface eth0</code>

<code>    </code><code>virtual_router_id </code><code>11</code>

<code>    </code><code>priority </code><code>100</code>   <code># 另一节点为50</code>

<code>    </code><code>advert_int </code><code>1</code>

<code>    </code><code>authentication {</code>

<code>        </code><code>auth_type PASS</code>

<code>        </code><code>auth_pass PASSWORD</code>

<code>    </code><code>}</code>

<code>    </code><code>track_script {</code>

<code>        </code><code>chk_http_port</code>

<code>    </code><code>virtual_ipaddress {        </code><code>192.168</code><code>.</code><code>0.100</code>

<code>}</code>

<code>virtual_server </code><code>192.168</code><code>.</code><code>0.100</code> <code>9696</code> <code>{</code>

<code>    </code><code>delay_loop </code><code>30</code>

<code>    </code><code>lb_algo rr</code>

<code>    </code><code>lb_kind DR</code>

<code>    </code><code>persistence_timeout </code><code>30</code>

<code>    </code><code>protocol TCP</code>

<code>    </code><code>real_server </code><code>192.168</code><code>.</code><code>0.101</code> <code>9696</code> <code>{</code>

<code>        </code><code>weight </code><code>3</code>

<code>        </code><code>TCP_CHECK {</code>

<code>            </code><code>connect_timeout </code><code>10</code>

<code>            </code><code>nb_get_retry </code><code>3</code>

<code>            </code><code>delay_before_retry </code><code>3</code>

<code>            </code><code>connect_port </code><code>9696</code>

<code>        </code><code>}</code>

<code>    </code><code>real_server </code><code>192.168</code><code>.</code><code>0.102</code> <code>9696</code> <code>{</code>

重启keepalived:

<code>systemctl </code><code>reload</code> <code>keepalived</code>

最后在neutron-server所在机器上为lo配置vip,并抑制ARP响应:

<code>vip</code><code>=</code><code>192.168</code><code>.</code><code>0.100</code>

<code>ifconfig lo:</code><code>1</code> <code>${vip} broadcast ${vip} netmask </code><code>255.255</code><code>.</code><code>255.255</code>

<code>route add </code><code>-</code><code>host ${vip} dev lo:</code><code>1</code>

<code>echo </code><code>"1"</code> <code>&gt;</code><code>/</code><code>proc</code><code>/</code><code>sys</code><code>/</code><code>net</code><code>/</code><code>ipv4</code><code>/</code><code>conf</code><code>/</code><code>lo</code><code>/</code><code>arp_ignore</code>

<code>echo </code><code>"2"</code> <code>&gt;</code><code>/</code><code>proc</code><code>/</code><code>sys</code><code>/</code><code>net</code><code>/</code><code>ipv4</code><code>/</code><code>conf</code><code>/</code><code>lo</code><code>/</code><code>arp_announce</code>

<code>echo </code><code>"1"</code> <code>&gt;</code><code>/</code><code>proc</code><code>/</code><code>sys</code><code>/</code><code>net</code><code>/</code><code>ipv4</code><code>/</code><code>conf</code><code>/</code><code>all</code><code>/</code><code>arp_ignore</code>

Keepalived主备模式设备利用率低;不能横向扩展;VRRP协议,有脑裂的风险。

ECMP的方式需要了解动态路由协议,LVS和交换机均需要较复杂配置;交换机的HASH算法一般比较简单,增加删除节点会造成HASH重分布,可能导致当前TCP连接全部中断;部分交换机的ECMP在处理分片包时会有BUG。

Haproxy

Haproxy也是Linux最常用的负载均衡软件之一,兼具性能和功能的组合,同时支持TCP和HTTP负载均衡。

配置和使用方法请见https://nginx.org/en/。

Nginx

Nginx也是Linux最常用的负载均衡软件之一,常用作反向代理和HTTP负载均衡(当然也支持TCP和UDP负载均衡)。

自研负载均衡

Google Maglev

Maglev是Google自研的负载均衡方案,在2008年就已经开始用于生产环境。Maglev安装后不需要预热5秒内就能处理每秒100万次请求。谷歌的性能基准测试中,Maglev实例运行在一个8核CPU下,网络吞吐率上限为12M PPS(数据包每秒)。如果Maglev使用Linux内核网络堆栈则速度会慢下来,吞吐率小于4M PPS。

<a href="https://s4.51cto.com/wyfs02/M00/9E/38/wKioL1mNepXSTFJyAAFBPVV9JEk782.jpg-wh_500x0-wm_3-wmp_4-s_3924501455.jpg" target="_blank"></a>

路由器ECMP (Equal Cost Multipath) 转发包到Maglev(而不是传统的主从结构)

Kernel Bypass, CPU绑定,共享内存

一致性哈希保证连接不中断

Vortex参考了Maglev,大致的架构和实现跟Maglev类似:

ECMP实现集群的负载均衡

一致性哈希保证连接不中断即使是不同的Vortex服务器收到了数据包,仍然能够将该数据包转发到同一台后端服务器后端服务器变化时,通过连接追踪机制保证当前活动连接的数据包被送往之前选择的服务器,而所有新建连接则会在变化后的服务器集群中进行负载分担

DPDK提升单机性能 (14M PPS,10G, 64字节线速)通过RSS直接将网卡队列和CPU Core绑定,消除线程的上下文切换带来的开销

Vortex线程间采用高并发无锁的消息队列通信

DR模式避免额外开销

总结

本文为大家详细讲解了关于Linux配置中的负载均衡的装置,适应现代社会的需求,

作为运维工程师的你,学会了吗?

本文转自 xjtuhit 51CTO博客,原文链接:http://blog.51cto.com/51reboot/1955535

继续阅读