页面树结构

版本比较

标识

  • 该行被添加。
  • 该行被删除。
  • 格式已经改变。


目录

Smartbi Proxy负载均衡服务,为产品提供负载均衡和代理服务。 

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

服务准备:


IP地址角色
服务器1192.168.137.111smartbi-proxy,keepalived
服务器2192.168.137.112smartbi-proxy,keepalived
虚拟IP(VIP)192.168.137.155

1、安装Smartbi Proxy

参考 Linux Tocmat部署Smartbi Proxy 在两台服务器上部署好Smartbi Proxy,并确保能正常访问。

2、安装keepalived

1.环境准备

安装需要基础环境

代码块
linenumberstrue
yum install -y gcc gcc-c++ make openssl-devel

2.安装keepalived(服务器均需执行)

上传Keepalived安装包到服务器,并解压到指定目录

代码块
linenumberstrue
tar -zxvf keepalived-1.3.6.tar.gz -C /opt

进入解压目录,执行脚本安装Keepalived

代码块
linenumberstrue
cd /opt/keepalived-1.3.6/
./configure --prefix=/opt/keepalived
make && make install

拷贝执行文件

代码块
linenumberstrue
cp /opt/keepalived/sbin/keepalived /usr/sbin/

将init.d文件拷贝到etc下,加入开机启动项keepalived文件拷贝到etc/init.d目录,加入开机启动项

代码块
linenumberstrue
cp /opt/keepalived-1.3.6/keepalived/etc/init.d/keepalived /etc/init.d/keepalived

将keepalived配置文件拷贝到etc下

代码块
linenumberstrue
cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

创建keepalived文件夹

代码块
linenumberstrue
mkdir /etc/keepalived

将keepalived配置文件拷贝到etc下

代码块
linenumberstrue
cp /opt/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

添加可执行权限

代码块
linenumberstrue
chmod +x /etc/init.d/keepalived

加入开机启动

代码块
linenumberstrue
chkconfig --add keepalived 
chkconfig keepalived on
systemctl enable keepalived

3.修改Keepalived配置文件

1.服务器192.168.137.111相关配置:

代码块
linenumberstrue
vi /etc/keepalived/keepalived.conf

修改配置参考如下

代码块
languagebash
linenumberstrue
! Configuration File for keepalived

global_defs {
   router_id master        #节点ID
}

vrrp_script chk_proxy {
    script "/etc/keepalived/proxy_check.sh"    #检查smartbi proxy状态脚本
    interval 2             #检测时间间隔
    weight -20             #如果条件成立权重-20
}

vrrp_instance smartbi_proxy {
    state MASTER           #主备状态
    interface ens33        #ifconfig 查看IP对应的网卡名称
    virtual_router_id 51   #虚拟路由id
    priority 100           #权重,权重高的为master
    advert_int 1 
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_proxy          #执行smartbi proxy监控
    }
    virtual_ipaddress {
        192.168.137.155    #VIP 虚拟IP
    }
}

创建监控脚本

代码块
linenumberstrue
vi /etc/keepalived/proxy_check.sh

脚本参考如下:

代码块
linenumberstrue
#!/bin/bash
count=$(ps -ef | grep tomcat | egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
   systemctl stop keepalived
fi

脚本添加执行权限

代码块
linenumberstrue
chmod +x /etc/keepalived/proxy_check.sh


2..服务器192.168.137.112相关配置:

代码块
linenumberstrue
vi /etc/keepalived/keepalived.conf

修改配置参考如下

代码块
languagebash
linenumberstrue
! Configuration File for keepalived

global_defs {
   router_id slave1        #节点ID
}

vrrp_script chk_proxy {
    script "/etc/keepalived/proxy_check.sh"    #检查smartbi proxy状态脚本
    interval 2             #检测时间间隔
    weight -20             #如果条件成立权重-20
}

vrrp_instance smartbi_proxy {
    state BACKUP           #主备状态
    interface ens33        #ifconfig 查看IP对应的网卡名称
    virtual_router_id 51   #虚拟路由id
    priority 90           #权重
    advert_int 1 
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_proxy          #执行smartbi proxy监控
    }
    virtual_ipaddress {
        192.168.137.155    #VIP 虚拟IP
    }
}

创建监控脚本

代码块
linenumberstrue
vi /etc/keepalived/proxy_check.sh

脚本参考如下:

代码块
linenumberstrue
#!/bin/bash
count=$(ps -ef | grep tomcat | egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
   systemctl stop keepalived
fi

脚本添加执行权限

代码块
linenumberstrue
chmod +x /etc/keepalived/proxy_check.sh

4.启动Keepalived

登陆两台服务器分别启动Keepalived

代码块
linenumberstrue
systemctl start keepalived

查看Keepalived运行状态

代码块
linenumberstrue
systemctl status keepalived

5.检测Keepalived

1.服务器1中Keepalived配置文件中权重高,所以该服务器成为Keepalived主服务器,拥有VIP地址,通过VIP地址能访问smartbi Proxy。

2.停止服务器1的smartbi proxy服务,脚本会关闭Keepalived并释放VIP,服务器2将自动获取VIP地址,通过VIP地址依旧能访问smartbi Proxy。

3.重新启动服务器1的smartbi proxy,Keepalived服务,服务器1根据Keepalived配置文件配置,会重新抢占VIP地址。

4.关闭两台服务器的smartbi proxy服务,Keepalived主动关闭,无法通过VIP地址访问服务。