haproxy安装

简介:

HA-Proxy version 1.7.2

CentOS release 6.8 (Final)


  1. 关闭selinux,清空防火墙规则

1
2
3
4
5
6
7
8
9
vim  /etc/selinux/config
SELINUX=disabled 
 
:wq   #保存退出
 
setenforce 0   #使配置立即生效
 
iptables -F    #清空防火墙规则
service iptables save

2.安装HAProxy


  1. 创建haproxy运行账户

    useradd -s /sbin/nologin haproxy

2.安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cd  /usr/local/src
 
tar  zxf haproxy-1.7.2. tar .gz
 
cd  haproxy-1.7.2
 
less  README
 
make  TARGET=linux2628 PREFIX= /usr/local/haproxy
 
make  install  PREFIX= /usr/local/haproxy
 
#参数说明:
#TARGET=linux2628 使用uname -a 或-r 可以查看系统内核版本号
#kernel 大于2.6.28的用TARGET=linux26228
#/usr/local/haproxy 为安装路径


3.设置haproxy


mkdir -p /usr/local/haproxy/conf  #创建配置文件目录

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

touch /usr/local/haproxy/conf/haproxy.cfg  #创建配置文件

ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg

#添加配置语言的软链接

cp -r /usr/local/src/haproxy-1.7.2/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/haprosy.log

#创建日志文

ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log

#添加软连接

cp -r /usr/local/src/haproxy-1.7.2/examples/haproxy.init /etc/init.d/haproxy 

#复制开机启动脚本文件

chmod +x /etc/init.d/haproxy

#添加脚本执行权限

ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin #添加软连接


4.配置haproxy.cfg参数

vim /usr/local/haproxy/conf/haproxy.cfg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
     log    127.0.0.1 local3           ###[err warning info debug] 
     chroot   /usr/local/haproxy
     pidfile   /var/run/haproxy .pid    ###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件 
     maxconn  4000                    ###最大连接数,默认4000
     user   haproxy
     group   haproxy
     daemon                           ###创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon"
  
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will 
# use if not designated in their block
#---------------------------------------------------------------------
defaults
     mode   http              ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
     log    global            ###采用全局定义的日志
     option  dontlognull      ###不记录健康检查的日志信息
     option  httpclose        ###每次请求完毕后主动关闭http通道 
     option  httplog          ###日志类别http日志格式 
     option  forwardfor       ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip  
     option  redispatch       ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器
     timeout connect 10000    #default 10 second timeout if a backend is not found
     timeout client 300000    ###客户端连接超时
     timeout server 300000    ###服务器连接超时
     maxconn     60000        ###最大连接数
     retries     3            ###3次连接失败就认为服务不可用,也可以通过后面设置 
####################################################################
listen stats
         bind 0.0.0.0:1080            #监听端口  
         stats refresh 30s            #统计页面自动刷新时间  
         stats uri  /stats             #统计页面url  
         stats realm Haproxy Manager  #统计页面密码框上提示文本  
         stats auth admin:admin       #统计页面用户名和密码设置  
         #stats hide-version         #隐藏统计页面上HAProxy的版本信息
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
     bind 0.0.0.0:80
     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      ###满足策略要求,则响应策略定义的backend页面
     default_backend   dynamic             ###不满足则响应backend的默认页面
  
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
  
backend static
     balance     roundrobin                  ###负载均衡模式轮询
     server      static 127.0.0.1:80 check  ###后端服务器定义
      
backend dynamic
     balance    roundrobin
     server         websrv1 10.252.97.106:80 check maxconn 2000
     server         websrv2 10.117.8.20:80 check maxconn 2000
  
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
 
errorfile 403  /etc/haproxy/errorfiles/403 .http
errorfile 500  /etc/haproxy/errorfiles/500 .http
errorfile 502  /etc/haproxy/errorfiles/502 .http
errorfile 503  /etc/haproxy/errorfiles/503 .http
errorfile 504  /etc/haproxy/errorfiles/504 .http


/etc/init.d/haproxy start


5.设置haproxy日志

vim /etc/rsyslog.conf

添加

1
2
local7.*                                                 /var/log/boot .log
local3.*                                              /var/log/haproxy .log

保存后重启服务

service rsyslog restart


5.浏览器打开haproxy的监控页面

输入http://192.168.141.188:1080/stats

#说明:1080是haproxy配置文件中监听的端口,stats是配置文件中的监听名称

用户名密码是admin:admin也是配置文件中定义的





      本文转自limingyu0312  51CTO博客,原文链接:http://blog.51cto.com/limingyu/1897441,如需转载请自行联系原作者



相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
7月前
|
负载均衡 Ubuntu Linux
haproxy简明配置
简介介绍haproxy的配置方法
104 1
|
4月前
|
负载均衡 监控 算法
HAproxy
HAProxy 是一个高性能的负载均衡软件,可以将客户端的请求均衡地分发给多个后端服务器。HAProxy支持多种负载均衡算法,并提供灵活的配置选项。与LVS相比,HAProxy更加灵活和高级,可以进行更复杂的负载均衡策略和应用层的请求转发。通常,HAProxy可以与Keepalived结合使用,以提供高可用性和负载均衡的解决方案。
78 5
|
9月前
|
应用服务中间件 数据安全/隐私保护 nginx
Haproxy-安装与配置
安装 Haproxy
68 0
|
负载均衡 Linux
HaProxy 安装
HaProxy 安装
|
缓存 算法 网络协议
haproxy
监听所有80端口转发到8080 global daemon maxconn 25600 defaults mode http timeout connect 5000ms timeout client 50000ms timeout server 50...
1275 0
|
网络协议 算法 应用服务中间件
haproxy 介绍
HAproxy均衡负载部署和配置文件详解 HAproxy均衡负载部署和配置文件详解 HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。
2317 0
|
负载均衡 数据安全/隐私保护 网络协议
|
Web App开发 网络协议 应用服务中间件
|
监控 负载均衡 网络协议
|
监控 负载均衡 算法