LVS+keepalived 实战
Posted CTO老王
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS+keepalived 实战相关的知识,希望对你有一定的参考价值。
安装依赖:
yum -y install wget libnl* popt* gcc.x86_64 gcc-c++.x86_64 gcc-objc++.x86_64 kernel-devel.x86_64 make popt-static.x86_64 yum -y install openssl-devel
一、分别在backup lvs和master lvs上安装LVS
wget http:uname -r 3.10.0-514.2.2.el7.x86_64 ln -s /usr/src/kernels/3.10.0-514.6.2.el7.x86_64-i686/ /usr/src/linux--&& make install
二、分别在backup lvs和master lvs上安装keepalived
wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz tar zxvf keepalived-1.1.19.tar.gz cd keepalived-1.1.19 ./configure --prefix=/usr/local/keepalived make make install cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ mkdir /etc/keepalived
三、修改keepalived相关配置
vi /etc/sysconfig/keepalived KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"
vi /etc/init.d/keepalived #更新 . /etc/sysconfig/keepalived PATH="$PATH:/usr/sbin" export PATH
vi /usr/local/keepalived/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 50625185@qq.com } notification_email_from 50625185@qq.com smtp_server localhost smtp_connect_timeout 30 router_id NodeA } vrrp_instance VI_1 { state MASTER interface ens160 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.20.229 //虚拟IP } } virtual_server 192.168.20.229 80 { //定义虚拟服务器 delay_loop 6 //健康检查时间,单位是秒 lb_algo rr //负载调度算法,这里设置为rr,即轮询算法 lb_kind DR //LVS实现负载均衡的机制,可以有NAT、TUN和DR三个模式可选 persistence_timeout 1 //会话保持时间,单位是秒(可以适当延长时间以保持session) protocol TCP //转发协议类型,有tcp和udp两种 real_server 192.168.20.200 8151 { //定义WEB服务器 weight 1 //权重 TCP_CHECK { //通过tcpcheck判断RealServer的健康状态 connect_timeout 5 //连接超时时间 nb_get_retry 3 //重连次数 delay_before_retry 3 //重连间隔时间 connect_port 8151 //检测端口 } } real_server 192.168.20.201 8151 { //定义WEB服务器 weight 2 //权重 TCP_CHECK { //通过tcpcheck判断RealServer的健康状态 connect_timeout 5 //连接超时时间 nb_get_retry 3 //重连次数 delay_before_retry 3 //重连间隔时间 connect_port 8151 //检测端口 } } }
虚拟VIP:192.168.20.229
分流WEB:192.168.20.200、192.168.20.201
四、启动keepalived
/etc/init.d/keepalived start
五、客户机脚本
#!/bin/bash # description: Config realserver lo and apply noarp SNS_VIP=192.168.20.229 /etc/rc.d/init.d/functions 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
六、测试
ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP docker223:80 rr persistent 1 -> 192.168.20.200:8151 Route 1 0 0
浏览器访问 http://192.168.20.229 分流 http://192.168.20.200:8151 WEB服务
以上是关于LVS+keepalived 实战的主要内容,如果未能解决你的问题,请参考以下文章