本文共 7159 字,大约阅读时间需要 23 分钟。
lvs集群类型中的术语:
VS:Virtual Server,Director Server(DS) Dispatcher(调度器),Load Balancer
RS:Real Server(lvs), upstream server(nginx) backend server(haproxy)CIP:Client IPVIP: Virtual serve IP VS外网的IPDIP: Director IP VS内网的IPRIP: Real server IP 访问流程:CIP <--> VIP == DIP <--> RIP
lvs集群的类型: lvs-nat:修改请求报文的目标IP,多目标IP的DNAT
lvs-dr:操纵封装新的MAC地址lvs-tun:在原请求IP报文之外新加一个IP首部lvs-fullnat:修改请求报文的源和目标IP本质是多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某挑出的RS的RIP和PORT实现转发
(1)RIP和DIP应在同一个IP网络,且应使用私网地址;RS的网关要指向DIP(2)请求报文和响应报文都必须经由Director转发,Director易于成为系统瓶颈(3)支持端口映射,可修改请求报文的目标PORT(4)VS必须是Linux系统,RS可以是任意OS系统主机 | ip1 | ip2 |
---|---|---|
Client | 172.22.50.100 | |
LVS | 172.22.50.200 | 192.168.64.120 |
Server1 | 192.168.64.130 | gateway 192.168.64.120 webserver |
Server2 | 192.168.64.140 | gateway 192.168.64.120 webserver |
[root@localhost ~]#hostnamectl set-hostname client[root@localhost ~]#hostnamectl set-hostname LVS[root@localhost ~]#hostnamectl set-hostname server1[root@localhost ~]#hostnamectl set-hostname server2[root@server1 ~]# echo server1 > /var/www/html/index.html[root@server2 ~]# echo server2 > /var/www/html/index.html[root@server1 ~]#systemctl start httpd.service[root@server2 ~]#systemctl start httpd.service[root@lvs ~]# yum install ipvsadm[root@lvs ~]#ipvsadm -A -t 172.22.50.200:80[root@lvs ~]#ipvsadm -a -t 172.22.50.200:80 -r 192.168.64.130 -m -w 3[root@lvs ~]#ipvsadm -a -t 172.22.50.200:80 -r 192.168.64.140 –m –w 1开启核心转发功能[root@lvs ~]#vim /etc/sysctl.conf net.ipv4.ip_forward=1[root@lvs ~]#sysctl -pnet.ipv4.ip_forward = 1[root@client ~]#while true; do curl 172.22.50.200 ;sleep 1;doneserver1server2保存[root@lvs ~]#ipvsadm-save -n > /etc/sysconfig/ipvsadm[root@lvs ~]#systemctl start ipvsadm[root@lvs ~]#systemctl enable ipvsadm.service
[root@server1 ~]# yum install mod_ssl[root@server1 ~]# systemctl restart httpd[root@lvs ~]#ipvsadm -A -t 172.22.50.200:443[root@lvs ~]#ipvsadm -a -t 172.22.50.200:443 -r 192.168.64.130 -m -w 3[root@lvs ~]#ipvsadm -a -t 172.22.50.200:443 -r 192.168.64.140 –m –w 1
主机 | ip1 | ip2 | |
---|---|---|---|
Client | 172.22.50.100 | gateway172.22.143.144 | |
router | 172.22.143.144 | 192.168.64.150 | |
LVS | 172.22.50.200 | 192.168.64.120 | gateway 192.168.64.150 |
Server1 | 192.168.64.130 | gateway 192.168.64.120 webserver | VIP192.168.64.200 |
Server2 | 192.168.64.140 | gateway 192.168.64.120 webserver | VIP192.168.64.200 |
router开启核心转发功能[root@router ~]#vim /etc/sysctl.conf net.ipv4.ip_forward=1[root@router~]#sysctl -pnet.ipv4.ip_forward = 1[root@client ~]#traceroute 192.168.64.130traceroute to 192.168.64.130 (192.168.64.130), 30 hops max, 60 byte packets 1 gateway (172.22.143.144) 1.551 ms 7.670 ms 7.611 ms 2 192.168.64.130 (192.168.64.130) 7.559 ms 7.512 ms 7.465 ms
#!/bin/bashvip='192.168.64.200'iface='lo:1'mask='255.255.255.255'port='80'rs1='192.168.64.130'rs2='192.168.64.140'scheduler='wrr'type='-g'rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/nullcase $1 instart) ifconfig $iface $vip netmask $mask #broadcast $vip up iptables -F ipvsadm -A -t ${vip}:${port} -s $scheduler ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1 ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1 echo "The VS Server is Ready!" ;;stop) ipvsadm -C ifconfig $iface down echo "The VS Server is Canceled!" ;;*) echo "Usage: $(basename $0) start|stop" exit 1 ;;esac
#!/bin/bashvip=192.168.64.200mask='255.255.255.255'dev=lo:1rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/nullservice httpd start &> /dev/null && echo "The httpd Server is Ready!"echo "`hostname`
" > /var/www/html/index.htmlcase $1 instart) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig $dev $vip netmask $mask #broadcast $vip up #route add -host $vip dev $dev echo "The RS Server is Ready!" ;;stop) ifconfig $dev down echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo "The RS Server is Canceled!" ;;*) echo "Usage: $(basename $0) start|stop" exit 1 ;;esac
[root@lvs ~]# ipvsadm -Ln --statsIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:PortTCP 192.168.64.200:80 7 42 0 2786 0 -> 192.168.64.130:80 3 18 0 1194 0 -> 192.168.64.140:80 4 24 0 1592 0
将VIP改为:10.0.0.100
Router加一个地址 10.0.0.200 与内网网卡绑定Ifconfig eth0:1 10.0.0.200/24[root@lvs ~]#ipvsadm -A -t 172.22.50.200:3306[root@lvs ~]#ipvsadm -a -t 192.168.64.130:3306 -r 192.168.64.130 -m -w 3[root@lvs ~]#ipvsadm -a -t 192.168.64.140:3306 -r 192.168.64.140 –m –w 1添加授权账号mysql -e ‘grant all on *.* to test@”%” identified by “centos” ’mysql –uroot –pcentos –h
iptables -t mangle -A PREROUTING -d 10.0.0.100 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 10[root@lvs ~]# ipvsadm -A -f 10 -s rr [root@lvs ~]# ipvsadm -a -f 10 –r 192.168.64.130 -m -w 3[root@lvs ~]# ipvsadm -a -f 10 –r 192.168.64.140 -m -w 3
[root@lvs ~]# ipvsadm -A -f 10 -s rr –p
前端的调度器会根据后端真实服务器的实际连接情况来分配请求
转载于:https://blog.51cto.com/9019400/2400117