0
点赞
收藏
分享

微信扫一扫

六月学习之Haproxy安装配置

2、Haproxy安装配置

2.1、使用系统rpm安装

Centos7默认的base仓库中包含haproxy的安装包文件,但是版本是1.5.18,比较旧
距离当前版本已经有较长时间没有更新,由于版本比较旧所以有很多功能不支持
yum install -y haproxy
haproxy -v

2.2、使用三方rpm安装

当然也可以通过第三方仓库安装较新的rpm包
haproxy  第三方rpm包"https://repo.ius.io/"

wget http://cdn.xuliangwei.com/haproxy22.rpm.tar.gz
tar xf haproxy22.rpm.tar.gz
yum localinstall haproxy/*.rpm -y
# 验证haproxy版本
haproxy -v
HA-Proxy version 2.2.9-a947cc2 2021/02/06

2.3、源码安装Haproxy

2.3.1、安装依赖环境

yum install gcc readline-devel openssl-devel systemd-devel -y

2.3.2、安装lua脚本

wget http://www.lua.org/ftp/lua-5.4.3.tar.gz
tar xf lua-5.4.3.tar.gz -C /usr/local/
cd /usr/local/lua-5.4.3/
make linux
make linux test
ln -s /usr/local/lua-5.4.3/ /usr/local/lua


2.3.3、编译Haproxy

wget http://cdn.xuliangwei.com/haproxy-2.4.0.tar.gz
tar xf haproxy-2.4.0.tar.gz
cd haproxy-2.4.0/
make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 \
USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 \
LUA_INC=/usr/local/lua/src LUA_LIB=/usr/local/lua/src
make install PREFIX=/usr/local/haproxy-2.4
ln -s /usr/local/haproxy-2.4/ /usr/local/haproxy
# 验证版本
/usr/local/haproxy/sbin/haproxy -v
HAProxy version 2.4.0-6cbbecf 2021/05/14 - https://haproxy.org/

2.3.4、创建启动文件

vim /usr/lib/systemd/system/haproxy24.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy24/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

2.3.5、创建配置文件

cat /etc/haproxy/haproxy.cfg
global
	maxconn 100000
	# uid 99
	# gid 99
	user haproxy
	group haproxy
	daemon
	log 127.0.0.1 local2 info
	pidfile /var/lib/haproxy24/haproxy.pid
	stats socket /var/lib/haproxy24/haproxy.sock mode 600 level admin

defaults
	option http-keep-alive
	option forwardfor
	maxconn 100000
	mode http
	timeout connect 300000ms
	timeout client 300000ms
	timeout server 300000ms

listen stats
	mode http
	bind 0.0.0.0:9999
	stats enable
	log global
	stats uri	/haproxy-status
	stats auth	admin:123456

listen web_port
	bind *:80
	mode http
	server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5

2.3.6、启动Haproxy

useradd -r haproxy
mkdir /var/lib/haproxy24

2.3.7、验证Haproxy

浏览器访问:`http://10.0.0.5:9999/haproxy-status`

2.4、Haproxy配置示例

对比Nginx负载均衡语法与haproxy负载均衡语法

1、nginx
server {} #前端监听对外端口
proxy_pass #将前端与后端建立关系
upstream {} #后端资源池

2、haproxy
frontend	#前端监听对外端口
use_backend	#将前端与后端建立关系,带条件的 location ~ \.php$
default_backend #默认建立关系(location /)
backend		    #后端资源池
listen        #frontend和backend的组合体(直接将前后端建立起来)
default        #提供统一的默认参数,如果定义了则使用自己的,没有定义则使用default的

2.4.1、场景配置示例1

1、监听在所有接口的80端口上HTTP proxy服务
1.1、将请求本机80端口的服务,都转发至webservers后端集群组
1.2、后端webservers资源池定义了172.16.1.7:80、172.16.1.8:80两台节点
1.2、调度方式采用轮询调度。
#nginx配置
upstream webservers {
	server 172.16.1.7:80;
	server 172.16.1.8:80;
}
server {
	listen 80;
	server_name proxy.qingchen.com;
	
	location / {
		proxy_pass http://webservers;
	}
}

#haproxy(frontend、default_backend、backend)
frontend proxy.qingchen.com *:80    #定义前端监听的端口
default_backend webservers          #所有请求调度至webservers集群

backend webservers                  #定义webservers集群名称
    balance roundrobin              #采用轮询调度算法
    server web1 172.16.1.7:80 check #定义节点信息 [名称 IP:端口] 检查
    server web2 172.16.1.8:80 check #定义节点信息 [名称 IP:端口] 检查

2.4.2、场景配置示例2

1、监听在所有接口的80端口上HTTP proxy服务
1.1、将请求本机80端口的服务,url为/的默认转发至webservers后端集群组
1.2、将请求本机80端口的服务,url为/1.png|/2.gif的转发至static后端集群组
1.3、后端webservers资源池定义了172.16.1.7:80、172.16.1.8:80两台节点
1.4、后端static资源池定义了172.16.1.9:80、172.16.1.10:80两台节点

#nginx
upstream webservers {
	server 172.16.1.7:80;
	server 172.16.1.8:80;
}
upstream static {
	server 172.16.1.9:80;
	server 172.16.1.10:80;
}
server {
	listen 80;
	server_name proxy.qingchen.com;
	
	location / {
		proxy_pass http://webservers;
	}
    location ~ \.(png|gif)$ {
		proxy_pass http://static;
	}
}

#haproxy(frontend、default_backend、use_backend、acl)
frontend proxy.qingchen.com *:80
    # location / {}
    default_backend webservers #url非png|gif结尾的,默认调度webservers集群
    # location ~ \.(png|gif)$ {}
    acl url_static path_end -i .gif .png  #定义url的acl规则
    use_backend static if url_static #调用static集群,url必须为.png|.gif结尾的
backend webservers
    balance roundrobin
    server web1 172.16.1.7:80 check
    server web2 172.16.1.8:80 check
backend static
    balance roundrobin
    server static1 172.16.1.9:80 check
    server static2 172.16.1.10:80 check

2.4.3、场景配置示例3

1、监听在所有接口的80端口上HTTP proxy服务:
1.1、将请求本机80端口的服务,直接代理至后端的172.16.1.11:80节点
1.2、无需采用负载均衡模式,与nginx代理配置大同小异

#nginx
server {
	listen 80;
	server_name proxy.qingchen.com;
	
	location / {
		proxy_pass http://172.16.1.11:80;
	}
}

#haproxy (listen)
frontend proxy.qingchen.com *:80
    server web1 172.16.1.11:80

2.4.4、官方示例配置

frontend main
    bind *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check

举报

相关推荐

0 条评论