LVS&&KeepAlived&&Nginx实现LB方案

相应概念部分:

LVSLinux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。

Keepalived作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
 

服务器部署准备部分:

1)虚拟机4台

LVS&&KeepAlived&&Nginx实现LB方案_第1张图片

2)基础架构示意图

LVS&&KeepAlived&&Nginx实现LB方案_第2张图片

3)安装源文件下载地址

Keepalived&Nginx

 

软件安装步骤

192.168.1.128192.168.1.129上安装keepalived
192.168.1.130192.168.1.131上安装nginx

Keepalived安装步骤:

基础环境安装

yum install gcc
yum -y install openssl-devel
yum -y install libnl libnl-devel
yum install -y libnfnetlink-devel
yum -y install net-tools
yum install vim -y

源码安装

1)将keepalived安装包复制到/usr/local/src

2)  执行下列安装编译步骤

    cd /use/local/src
    tar -zxvf keepalived-2.0.10.tar.gz
    mv keepalived-2.0.10 ../keepalived
    cd /use/local/keepalived/
    ./configure
    make && make install

3)keepalived配置部分

将keepalived配置文件拷贝到etc/keepalived下

mkdir /etc/keepalived
cp /usr/local/keepalived/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

 设置Keepalived对应的开机启动服务

cp /usr/local/keepalived/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/sbin/keepalived /usr/sbin/

4)Keepalived 启动相关

   service keepalived start|stop|restart

 

ipvsadm 安装步骤:

只需要在192.168.1.128及192.168.1.129上安装即可

yum install ipvsadm -y

Nginx 安装步骤:

进入到nginx 安装目录,执行yum install nginx-* -y

 

防火墙相关配置:

systemctl stop firewalld.service
systemctl stop iptables.service

这边为了方便,直接采用关闭相关的防火墙进行操作

 

Keepalived&&Nginx对应的配置

Nginx相关配置:

首先,确定nginx已经处于启动状态;

cd /etc/init.d/

编辑脚本:

vim realserver

#虚拟的vip 根据自己的实际情况定义
SNS_VIP=192.168.1.200
/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

设置脚本对应权限

chmod 755 /etc/init.d/realserver

chmod 755 /etc/rc.d/init.d/functions

 

执行脚本启动: service realserver start

 

ip addr  查看VIP地址,出现对应的VIP地址,则表示配置成功

LVS&&KeepAlived&&Nginx实现LB方案_第3张图片

 

Keepalived配置部分:

cd /etc/keepalived

mv keepalived.conf keepalived-back.conf

vim keepalived.conf

备注:master 和 slave 配置有一定的差别

以下为Master的配置:

LVS&&KeepAlived&&Nginx实现LB方案_第4张图片

以下为Slave的配置:

LVS&&KeepAlived&&Nginx实现LB方案_第5张图片

配置注意事项:

router_id      后面跟的自定义的ID在同一个网络下是一致的

state             state后跟的MASTER和BACKUP必须是大写;否则会造成配置无法生效的问题

interface      网卡ID, 采用ip a 进行查看

priority         MASTER中配置的priority必须比BACKUP大;差值最好>=50

virtual_ipaddress   配置的VIP;允许配置多个

 

启动Keepalived; service Keepalived

 

检查Keepalived 启动之后配置情况;只在MASTER节点上出现对应的VIP,则为成功;

如果两个服务器均出现VIP地址, 则发生脑裂情况。

 

nginx配置调用其他服务接口配置DEMO大致如下:

upstream proxy-XX2 {
        #ip_hash;
        server 10.1.80.167:8080 weight=1 max_fails=3 fail_timeout=10s;
        server 10.1.80.172:8080 weight=1 max_fails=3 fail_timeout=10s;
}
server {
        listen 80;
        server_name XX2.zhongbang.com;
        access_log /data/log/nginx/XX2.access.log;
        error_log /data/log/nginx/XX2.error.log;

        location / {
            proxy_pass http://proxy-XX2;
            proxy_set_header Host $http_host;
            #proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

 

最终访问服务均采用 域名的方式

 

你可能感兴趣的:(大数据基础平台)