keepalived+lvs+nginx高可用

环境说明:

IP地址 部署应用
192.168.10.100 VIP0
192.168.10.101 VIP1
192.168.10.17 keepalived+lvs
192.168.10.16 keepalived+lvs
192.168.10.15 nginx
192.168.10.14 nginx

keepalived+lvs机器上配置

#1.安装keepalived

yum install keepalived –y

#2.配置keepalived

global_defs {
	router_id  LVS
}
vrrp_instance LVS_1 {
    state MASTER	#备改为 BACKUP
    interface ens33
    virtual_router_id 100
    priority 100	#备改为 90
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.100
        192.168.10.101
    }
}

virtual_server 192.168.10.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 120
    protocol TCP
    real_server 192.168.10.15 80 {
        TCP_CHECK {
            weight 1
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 192.168.10.10 80 {
        TCP_CHECK {
            weight 1
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

virtual_server 192.168.10.101 81 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0
    protocol TCP
    real_server 192.168.10.15 81 {
        TCP_CHECK {
            weight 1
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 81
        }
    }
    real_server 192.168.10.10 81 {
        TCP_CHECK {
            weight 1
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 81
        }
    }
}

#3.检查是否支持安装lvs
find /lib/modules/$(uname -r)/ -iname "**.ko*" | cut -d/ -f5-

yum install ipvsadm –y	#yum安装lvs

#4.开启转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward

sysctl -p	# 重新载入sysctl变量

nginx机器配置

#1.yum安装nginx或者编译安装

yum install nginx -y

service nginx start	#启动nginx

#2.修改nginx.conf,配置81的端口
vim /etc/nginx/nginx.conf

echo '192.168.10.15:80' > /usr/share/nginx/html/index.html	#设置web的标志内容

#3.lvs脚本

#!/bin/bash
# Script to start LVS DR real server. 
# chkconfig: - 90 10 
# description: LVS DR real server

VIP0=192.168.10.100
VIP1=192.168.10.101

. /etc/rc.d/init.d/functions

case "$1" in
start)
	ifconfig lo:0 $VIP0 broadcast $VIP0 netmask 255.255.255.255 up
	route add -host $VIP0 dev lo:0

	ifconfig lo:1 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up
	route add -host $VIP1 dev lo:1

	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 $VIP0 > /dev/null 2>&1
	ifconfig lo:1 down
	route del $VIP1 > /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 "$0: Usage:$0 {start|stop}"
	exit 1
;;
esac

#4.添加开机自启动
hmod +x /etc/init.d/lvs-rs
chkconfig --add lvs-rs
chkconfig lvs-rs on
/etc/init.d/lvs-rs start

验证

访问VIP的网址,看是否每次访问的站点内容都是不同的。windows中的浏览器有缓存功能,如果站点内容有时候是相同的,那么不一定是自己配错了,可能是缓存的原因,避免的办法就是使用Linux的curl命令去访问站点的内容

ipvsadm -ln		#查看连接数
posted @ 2019-07-15 17:27  博客萨斯州  阅读(321)  评论(0编辑  收藏  举报