lvs+keepalived集群安装配置

时间:2021-01-26 03:07:50
我这里实验选择的是DR模式wlc算法。
1、环境准备
lvs1:192.0.2.16
lvs2:192.0.2.17
nginx:192.0.2.18
nginx:192.0.2.19
VIP:192.0.2.20


2、安装ipvsadm
yum install ipvsadm 
ipvsadm-1.26-4.el6.i686.rpm 
3、安装keepalived
yum install keepalived
keepalived-1.2.13-5.el6_6.i686.rpm 
4、配置keepalived
vi /etc/keepalived/keepalived.conf
service keepalived restart
5、维护命令
service ipvsadm start
service keepalived start


service ipvsadm stop
service keepalived stop


6、加入开机自启动
chkconfig keepalived on
chkconfig ipvsadm on


7、服务器端配置
master
[root@lvs2 keepalived]# cat keepalived.conf 
! Configuration File for keepalived


global_defs {
   notification_email {
     yujinga@wumart.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server smtp.wumart.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}


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 {
        192.0.2.20                                   //vip
    }
}


virtual_server 192.0.2.20 80 {
    delay_loop 6
    lb_algo wlc
    lb_kind DR
    nat_mask 255.255.0.0
    persistence_timeout 50
    protocol TCP


    real_server 192.0.2.18 80 {
        weight 15
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
    }
}
    real_server 192.0.2.19 80 {
        weight 10
            TCP_CHECK { 
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
    }
! Configuration File for keepalived


global_defs {
   notification_email {
     yujinga@wumart.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server smtp.wumart.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}


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 {
        192.0.2.20
    }
}


virtual_server 192.0.2.20 80 {
    delay_loop 6
    lb_algo wlc
    lb_kind DR
    nat_mask 255.255.0.0
    persistence_timeout 50
    protocol TCP


    real_server 192.0.2.18 80 {
        weight 15
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
    }
}
    real_server 192.0.2.19 80 {
        weight 10
            TCP_CHECK { 
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
    }
}
=====================================================================================
linux客户端配置
客户端配置脚本 drvip.sh
#!/bin/bash
# description: Config realserver lo and apply noarp 
SNS_VIP=192.0.2.20
/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


客户端启停
/sbin/drvip.sh start
/sbin/drvip.sh stop
--------------------------------------------------------------------------------
win客户端配置
1、win客户端
需要从管理工具里面--添加设备---回环网卡---4个255+虚拟ip
2、添加到服务器配置文件
3、重启keeplived
=================================================================================
服务器端查看连接数和vip
查看连接数
ipvsadm -ln
查看源IP 目的IP 访问
ipvsadm -l -n --connection
==================================================================================
测试
安装两台nginx服务器
chkconfig iptables off
service iptables stop
chkconfig NetworkManager off
vi /etc/selinux/config
SELINUX=disabled


1、相关依赖包安装
yum -y install gcc openssl-devel pcre-devel zlib-devel openssl pcre wget 
2、建立nginx用户及组
groupadd -r nginx && useradd -r -g nginx -s /bin/false -M nginx
3、下载并安装nginx
wget ftp://10.101.200.210/pub/nginx/nginx-1.8.0.tar.gz
tar -xzvf nginx-1.8.0.tar.gz
cd ./nginx-1.8.0
./configure   --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log   --pid-path=/var/run/nginx/nginx.pid    --lock-path=/var/lock/nginx.lock   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_realip_module   --with-http_stub_status_module   --with-http_gzip_static_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre
make && make install
启动停止脚本
wget ftp://10.101.200.210/pub/nginx/nginx -O /etc/rc.d/init.d/nginx
chmod +x /etc/rc.d/init.d/nginx
chkconfig --level 345 nginx on
chkconfig --list | grep nginx
service nginx start
service nginx status
#service nginx stop


查看nginx配置
nginx -V
进入目录
cd /usr/local/nginx/html
vi index.html
加入区分标志
在表头加入ip地址
分别输入ip地址可以访问。
===================================================================================
启动keealived,并进行测试访问。
1、启动keealived,并进行测试访问。
2、停止lvs1,在进行访问,并查看lvs2上面的vip地址和连接。
3、启动lvs1,看是否回切。
4、可以在主备机上打开log。查看切换过程。