目标如图,不考虑负载均衡服务器的主备。
在负载均衡服务器做如下操作
1.安装lvs
安装依赖包:yum -y install gcc popt-devel popt-static libnl libnl-devel
获取安装包:wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
解压到指定目录:tar -zxvf ipvsadm-1.26.tar.gz -C /usr/local/
进入解压后的目录:cd /usr/local/ipvsadm-1.26/
安装:make && make install
2.安装keepalive
先安装依赖包:yum -y install openssl-devel
获取安装包:wget http://www.keepalived.ord/software/keepalived-1.2.19.tar.gz
解压到指定目录:tar -zxvf keepalived-1.2.19.tar.gz -C /root
进入解压后的目录:cd /root/keepalived-1.2.19
配置:./configure --prefix=/usr/local
安装:make && make install
做软连接:
ln -s /usr/local/etc/keepalived/ /etc ln -s /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ ln -s /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ ln -s /usr/local/sbin/keepalived /usr/sbin/
3.配置keepalive
参考修改/etc/keepalive/keepalive.cfg
! Configuration File for keepalived global_defs { notification_email { test@gmail.com } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id lvs_1 #设备标识 } vrrp_instance LVS_HA { state MASTER #LVS slave is BACKUP interface eth0 #根据要配置的网卡确定,不能错 virtual_router_id 60 #设置虚拟路由ID,主从要保持一致 priority 100 #优先级数值大的被推举为主设备 advert_int 1 #检查间隔,默认为1秒 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.215.7.114/24 } } virtual_server 10.215.7.114 80 { delay_loop 6 lb_algo wrr #加权轮询-负载均衡算法 lb_kind DR #调度算法为DR直接路由模式 nat_mask 255.255.255.0 persistence_timeout 50 #会话保持50秒 protocol TCP #数据转发协议为TCP real_server 10.215.7.101 80 { weight 1 #设置服务器权重 TCP_CHECK { #对后端真实服务器进行TCP健康检查 connect_timeout 20 #连接超时20秒 connect_port 80 #通过TCP端口80去检查真实服务器健康状态 nb_get_retry 3 #连接重试次数为3次 } } real_server 10.215.7.102 80 { weight 1 TCP_CHECK { connect_timeout 20 connect_port 80 nb_get_retry 3 } } }
4.启动Keepalived
启动keepalived:service keepalived start
配置开机启动:chkconfig keepalived on
5.检查VIP是否生效
查看虚拟IP:ip addr show (可简写为ip addr或ip a)
[root@lvs1-(102.109) ~]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 link/ether 40:f2:e9:2c:cf:da brd ff:ff:ff:ff:ff:ff inet 10.215.7.109/24 brd 10.215.7.255 scope global eth0 inet 10.215.7.114/24 scope global secondary eth0 #此行为虚拟IP,即VIP。 inet6 fe80::42f2:e9ff:fe2c:cfda/64 scope link valid_lft forever preferred_lft forever
6.检查LVS规则
[root@lvs1-(102.109) ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.215.7.114:80 wrr persistent 50 -> 10.215.7.101:80 Route 1 0 0 -> 10.215.7.102:80 Route 1 0 0
在实际业务服务器做如下操作
7.添加VIP
编写脚本realserver,并给于执行权限。
SNS_VIP=192.168.80.200 case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0
然后运行
[root@slave15 ~]# ./realserver start RealServer Start OK [root@slave15 ~]# ifconfig em1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.92.165 netmask 255.255.255.0 broadcast 192.168.92.255 inet6 fe80::266e:96ff:fe06:3c40 prefixlen 64 scopeid 0x20<link> ether 24:6e:96:06:3c:40 txqueuelen 1000 (Ethernet) RX packets 444279825 bytes 167878005342 (156.3 GiB) RX errors 1 dropped 0 overruns 0 frame 1 TX packets 12911485 bytes 3863382091 (3.5 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 em2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 150.158.3.165 netmask 255.255.255.192 broadcast 150.158.3.191 inet6 fe80::266e:96ff:fe06:3c42 prefixlen 64 scopeid 0x20<link> ether 24:6e:96:06:3c:42 txqueuelen 1000 (Ethernet) RX packets 35969392722 bytes 118119416735866 (107.4 TiB) RX errors 69103 dropped 0 overruns 0 frame 69103 TX packets 30584120194 bytes 31541025875439 (28.6 TiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 494491018 bytes 5865911143344 (5.3 TiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 494491018 bytes 5865911143344 (5.3 TiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 192.168.80.200 netmask 255.255.255.255 #此行为虚拟IP,即VIP。 loop txqueuelen 0 (Local Loopback)
如果要停止vip,可以通过如下命令执行
./realserver stop
验证负载均衡
客户端用curl或wget命令测试VIP