ts2009 发表于 2018-12-30 14:15:33

keepalived + lvs + nginx 主备配置案例

  一、keepalived lvs集群
1、结构
http://i2.运维网.com/images/blog/201803/03/501b29cdd8f42a0a9857bf57db881918.png
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
# ./sbin/keepalived -f /usr/local/keepalived/etc/keepalived.conf
http://i2.运维网.com/images/blog/201803/03/2713caaeb4c0c2ce0add832ad21a53e0.png
4、修改配置文件
  1 > 192.168.1.109 vrrp-master-config

global_defs {      #全局定义模块
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
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上:
http://i2.运维网.com/images/blog/201803/03/4b490c42240b8ad4f5b6e7a290ba21fd.png
把109的keepalived kill掉,vip会飘到备机110上
http://i2.运维网.com/images/blog/201803/03/67fec45c88d4aea46a8974387fac7bf0.png
  二、nginx 安装
1>分别在192.168.1.111 , 192.168.1.112 上安装,安装httpd也可以
http://i2.运维网.com/images/blog/201803/03/382e42b2a0427afc79abfb5943bbe9be.png
  2>   dr 模式需做特殊设置,创建脚本,并且start

# 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

  三、访问测试,实现主备
http://i2.运维网.com/images/blog/201803/03/888f507844eb88e9c0234fb5816c286a.png
http://i2.运维网.com/images/blog/201803/03/5fbfa48db0ea28c1b0629d907f425316.png
wrr 轮循
http://blog.运维网.com/keep11/



页: [1]
查看完整版本: keepalived + lvs + nginx 主备配置案例