首页 > 其他 > 详细

keepalived lvs 高可用负载均衡

时间:2018-04-10 17:33:38      阅读:201      评论:0      收藏:0      [点我收藏+]
主分发器设置

安装keepalived和nginx
yum install -y keepalived
yum install -y nginx
关闭防火墙和SELinux
systemctl stop firewalld
setenforce 0
配置网卡ens33

[root@lynn-04 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.116
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

编辑/etc/keepalived/keepalived.conf
写入如下内容:

[root@lynn-04 ~]# vim /etc/keepalived/keepalived.conf

    script "/usr/local/sbin/check_ng.sh"
    interval 3
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass aminglinux>com
    }
    virtual_ipaddress {
        192.168.130.100
    }
    track_script {
        chk_nginx
    }
}
virtual_server 192.168.130.100 80 {
    #(每隔10秒查询realserver状态)
    delay_loop 10
    #(lvs 算法)
    lb_algo wlc
    #(DR模式)
    lb_kind DR
    #(同一IP的连接0秒内被分配到同一台realserver)
    persistence_timeout 0
    #(用TCP协议检查realserver状态)
    protocol TCP
    real_server 192.168.130.118 80 {
        #(权重)
        weight 100
        TCP_CHECK {
        #(10秒无响应超时)
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.130.119 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

定义监控脚本/usr/local/sbin/check_ng.sh

[root@lynn-04 ~]# vim /usr/local/sbin/check_ng.sh

写入下面内容

#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
        /etc/init.d/nginx start
        n2=`ps -C nginx --no-heading|wc -l`
        if [ $n2 -eq "0"  ]; then
                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                systemctl stop keepalived
        fi
fi

更改/usr/local/sbin/check_ng.sh权限

[root@lynn-04 ~]# chmod 755 /usr/local/sbin/check_ng.sh

编写脚本 /usr/local/sbin/lvs_dr.sh

[root@lynn-04 ~]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.130.100
rs1=192.168.130.118
rs2=192.168.130.119
#注意这里的网卡名字
ifdown ens33
ifup ens33
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev ens33:2

执行脚本 /usr/local/sbin/lvs_dr.sh

[root@lynn-04 ~]# sh /usr/local/sbin/lvs_dr.sh
成功断开设备 ‘ens33‘。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)

启动keepalived

[root@lynn-04 ~]# systemctl start keepalived
[root@lynn-04 ~]# ps aux |grep keepalived
root       2190  0.1  0.1 120740  1404 ?        Ss   14:33   0:00 /usr/sbin/keepalived -D
root       2191  0.1  0.3 127480  3336 ?        S    14:33   0:00 /usr/sbin/keepalived -D
root       2192  0.0  0.3 131780  3108 ?        S    14:33   0:00 /usr/sbin/keepalived -D
root       2206  0.0  0.0 112676   988 pts/0    S+   14:33   0:00 grep --color=auto keepalived

启动nginx

[root@lynn-04 ~]# /etc/init.d/nginx start
[root@lynn-04 ~]# ps aux |grep nginx
root        912  0.0  0.1  45988  1240 ?        Ss   08:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody      913  0.0  0.4  48476  4388 ?        S    08:06   0:00 nginx: worker process
nobody      915  0.0  0.4  48476  4392 ?        S    08:06   0:00 nginx: worker process
root       2072  0.0  0.0 112680   980 pts/0    S+   10:23   0:00 grep --color=auto nginx

把nginx的默认页做个标记

[root@lynn-04 ~]# echo master > /data/wwwroot/default/index.html

在另一台机器使用从ip访问
得到下面结果

[root@lynn-02 ~]# curl 192.168.130.116
master

从分发器设置

安装keepalived和nginx
yum install -y keepalived
yum install -y nginx
关闭防火墙和SELinux
systemctl stop firewalld
setenforce 0
配置网卡ens33

[root@lynn-06 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=fef8a686-6a1e-49bc-b03e-7797fc4c58bb
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.128
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

编辑/etc/keepalived/keepalived.conf
写入如下内容:

[root@lynn-06 ~]# vim /etc/keepalived/keepalived.conf

    interval 3
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass aminglinux>com
    }
    virtual_ipaddress {
        192.168.130.100
    }
    track_script {
        chk_nginx
    }
}
virtual_server 192.168.130.100 80 {
    #(每隔10秒查询realserver状态)
    delay_loop 10
    #(lvs 算法)
    lb_algo wlc
    #(DR模式)
    lb_kind DR
    #(同一IP的连接0秒内被分配到同一台realserver)
    persistence_timeout 0
    #(用TCP协议检查realserver状态)
    protocol TCP
    real_server 192.168.130.118 80 {
        #(权重)
        weight 100
        TCP_CHECK {
        #(10秒无响应超时)
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.130.119 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

定义监控脚本/usr/local/sbin/check_ng.sh

[root@lynn-06 ~]# vim /usr/local/sbin/check_ng.sh

#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
        systemctl start nginx
        n2=`ps -C nginx --no-heading|wc -l`
        if [ $n2 -eq "0"  ]; then
                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                systemctl stop keepalived
        fi
fi

更改/usr/local/sbin/check_ng.sh权限

[root@lynn-06 ~]# chmod 755 /usr/local/sbin/check_ng.sh

编写脚本 /usr/local/sbin/lvs_dr.sh

[root@lynn-06 ~]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.130.100
rs1=192.168.130.118
rs2=192.168.130.119
#注意这里的网卡名字
ifdown ens33
ifup ens33
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev ens33:2

执行脚本 /usr/local/sbin/lvs_dr.sh

[root@lynn-06 ~]# sh /usr/local/sbin/lvs_dr.sh
成功断开设备 ‘ens33‘。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)

启动keepalived

[root@lynn-06 ~]# systemctl start keepalived
[root@lynn-06 ~]# ps aux |grep keepalived
root       2190  0.1  0.1 120740  1404 ?        Ss   14:33   0:00 /usr/sbin/keepalived -D
root       2191  0.1  0.3 127480  3336 ?        S    14:33   0:00 /usr/sbin/keepalived -D
root       2192  0.0  0.3 131780  3108 ?        S    14:33   0:00 /usr/sbin/keepalived -D
root       2206  0.0  0.0 112676   988 pts/0    S+   14:33   0:00 grep --color=auto keepalived

启动nginx

[root@lynn-06 ~]# systemctl start nginx
[root@lynn-06 ~]# ps aux |grep nginx
root        912  0.0  0.1  45988  1240 ?        Ss   08:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody      913  0.0  0.4  48476  4388 ?        S    08:06   0:00 nginx: worker process
nobody      915  0.0  0.4  48476  4392 ?        S    08:06   0:00 nginx: worker process
root       2072  0.0  0.0 112680   980 pts/0    S+   10:23   0:00 grep --color=auto nginx

把nginx的默认页做个标记

[root@lynn-06 ~]# echo backup > /usr/share/nginx/html/index.html

在另一台机器使用从ip访问
得到下面结果

[root@lynn-02 ~]# curl 192.168.130.128
backup

rs1设置

安装nginx
yum install -y nginx
关闭防火墙和SELinux
systemctl stop firewalld
setenforce 0
配置网卡ens33

[root@lynn-05 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.118
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

编写脚本/usr/local/sbin/lvs_rs.sh

[root@lynn-05 ~]# vim /usr/local/sbin/lvs_rs.sh

#/bin/bash
vip=192.168.130.100
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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

执行脚本/usr/local/sbin/lvs_rs.sh

[root@lynn-05 ~]# sh /usr/local/sbin/lvs_rs.sh

启动nginx

[root@lynn-05 ~]# systemctl start nginx
[root@lynn-05 ~]# ps aux |grep nginx
root        995  0.0  0.2 122908  2236 ?        Ss   14:29   0:00 nginx: master process /usr/sbin/nginx
nginx       996  0.0  0.3 123292  3136 ?        S    14:29   0:00 nginx: worker process
nginx       997  0.0  0.3 123292  3136 ?        S    14:29   0:00 nginx: worker process
nginx       998  0.0  0.3 123292  3136 ?        S    14:29   0:00 nginx: worker process
nginx       999  0.0  0.3 123292  3136 ?        S    14:29   0:00 nginx: worker process
root       1006  0.0  0.0 112676   980 pts/0    R+   14:30   0:00 grep --color=auto nginx

把nginx的默认页做个标记

[root@lynn-05 ~]# echo This is lynn-05 > /usr/share/nginx/html/index.html

在另一台机器使用从ip访问
得到下面结果

[root@lynn-02 ~]# curl 192.168.130.118
This is lynn-05

rs2设置

安装nginx
yum install -y nginx
关闭防火墙和SELinux
systemctl stop firewalld
setenforce 0
配置网卡ens33

[root@lynn-07 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.119
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

编写脚本/usr/local/sbin/lvs_rs.sh

[root@lynn-07 ~]# vim /usr/local/sbin/lvs_rs.sh

#/bin/bash
vip=192.168.130.100
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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

执行脚本/usr/local/sbin/lvs_rs.sh

[root@lynn-07 ~]# sh /usr/local/sbin/lvs_rs.sh

启动nginx

[root@lynn-07 ~]# systemctl start nginx
[root@lynn-07 ~]# ps aux |grep nginx
root        990  0.0  0.2 122908  2244 ?        Ss   14:30   0:00 nginx: master process /usr/sbin/nginx
nginx       991  0.0  0.3 123292  3144 ?        S    14:30   0:00 nginx: worker process
nginx       992  0.0  0.3 123292  3144 ?        S    14:30   0:00 nginx: worker process
nginx       993  0.0  0.3 123292  3144 ?        S    14:30   0:00 nginx: worker process
nginx       994  0.0  0.3 123292  3144 ?        S    14:30   0:00 nginx: worker process
root        996  0.0  0.0 112676   980 pts/0    S+   14:30   0:00 grep --color=auto nginx

把nginx的默认页做个标记

[root@lynn-07 ~]# echo This is lynn-07 > /usr/share/nginx/html/index.html

在另一台机器使用从ip访问
得到下面结果

[root@lynn-02 ~]# curl 192.168.130.119
This is lynn-07

测试

从下图可以看出当vip在主机上时可以实现负载均衡
技术分享图片
从下图可以看出当主机keepalived停止后vip就出现在从机上时也能可以实现负载均衡
技术分享图片
从下图可以看出当主机keepalived再次启动时vip又回到主机负载均衡还是可以实现
技术分享图片
综上这里就同时实现了高可用和负载均衡

keepalived lvs 高可用负载均衡

原文:http://blog.51cto.com/10963213/2096577

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!