1. 程式人生 > >負載均衡,LVS

負載均衡,LVS

lvm

負載均衡介紹

技術分享圖片

LVS介紹

技術分享圖片

有三種模式
1.NAT模式,訪問量很大的時候分發器就成為瓶頸
技術分享圖片
2.IP Tunnel模式:

技術分享圖片

3.DR模式

技術分享圖片

LVS的調度算法

技術分享圖片

一共八種,前面四種比較常用,後面用的比較少

LVS NAT模式搭建

技術分享圖片

1.設置IP地址
分發器:
內網:188.130 外網:252.147(僅主機模式)

rs1

內網:188.129 設置網關 188.130

rs2

內網:188.133 設置網關 188.130

2.關閉防火墻:三臺都完成

[root@weixing01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination      

3.然後三臺機器開啟iptables規則,並且調用空規則:

[root@weixing-03 ~]# systemctl start iptables.service 
[root@weixing-03 ~]# iptables -F
[root@weixing-03 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  確定  ]

技術分享圖片

4.安裝工具:只在dir上

[root@weixing01 ~]# yum install -y ipvsadm
已加載插件:fastestmirror
base                                                                          | 3.6 kB  00:00:00   

5.編輯腳本:在dir上

[root@weixing01 ~]# vim /usr/local/sbin/lvs_nat.sh
#! /bin/bash
# director 服務器上開啟路由轉發功能
echo 1 > /proc/sys/net/ipv4/ip_forward
# 關閉icmp的重定向
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
# 註意區分網卡名字,阿銘的兩個網卡分別為ens33和ens37
echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/ens37/send_redirects
# director 設置nat防火墻
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.188.0/24  -j MASQUERADE
# director設置ipvsadm
IPVSADM=‘/usr/sbin/ipvsadm‘
$IPVSADM -C
$IPVSADM -A -t 192.168.252.147:80 -s lc -p 3
$IPVSADM -a -t 192.168.252.147:80 -r 192.168.188.129:80 -m -w 1
$IPVSADM -a -t 192.168.252.147:80 -r 192.168.188.133:80 -m -w 1

6.編輯兩個rs上的默認頁,做區分:

[root@weixing-02 ~]# vi /usr/share/nginx/html/index.html 
[root@weixing-02 ~]# curl localhost
weixing02
[root@weixing-03 ~]# curl localhost
weixing03

7.編輯配置文件:

$IPVSADM -A -t 192.168.252.147:80 -s rr              更改規則為rr
$IPVSADM -a -t 192.168.252.147:80 -r 192.168.188.129:80 -m -w 1
$IPVSADM -a -t 192.168.252.147:80 -r 192.168.188.133:80 -m -w 1

8.進行測試:

[root@weixing01 ~]# curl 192.168.252.147
weixing03
[root@weixing01 ~]# curl 192.168.252.147
weixing02
[root@weixing01 ~]# curl 192.168.252.147
weixing03
[root@weixing01 ~]# curl 192.168.252.147
weixing02
[root@weixing01 ~]# curl 192.168.252.147
weixing03
[root@weixing01 ~]# curl 192.168.252.147
weixing02

負載均衡,LVS