keepalived + lvs + nginx 主备配置案例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived + lvs + nginx 主备配置案例相关的知识,希望对你有一定的参考价值。

一、keepalived lvs 集群
1、结构
技术分享图片
2、主备修改内核参数
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

注:如果遇到提示是缺少包,yum 安装即可

3、启动keepalived
[[email protected] keepalived-1.3.5]# ./sbin/keepalived -f /usr/local/keepalived/etc/keepalived.conf
技术分享图片
4、修改配置文件

1 > 192.168.1.109 vrrp-master-config


global_defs { #全局定义模块
notification_email {br/>[email protected]
[email protected]
br/>[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 { #vrrp 实例
state MASTER #主master
interface eth0
virtual_router_id 51
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.50 #vip 浮动的虚拟ip
}
}

virtual_server 192.168.1.50 80 { #虚拟ip
delay_loop 6 # 健康检查时间,
lb_algo wrr # 负载调度的算法为wlc
lb_kind DR #DR模式
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 192.168.1.111 80 { # 指定real server1的IP地址
weight 3 #权值,数字越大权重越高
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.112 80 { # 指定real server2的IP地址
weight 3 # 权值,数字越大权重越高
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}


2> 192.168.1.110 vrrp-slave-config: 把master的文件copy过来,其它不变


vrrp_instance VI_1 { #vrrp 实例要与master一致
state BACKUP #slave
interface eth0
virtual_router_id 51
priority 100 #优先级调低
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.50 #vip 浮动的虚拟ip
}
}


3> 状态
vrrp 虚拟ip飘在master 109上:
技术分享图片
把109的keepalived kill掉,vip会飘到备机110上
技术分享图片

二、nginx 安装
1>分别在192.168.1.111 , 192.168.1.112 上安装,安装httpd也可以
技术分享图片

2> dr 模式需做特殊设置,创建脚本,并且start
[[email protected] init.d]# cat realserver
#! /bin/bash

SNS_VIP=192.168.1.50
/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

三、访问测试,实现主备
技术分享图片
技术分享图片
wrr 轮循
技术分享图片



以上是关于keepalived + lvs + nginx 主备配置案例的主要内容,如果未能解决你的问题,请参考以下文章

keepalived+nginx+lvs 安装流程

Linux下部署LVS(DR)+keepalived+Nginx负载均衡

LVS/Nginx/HAProxy keepalived/heartbeat总结

lvs + keepalived + nginx 实现高可用

lvs+keepalived+nginx实现高性能负载均衡集群

Nginx+Keepalived高可用集群应用实践