haproxy安装和配置超详细步骤(全部流程)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

haproxy安装和配置步骤(全部流程)

此文档是根据网上查找的资料,并通过测试成功配置haproxy负载均衡器整理出来的,这里我将所有操作步骤一步步列出来,这样可以节省大家很多的时间,而且最主要的是非常适合刚开始接触linux系统的初学者,即使什么都不懂也可以完成配置。

部署在centos 6.5系统

软件版本haproxy-1.5.15(1.3.20也可以用过)

假设公网ip地址10.10.10.10

本人都是以管理员身份进行安装。

下载软件

wget /download/1.5/src/haproxy-1.5.15.tar.gz

解压

tarzxf haproxy-1.5.15.tar.g

进入文件夹后开始安装

cd haproxy-1.5.15

make TARGET=linux26 PREFIX=/usr/local/haproxy

#上一条命令如果提示没有gcc编译器,使用下述语句进行安装

yum install ncurses-develgccgcc-c++ make rpm-build

make install PREFIX=/usr/local/haproxy

cd /usr/local/haproxy/

mkdirconf

mkdir logs

cd conf #配置文件放在此目录里

以上命令万一有看不懂的哈哈,请上网查一下。

#创建配置文件

vimhaproxy.cfg

#如果提示没有vim ,使用下述语句进行安装

yum install vim

复制以下haproxy.cfg配置文件里的代码(可以在windows里创建好文件,用记事本打开编辑好了上传到/usr/local/haproxy/conf文件夹里)

global

log 127.0.0.1 local0

log 127.0.0.1 local3

#log 127.0.0.1 local1 notice

maxconn 60000

chroot /usr/local/haproxy

uid 501

gid 501

daemon

pidfile /usr/local/haproxy/logs/haproxy.pid

defaults

log global

optionhttplog

mode http

balance source

optiondontlognull

option http-server-close

optionforwardfor except 127.0.0.1

optionredispatch

retries 3

optionredispatch

maxconn 60000

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 60000

#listen admin_stats

#bind 0.0.0.0:80

#mode http

#option httplog

#maxconn 10

stats refresh 30s

stats uri /haproxy-stats#注释:运行状态、后端服务器监控地址,上面说公网ip地址10.10.10.10那在浏览器里打开网址10.10.10.10/haproxy-stats,输入下面设置帐号密码,即可查看

stats auth帐号:密码#注释:这里自己设置帐号和密码,

stats hide-version

#注释:本人用到acl规则可以一台haproxy分别处理不同的域名

frontendweblb

bind *:80

acl w2017hdr_reg(host) -i ^(||)$

acl w2016_web hdr_reg(host) -i

#注释:这w2017,www2017就是随便命名的而已别乱就行。

use_backend www2017 if w2017

use_backend www2016 if w2016

mode http

optionhttplog

backend www2017

balance source

optionhttpchk

#注释:下面是后端服务器地址,端口号可以不用是80,注意防火墙要放行端口号,端口号后面是权重,健康检测等等。

server web1123.11.23.13:12345 weight 1 check inter 2000 rise 2 fall 5

server web221.11.23.12:80 weight 1 check inter 2000 rise 2 fall 5

backend www2016

balance source

optionhttpchk

server web2016123.11.23.2:8888 weight 1 check inter 2000 rise 2 fall 5

server web20162 22.90.123.12:7777 weight 1 check inter 2000 rise 2 fall 5

然后按esc键再输入:wq保存退出,有不知道的?详细到每一个按键~~~

#启动haprox

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg&

#重启haproxy

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -st `cat

/usr/local/haproxy/logs/haproxy.pid`

#停止haproxy

killallhaproxy

创建haproxy启动脚本(注意此文件没有扩展名就是haproxy)

放在目录/etc/init.d/下

vim /etc/init.d/haproxy

复制下面代码

#!/bin/bash

#

# haproxy

#

# chkconfig: 35 85 15

# description: HAProxy is a free, very fast and reliable solution \

# offering high availability, load balancing, and \

# proxying for TCP and HTTP-based applications

# processname: haproxy

# config: /etc/haproxy.cfg

# pidfile: /var/run/haproxy.pid

相关文档
最新文档