CentOS 6.4上HAProxy-1.4.24安装配置

简介:

HAProxy是一款免费、快速并且可靠的一种代理解决方案,支持高可用性、负载均衡特性,同时适用于做基于TCP和HTTP的应用的代理。对于一些负载较大的Web站点,使用HAProxy特别合适。HAProxy能够支撑数以万计的并发连接。它的配置简单,能够很容易整合大我们现有的应用架构之中。
下面,我们在CentOS 6.4上进行安装配置HAProxy。

安装配置

按照如下步骤进行安装:

1 wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
2 tar xvzf haproxy-1.4.24.tar.gz
3 cd haproxy-1.4.24
4 make TARGET=linux26
5 make install

默认安装,HAProxy对应的配置文件的存放路径为/etc/haproxy/haproxy.cfg。
我们看一下,默认安装的配置文件内容,如下所示:

01 #---------------------------------------------------------------------
02 # Example configuration for a possible web application. See the
03 # full configuration options online.
04 #
05 # http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
06 #
07 #---------------------------------------------------------------------
08
09 #---------------------------------------------------------------------
10 # Global settings
11 #---------------------------------------------------------------------
12 global
13 # to have these messages end up in /var/log/haproxy.log you will
14 # need to:
15 #
16 # 1) configure syslog to accept network log events. This is done
17 # by adding the '-r' option to the SYSLOGD_OPTIONS in
18 # /etc/sysconfig/syslog
19 #
20 # 2) configure local2 events to go to the /var/log/haproxy.log
21 # file. A line like the following can be added to
22 # /etc/sysconfig/syslog
23 #
24 # local2.* /var/log/haproxy.log
25 #
26 log 127.0.0.1 local2
27
28 chroot /var/lib/haproxy
29 pidfile /var/run/haproxy.pid
30 maxconn 4000
31 user haproxy
32 group haproxy
33 daemon
34
35 # turn on stats unix socket
36 stats socket /var/lib/haproxy/stats
37
38 #---------------------------------------------------------------------
39 # common defaults that all the 'listen' and 'backend' sections will
40 # use if not designated in their block
41 #---------------------------------------------------------------------
42 defaults
43 mode http
44 log global
45 option httplog
46 option dontlognull
47 option http-server-close
48 option forwardfor except 127.0.0.0/8
49 option redispatch
50 retries 3
51 timeout http-request 10s
52 timeout queue 1m
53 timeout connect 10s
54 timeout client 1m
55 timeout server 1m
56 timeout http-keep-alive 10s
57 timeout check 10s
58 maxconn 3000
59
60 #---------------------------------------------------------------------
61 # main frontend which proxys to the backends
62 #---------------------------------------------------------------------
63 frontend main *:5000
64 acl url_static path_beg -i /static /images /javascript /stylesheets
65 acl url_static path_end -i .jpg .gif .png .css .js
66
67 use_backend static if url_static
68 default_backend app
69
70 #---------------------------------------------------------------------
71 # static backend for serving up images, stylesheets and such
72 #---------------------------------------------------------------------
73 backend static
74 balance roundrobin
75 server static 127.0.0.1:4331 check
76
77 #---------------------------------------------------------------------
78 # round robin balancing between the various backends
79 #---------------------------------------------------------------------
80 backend app
81 balance roundrobin
82 server app1 127.0.0.1:5001 check
83 server app2 127.0.0.1:5002 check
84 server app3 127.0.0.1:5003 check
85 server app4 127.0.0.1:5004 check

我们对上面配置文件的内容,适当的扩展,做简单的解释:

  • global段

global段用于配置进程级的参数。官网文档基于参数的功能,将global配置参数分为3组:

  1. 进程管理和安全
  2. 性能调优
  3. 调试

具体内容可以参考文档详细介绍。

  • defaults段

defaults段主要是代理配置的默认配置段,设置默认参数,这些默认的配置可以在后面配置的其他段中使用。如果其他段中想修改默认的配置参数,只需要覆盖defaults段中的出现配置项内容。
关于defaults段可以配置的参数,可以参考官网文档的详细介绍。

  • frontend段

frontend段主要配置前端监听的Socket相关的属性,也就是接收请求链接的虚拟节点。这里除了配置这些静态的属性,还可以根据一定的规则,将请求重定向到配置的backend上,backend可能配置的是一个服务器,也可能是一组服务器(集群)。

  • backend段

backend段主要是配置的实际服务器的信息,通过frontend配置的重定向请求,转发到backend配置的服务器上。

  • listen段

listen段是将frontend和backend这两段整合在一起,直接将请求从代理转发到实际的后端服务器上。

启动HAProxy代理

启动非常简单,执行如下命令即可:

1 sudo haproxy -f /etc/haproxy/haproxy.cfg

我们简单修改一下配置文件内容,配置一个用来均衡后端SolrCloud搜索集群服务器,如下所示:

01 #---------------------------------------------------------------------
02 # Global settings
03 #---------------------------------------------------------------------
04 global
05 log 127.0.0.1 local2
06
07 chroot /var/lib/haproxy
08 pidfile /var/run/haproxy.pid
09 maxconn 4000
10 user haproxy
11 group haproxy
12 daemon
13
14 # turn on stats unix socket
15 stats socket /var/lib/haproxy/stats
16
17 #---------------------------------------------------------------------
18 # common defaults that all the 'listen' and 'backend' sections will
19 # use if not designated in their block
20 #---------------------------------------------------------------------
21 defaults
22 mode http
23 log global
24 option httplog
25 option dontlognull
26 option http-server-close
27 option forwardfor except 127.0.0.0/8
28 option redispatch
29 retries 3
30 timeout http-request 10s
31 timeout queue 1m
32 timeout connect 10s
33 timeout client 1m
34 timeout server 1m
35 timeout http-keep-alive 10s
36 timeout check 10s
37 maxconn 3000
38
39 #---------------------------------------------------------------------
40 # main frontend which proxys to the backends
41 #---------------------------------------------------------------------
42 frontend haproxy-lbserver
43 bind 0.0.0.0:80
44 acl url_solr_path path_beg /solr-cloud
45 acl url_static path_beg -i /static /images /javascript /stylesheets
46 acl url_static path_end -i .jpg .gif .png .css .js
47
48 use_backend static if url_static
49 use_backend solr-cloud if url_solr_path
50 default_backend static
51
52 #---------------------------------------------------------------------
53 # static backend for serving up images, stylesheets and such
54 #---------------------------------------------------------------------
55 backend static
56 balance roundrobin
57 server static 127.0.0.1:4331 check
58
59 #---------------------------------------------------------------------
60 # round robin balancing between the various backends
61 #---------------------------------------------------------------------
62 backend solr-cloud
63 balance roundrobin
64 server solr-1 slave1:8888 check
65 server solr-2 slave2:8888 check
66 server solr-3 slave3:8888 check
67 server solr-4 slave4:8888 check

frontend的名称为haproxy-lbserver,实际上映射为具体服务IP地址,绑定到80端口,然后请求Path设置为/solr-cloud,也就是前端接收到类似以“http://haproxy-lbserver/solr-cloud”开始的链接,后面可以加上具体的其他请求参数。
在frontend中使用use_backend指令指定了一个转发至的backend,名称为solr-cloud,可以在use_backend指令后面使用过滤条件指令if来指定转发的backend名称。
backend中指定了实际集群服务器的配置,对其进行负载均衡,一共指定了4台Solr搜索服务器,使用roundrobin负载均衡策略。
我们将默认配置文件拷贝到目录/home/hadoop/shiyanjun/haproxy-1.4.24/conf下面,然后启动haproxy:

1 sudo haproxy -f /home/hadoop/shiyanjun/haproxy-1.4.24/conf/haproxy.cfg

启动成功以后,可以访问类似如下的请求链接:

1 http://haproxy-lbserver/solr-cloud/mycollection/select?q=北京&fl=*&fq=building_type:1&start=0&rows=10

HAProxy会将请求转发至backend端的集群服务器上去,执行实际的请求处理。

HAProxy的官网文档相当详细,推荐参考官网文档,了解对应的配置选项和使用方法。

目录
相关文章
|
3天前
|
网络安全 开发工具
Centos6.5安装配置autofs服务
配置IP地址和挂载yum源后,安装autofs:`yum -y install autofs`。编辑配置文件 `/etc/auto.misc` 和 `/etc/auto.master`,示例中展示了将yum源路径添加到auto.master。关闭防火墙并重启autofs服务以应用更改。
25 2
|
3天前
|
Java Linux
为centos7配置jdk
为centos7配置jdk
28 3
|
3天前
|
网络协议 安全 Linux
linux配置防火墙 Centos7下 添加 端口白名单
linux配置防火墙 Centos7下 添加 端口白名单
104 0
|
3天前
|
Linux 网络安全 开发工具
Centos6.5安装并配置Telnet服务
该内容是一个关于如何安装配置Telnet服务的教程。首先,通过yum安装vim、xinetd、telnet和telnet-server。接着,修改/etc/xinetd.d/telnet配置文件,将disable改为no,并设置访问限制(如限定特定网段和时间)。关闭防火墙,重启服务。创建测试用户后,分别使用CentOS和Windows的Telnet客户端进行连接测试,显示成功,实验完成。
30 1
|
3天前
|
NoSQL Linux Redis
在CentOS上安装和配置Redis
在CentOS上安装和配置Redis
42 0
|
3天前
|
Linux
CentOS 7 配置yum阿里源 (三步即可)
CentOS 7 配置yum阿里源 (三步即可)
165 1
|
3天前
|
网络协议 Java 应用服务中间件
记录_centos7离线环境和虚拟机共享文件安装jdk和tomcat(配置环境变量)
记录_centos7离线环境和虚拟机共享文件安装jdk和tomcat(配置环境变量)
11 0
|
3天前
|
Linux 网络安全 开发工具
Centos7 sendmail服务安装与配置
该文本描述了在Linux系统中设置邮件服务的步骤。首先,启用httpd的邮件发送功能,然后安装sendmail、sendmail-cf和dovecot。接着配置/sendmail.mc,设定IP和邮件域名。在dovecot配置文件中启用imap、pop3和lmtp协议,取消明文认证限制,设定mail_location,并开启SSL。创建用户mail3和mail4,给予相应权限。停止postfix服务,编辑访问控制、提交配置、本地主机名等文件。最后,重置sendmail、dovecot和saslauthd服务。
56 0
|
3天前
|
运维 网络协议 Linux
【运维系列】Centos7安装并配置PXE服务
PXE是Intel开发的预启动执行环境,允许工作站通过网络从远程服务器启动操作系统。它依赖DHCP分配IP,DNS服务分配主机名,TFTP提供引导程序,HTTP/FTP/NFS提供安装源。要部署PXE服务器,需关闭selinux和防火墙,安装dhcpd、httpd、tftp、xinetd及相关服务,配置引导文件和Centos7安装源。最后,通过syslinux安装引导文件,并创建pxelinux.cfg/default配置文件来定义启动参数。
61 0
|
3天前
|
运维 网络协议 Linux
【运维系列】Centos7安装并配置postfix服务
安装CentOS7的Postfix和Dovecot,配置Postfix的`main.cf`文件,包括修改完全域名、允许所有IP、启用邮箱等。然后,配置Dovecot的多个配置文件以启用auth服务和调整相关设置。重启Postfix和Dovecot,设置开机自启,并关闭防火墙进行测试。最后,创建邮箱账户并在Windows邮箱客户端中添加账户设置。
23 0

热门文章

最新文章