我是靠谱客的博主 紧张玉米,最近开发中收集的这篇文章主要介绍haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)1. 准备工作2. Haproxy安装(源码安装)3.centos7 yum安装Haproxy / keepalived (两台机器),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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是 安装路径

ldd haproxy | grep ssl

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 具体参数

##复制的时候需要把 #注释的去掉 


global

    log         127.0.0.1 local2

    chroot      /usr/local/haproxy   #haproxy安装目录
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

defaults
    mode                    tcp            
    log                     global
    option                  dontlognull
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 600      


listen hive2 *:3308
    mode tcp
    balance roundrobin 
    maxconn 1024 
    server hive_1 172.20.3.102:10000 check inter 180000 rise 1 fall 2
    server hive_2 172.20.3.103:10000 check inter 180000 rise 1 fall 2
	

listen stats                              
    mode http
    bind :6677                          
    stats enable
    stats 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   #查看服务状态

web页面查看

http://172.20.3.102:6677/haproxyadmin?stats

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

global

    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    stats socket /var/lib/haproxy/stats

defaults
    mode                    tcp            
    log                     global
    option                  dontlognull
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 600      


listen hive2 
	bind *:3308
    mode tcp
    balance roundrobin 
    maxconn 1024 
    server hive_1 172.20.3.102:10000 check inter 180000 rise 1 fall 2
    server hive_2 172.20.3.103:10000 check inter 180000 rise 1 fall 2
	

listen stats                              
    mode http
    bind :6677                          
    stats enable
    stats 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 keepalived

global_defs {
  
router_id 172.20.3.102

}
  
vrrp_script chk_haproxy {                           
  script "/etc/keepalived/check_haproxy.sh"
  interval 2
  weight 2
}
  
vrrp_instance VI_1 {
  state MASTER
  interface eth0
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
    auth_type PASS
    auth_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 keepalived

global_defs {

router_id 172.20.3.103

}
  
vrrp_script chk_haproxy {                           
  script "/etc/keepalived/check_haproxy.sh"
  interval 2
  weight 2
}
  
vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 90
  advert_int 1
  authentication {
    auth_type PASS
    auth_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 start


sleep 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-reload

systemctl enable haproxy

systemctl enable keepalived

systemctl start haproxy

systemctl start keepalived

#查看状态
systemctl status keepalived

systemctl status haproxy

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

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

在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

 

最后

以上就是紧张玉米为你收集整理的haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)1. 准备工作2. Haproxy安装(源码安装)3.centos7 yum安装Haproxy / keepalived (两台机器)的全部内容,希望文章能够帮你解决haproxy+keepalived实现hiveserver2负载均衡(源码和yum安装haproxy+keepalived)1. 准备工作2. Haproxy安装(源码安装)3.centos7 yum安装Haproxy / keepalived (两台机器)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部