【运维】【haproxy+keepalived分布式高可用负载均衡下的nacos集群搭建】

1 篇文章 0 订阅
1 篇文章 0 订阅

一、haproxy安装及配置

1、 安装及配置

1.1 下载及解压

wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.2.tar.gz
tar -xzf haproxy-1.7.2.tar.gz

1.2 编译安装

### 对haproxy进行源码编译
make TARGET=linux31   # 需要进入 haproxy目录下执行,或者加上PREFIX=/usr/local/haproxy
#### 这里需要使用 cat /etc/redhat-release 查看系统版本centos6.X需要使用TARGET=linux26  centos7.x使用linux31

### 安装 编译好的haproxy 并指定安装目录
make install PREFIX=/usr/local/haproxy

#创建haproxy目录放置配置文件
mkdir /etc/haproxy

#赋权:添加用户组和用户,后面haproxy的配置中会用到
groupadd -r -g 149 haproxy
useradd -g haproxy -r -s /sbin/nologin -u 149 haproxy

1.3 配置haproxy

创建配置文件

touch /etc/haproxy/haproxy.cfg
vim /etc/haproxy/haproxy.cfg

配置参考

#logging options
global
        log 127.0.0.1 local0 info #日志输出配置,所有日志都记录在本机,通过local0输出
        maxconn 5120
        chroot /usr/local/haproxy #haproxy 安装路径
        uid 149 #所属运行的用户uid
        gid 149 #所属运行的用户组
        daemon  #后台运行
        quiet
        nbproc 20
        # pidfile /var/run/haproxy.pid #指定PID文件路径
  pidfile /usr/local/haproxy/run/haproxy.pid  #将所有进程写入pid文件

defaults
        log global

        #使用4层代理模式,"mode http"为7层代理模式
        mode http

        #if you set mode to tcp,then you nust change tcplog into httplog
        option httplog
        option dontlognull
        retries 3
        option redispatch
        maxconn 2000

        #连接超时时间
        timeout connect 5s
        #客户端空闲超时时间为 60秒 则HA 发起重连机制
        timeout client 60s
        #服务器端连接超时时间为 15秒 则HA 发起重连机制
        timeout server 15s

########################## 监控界面     ##########################
#配置haproxy web监控,查看统计信息
listen stats
        bind  0.0.0.0:7749 #前端浏览器中查看统计的WEB界面地址
        # mode http
        option httplog #日志类别,采用httplog
        stats enable
        stats auth admin:123456  #设置查看统计的账号密码
        #设置haproxy监控地址为http://localhost:50020/nacos-stats
        stats uri /nacos-stats
        stats refresh 5s  #5s刷新一次

########################## nacos集群负载均衡 ##########################
listen nacos_cluster
        bind 0.0.0.0:10011  #绑定协议端口
        #配置TCP模式
        #所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
        # mode tcp
        #balance url_param userid
        #balance url_param session_id check_post 64
        #balance hdr(User-Agent)
        #balance hdr(host)
        #balance hdr(Host) use_domain_only
        #balance rdp-cookie
        #balance leastconn
        #balance source //ip
        #简单的轮询
        balance roundrobin
        #负载均衡策略
        #rabbitmq集群节点配置 #inter 每隔五秒对mq集群做健康检查, 2次正确证明服务器可用,2次失败证明服务器不可用,并且配置主备机制
      server  nacos_155 192.xxx.155:10086 check inter 5000 rise 2 fall 2
      server  nacos_156 192.xxx.156:10086 check inter 5000 rise 2 fall 2
      server  nacos_157 192.xxx.157:10086 check inter 5000 rise 2 fall 2

复制haproxy文件到/usr/sbin下
因为上面的haproxy.init启动脚本默认会去/usr/sbin下找

[root@localhost ~]#cp /usr/local/haproxy/sbin/haproxy  /usr/sbin/

创建目录和权限

[root@localhost ~]# mkdir -p /usr/local/haproxy/run
[root@localhost ~]# chown nobody /usr/local/haproxy/ -R

配置日志收集
[root@localhost ~]# vim /etc/rsyslog.conf #打开以下两行的注释,不打开收不到日志
$ModLoad imudp #取消注释
$UDPServerRun 514 #取消注释
local7.* /var/log/boot.log #下面添加两行
local3.* /var/log/haproxy.log
local0.* /var/log/haproxy.log
[root@localhost ~]# systemctl restart rsyslog

1.4 创建haproxy启动脚本

cp ./haproxy-1.7.9/examples/haproxy.init  /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
vim /etc/init.d/haproxy   #修改后的脚本
#!/bin/sh
# chkconfig: - 85 15
# description: HA-Proxy server
# processname: haproxy
# config: /usr/local/haproxy/etc/haproxy.cfg
# pidfile: /usr/local/haproxy/run/haproxy.pid
 
# Source function library.
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
# This is our service name
BASENAME=`haproxy`
 
BIN=/usr/sbin/haproxy
 
CFG=/usr/local/haproxy/etc/haproxy.cfg
[ -f $CFG ] || exit 1
 
PIDFILE=/usr/local/haproxy/run/haproxy.pid
LOCKFILE=/usr/local/haproxy/run/haproxy
 
RETVAL=0
 
start() {
  quiet_check
  if [ $? -ne 0 ]; then
    echo "Errors found in configuration file, check it with '$BASENAME check'."
    return 1
  fi
 
  echo -n "Starting $BASENAME: "
  daemon $BIN -D -f $CFG -p $PIDFILE
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch $LOCKFILE
  return $RETVAL
}
 
stop() {
  echo -n "Shutting down $BASENAME: "
  killproc $BASENAME -USR1
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
  [ $RETVAL -eq 0 ] && rm -f $PIDFILE
  return $RETVAL
}
 
restart() {
  quiet_check
  if [ $? -ne 0 ]; then
    echo "Errors found in configuration file, check it with '$BASENAME check'."
    return 1
  fi
  stop
  start
}
 
reload() {
  if ! [ -s $PIDFILE ]; then
    return 0
  fi
 
  quiet_check
  if [ $? -ne 0 ]; then
    echo "Errors found in configuration file, check it with '$BASENAME check'."
    return 1
  fi
  $BIN -D -f $CFG -p $PIDFILE -sf $(cat $PIDFILE)
}
 
check() {
  $BIN -c -q -V -f $CFG
}
 
quiet_check() {
  $BIN -c -q -f $CFG
}
 
rhstatus() {
  status $BASENAME
}
 
condrestart() {
  [ -e $LOCKFILE ] && restart || :
}
 
# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    reload
    ;;
  condrestart)
    condrestart
    ;;
  status)
    rhstatus
    ;;
  check)
    check
    ;;
  *)
    echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status|check}"
    exit 1
esac
  
exit $?

2 启动服务

  • 常规启动
[root@localhost /]# /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
  • 通过脚本启动
[root@localhost ~]# /etc/init.d/haproxy start  或 systemctl  restart  haproxy
  • 停止
[root@localhost ~]# killall haproxy   #没有killall命令,安装yum -y install psmisc
  • 查看haproxy服务运行状态
[root@localhost /]# ps -ef | grep haproxy
haproxy   76881      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76882      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76883      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76884      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76885      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76886      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76887      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76888      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76889      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76890      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76891      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76892      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76893      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76894      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76895      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76896      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76897      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76898      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76899      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy   76900      1  0 09:47 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
root     101097 101096  0 11:02 ?        00:00:00 /bin/bash /etc/keepalived/haproxy_check.sh
root     101104  62894  0 11:02 pts/0    00:00:00 grep --color=auto haproxy

[root@localhost /]# netstat -antup | grep 10011 #当前配置的haproxy监听的nacos集群的端口号
tcp        0      0 0.0.0.0:10011           0.0.0.0:*               LISTEN      76881/haproxy

3、负载均衡测试

这里只演示一个简单的负载均衡效果测试

3.1 环境准备

  • 分别修改155、156、157这三台服务器上的nginx配置:nginx.conf
#增加如下配置
   server {
       listen       10086;
       server_name  localhost;

       location / {
           root   html;
           index  haproxy_test.html;
       }
   }
  • 在nginx安装目录下的 html目录中添加文件: haproxy_test.html
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <title>155_server</title>
</head>
<body>
<h1>155server!</h1>
</body>
</html>

配置完之后,重启nginx服务

  • 修改haproxy服务的配置
########################## 负载均衡测试 ##########################
listen test_haproxy
        bind 0.0.0.0:10011  #绑定协议端口
        #配置TCP模式
        #所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
        mode tcp
        #简单的轮询
        balance roundrobin
        #负载均衡策略
        #上面添加的nginx配置
      server  test_155 192.xxx.155:10086 check inter 5000 rise 2 fall 2
      server  test_156 192.xxx.156:10086 check inter 5000 rise 2 fall 2
      server  test_157 192.xxx.157:10086 check inter 5000 rise 2 fall 2

3.2 测试负载均衡

  • 分别访问每个服务器上的10086端口
http://192.xxx.155:10086
http://192.xxx.156:10086
http://192.xxx.157:10086

在这里插入图片描述

  • 启动155上的haproxy服务,访问155上的10011端口。测试haproxy负载均衡效果-轮询模式
    在这里插入图片描述

可以看到,只要刷新页面,haproxy就会根据配置的负载均衡策略(轮循模式)下将请求分发到不同的后端服务器

二、keepalived安装及配置

三、集群搭建

拓扑图如下:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值