当前位置: 代码迷 >> 综合 >> haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)
  详细解决方案

haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)

热度:43   发布时间:2023-10-17 03:24:41.0

1. 准备工作

yum install gcc gcc-c++ make openssl-devel kernel-devel

2. Haproxy安装(源码安装)

本次安装使用的版本是 haproxy-1.5.14.tar.gz

1. 解压  

我的目录是/usr/local/bigdata/下

tar -zvxf haproxy-1.5.14.tar.gz

2. 进入解压目录  cd   haproxy-1.5.14   

make TARGET=linux310 CPU=x86_64 PREFIX=/usr/local/haproxy USE_OPENSSL=1 ADDLIB=-lz

linux310 CPU=x86_64    310和*86_64 根据 命令 uname -r确定

/usr/local/haproxy是 安装路径

haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)

ldd haproxy | grep ssl

haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)

make install PREFIX=/usr/local/haproxy
##/usr/local/bigdata是我的解压目录mkdir -p /usr/local/haproxy/conf
mkdir -p /etc/haproxy
cp /usr/local/bigdata/haproxy-1.5.14/examples/option-http_proxy.cfg /usr/local/haproxy/conf/haproxy.cfg
ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg
cp -r /usr/local/bigdata/haproxy-1.5.14/examples/errorfiles  /usr/local/haproxy/errorfiles
ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles
mkdir -p /usr/local/haproxy/log
touch /usr/local/haproxy/log/haproxy.log
ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log
cp /usr/local/bigdata/haproxy-1.5.14/examples/haproxy.init /etc/rc.d/init.d/haproxy
chmod +x /etc/rc.d/init.d/haproxy
chkconfig haproxy on
ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin

3. 配置文件 haproxy 具体参数

##复制的时候需要把 #注释的去掉 globallog         127.0.0.1 local2chroot      /usr/local/haproxy   #haproxy安装目录maxconn     4000user        haproxygroup       haproxydaemondefaultsmode                    tcp            log                     globaloption                  dontlognulloption                  redispatchretries                 3timeout http-request    10stimeout queue           1mtimeout connect         10stimeout client          1mtimeout server          1mtimeout http-keep-alive 10stimeout check           10smaxconn                 600      listen hive2 *:3308mode tcpbalance roundrobin maxconn 1024 server hive_1 172.20.3.102:10000 check inter 180000 rise 1 fall 2server hive_2 172.20.3.103:10000 check inter 180000 rise 1 fall 2listen stats                              mode httpbind :6677                          stats enablestats hide-version                    stats uri     /haproxyadmin?stats    stats realm   Haproxy\ Statistics    stats auth    admin:admin      #登录网页所需的账号密码          stats hide-version                   

4.启停

 service haproxy start    #启动service haproxy stop     #关闭service haproxy restart  #重启service haproxy status   #查看服务状态

haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)

web页面查看

http://172.20.3.102:6677/haproxyadmin?stats

haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)

5.  报错  启动haproxy报错 [: =: unary operator expected

如果启动HAProxy出现 /etc/rc.d/init.d/haproxy: line 26: [: =: unary operator expected 这个错误,修改/etc/init.d/haproxy  文件的26行 [ ${NETWORKING} = "no" ] && exit 0 为 [ "${NETWORKING}" = "no" ] && exit 0

3.centos7 yum安装Haproxy / keepalived (两台机器)

yum install -y haproxy keepalived

版本  haproxy-1.5.18-8.el7.x86_64    keepalived-1.3.5-8.el7_6.x86_64

3.1 Haproxy配置 (两台机器相同)

cd /etc/haproxy/                 vim haproxy.cfg

globallog         127.0.0.1 local2chroot      /var/lib/haproxypidfile     /var/run/haproxy.pidmaxconn     4000user        haproxygroup       haproxydaemonstats socket /var/lib/haproxy/statsdefaultsmode                    tcp            log                     globaloption                  dontlognulloption                  redispatchretries                 3timeout http-request    10stimeout queue           1mtimeout connect         10stimeout client          1mtimeout server          1mtimeout http-keep-alive 10stimeout check           10smaxconn                 600      listen hive2 bind *:3308mode tcpbalance roundrobin maxconn 1024 server hive_1 172.20.3.102:10000 check inter 180000 rise 1 fall 2server hive_2 172.20.3.103:10000 check inter 180000 rise 1 fall 2listen stats                              mode httpbind :6677                          stats enablestats hide-version                    stats uri     /haproxyadmin?stats    stats realm   Haproxy\ Statistics    stats auth    admin:admin             stats hide-version         

3.2 keepalived 配置(主从配置)

(主节点   172.20.3.102)cd /etc/keepalived               vim /keepalived.cfg

! Configuration File for keepalivedglobal_defs {router_id 172.20.3.102}vrrp_script chk_haproxy {                           script "/etc/keepalived/check_haproxy.sh"interval 2weight 2
}vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111
}track_script {chk_haproxy
}
virtual_ipaddress {172.20.3.112
}
}

(从节点  172.20.3.103)cd /etc/keepalived               vim /keepalived.cfg

 Configuration File for keepalivedglobal_defs {router_id 172.20.3.103}vrrp_script chk_haproxy {                           script "/etc/keepalived/check_haproxy.sh"interval 2weight 2
}vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 51priority 90advert_int 1authentication {auth_type PASSauth_pass 1111
}track_script {chk_haproxy
}
virtual_ipaddress {172.20.3.112
}
}

vim check_haproxy.sh(两个节点相同)

sleep时间稍微久一点,haproxy启动需要点时间

#!/bin/bash
A=`ps -C haproxy --no-header | wc -l`
if [ $A -eq 0 ];then
/etc/init.d/haproxy startsleep 8
if [ `ps -C haproxy --no-header | wc -l ` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi

chmod +x /etc/keepalived/check_haproxy.sh

3.3 启动

systemctl daemon-reloadsystemctl enable haproxysystemctl enable keepalivedsystemctl start haproxysystemctl start keepalived#查看状态
systemctl status keepalivedsystemctl status haproxy

ip a查看keepalived主节点,可以看到虚拟ip在该节点上

haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)

使用虚拟ip,不用主从节点得ip来访问 haproxy

haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)

在check_haproxy.sh文件中,如果haproxy挂掉且没拉起来,主节点的keepalived也会停掉,此时虚拟ip会飘到从节点,由从节点的haproxy继续提供负载均衡服务

3.4 报错解决

3.4.1 cannot bind socket [0.0.0.0:3308]

设置haproxy连接策略   执行  setsebool -P haproxy_connect_any=1

3.4.2  Popt libraries is required 

执行 yum install popt-devel

 

  相关解决方案