LVS+Keepalived+nginx+单纯keepalived高可用

LVS+Keepalived实现高可用集群

LVS+Keepalived 介绍


LVS

LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群 

系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项 目之 

一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);

十种调度算法(rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。

Keepalvied

Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP 

主机之间failover的实现

IP配置信息:

  LVS-DR-Master          172.16.1.1 

  LVS-DR-BACKUP          172.16.1.8 

  LVS-DR-VIP             172.16.1.80        

  WEB1-Realserver01      172.16.1.2 

  WEB2-Realserver02      172.16.1.3

  WEB3-Realserver03      172.16.1.4


一,安装LVS和Keepalvied软件包

1. Install ipvsadm

yum –y ipvsadm

2. 安装LVS和Keepalived 

yum –y install gcc kernel-devel openssl-devel
tar zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure -–sysconfdir=/etc/ --with-kernel-dir=/usr/src.kernel/2.6XXXX  
make && make install
ln -s /usr/local/sbin/keepalived /sbin/
chkconfig --add keepalived
chkconfig --level 2345 keepalived on
service keepalived restart


二. 配置LVS实现负载均衡

lvs-dr

#!/bin/bash
VIP=172.16.1.80
./etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:1
echo "DirectoryServer Start OK"
;;
stop)
ipvsadm -C
ipvsadm -Z
ifconfig eth0:1 down
/sbin/route del $VIP >/dev/null 2>&1
echo "DirectoryServer Stop OK"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
 esca
exit0

lvs-realserver

#!/bin/bash

VIP=10.207.238.82
. /etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $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)
/sbin/ifconfig lo:0 down
/sbin/route del $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 "Usage: $0 {start|stop}"
exit 1
esac
exit 0
#end


三.利用Keepalvied結合lvs實現高可用实现负载均衡和和高可用性

1.配置在主负载均衡服务器上配置keepalived.conf

#vi /etc/keepalived/keepalived.conf (主调度器)

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.1.80
    }
}
virtual_server 172.16.1.80 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 60
    protocol TCP
   
    real_server 172.16.1.2 80 {
        weight 3             
        TCP_CHECK {
        connect_timeout 10   
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
} 
    }
    real_server 172.16.1.3 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
    real_server 172.16.1.4 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}


在备用调度器上:

#vi /etc/keepalived/keepalived.conf (备调度器)

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.1.80
    }
}
virtual_server 172.16.1.80 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 60
    protocol TCP
   
    real_server 172.16.1.2 80 {
        weight 3             
        TCP_CHECK {
        connect_timeout 10   
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
} 
    }
    real_server 172.16.1.3 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
    real_server 172.16.1.4 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

如果報錯

Jan 11 09:06:26 hnxz-vm-dg-s1 Keepalived_vrrp[2436]: ip address associated with VRID not present in received packet : 10.207.238.82
Jan 11 09:06:26 hnxz-vm-dg-s1 Keepalived_vrrp[2436]: one or more VIP associated with VRID mismatch actual MASTER advert

應該為virtual id 問題可以通過命令查看

tcpdump -nn -i any net 224.0.0.0/8

#watch ipvsadm –ln 
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
  -> RemoteAddressort           Forward Weight ActiveConn InActConn
TCP  61.164.122.8:80 wrr persistent 60
  -> 61.164.122.10:80            Route   3      0          0
  -> 61.164.122.9:80             Route   3      0          0

#tail –f /var/log/message  监听日志,查看状态,测试LVS负载均衡及高可用性是否 

有效。

5.停Master服务器的keepalived服务,查看BAKCUP服务器是否能正常接管服务。


6.单纯keepalived 实现高可用

! Configuration File for keepalived
global_defs {
        notification_email {
        jason.zr.zhang@gmail.com
        }
        notification_email_from jason.zr.chang@mail.foxconn.com
        smtp_server 10.195.225.126
        smtp_connect_timeout 30
        router_id LVS_DEVEL
}
vrrp_script chk_http_port {
        script "/etc/keepalived/check-httpd.sh"
        interval 2
        weight 2
        }
vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 80
        priority 100
        authentication {
                auth_type PASS
                auth_pass eric
                }
        track_script {
                chk_http_port
                }
        virtual_ipaddress {
                10.207.238.82
                }
}

检测脚本文件

vim /etc/keepalived/check-httpd.sh

#!/bin/bash
A=`ps -C httpd –no-header |wc -l`
if [ $A -eq 0 ];then
  /usr/local/nginx/sbin/nginx #nginx命令的路径
  sleep 3
  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
    killall keepalived
  fi
fi

测试是否正常



转载于:https://my.oschina.net/ambari/blog/611087

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值