haproxy2.0.0安装部署

服务版本说明

服务 版本号 备注
Haproxy haproxy-2.0.0

部署环境

环境centos7.5
Haproxy 2.0.0
主机名:haproxy1、haproxy2
IP:192.168.43.88、192.168.43.89

下载路径

http://download.openpkg.org/components/cache/haproxy/
https://src.fedoraproject.org/repo/pkgs/haproxy/

安装依赖项

[root@haproxy1 /]# yum -y install make gcc gcc-c++ openssl-devel

创建程序包存放路径

mkdir -p /data/software/ haproxy

上传haproxy2.0.0.tar.gz

上传并解压下载好的tar包
[root@haproxy1 /]# cd /data/software/haproxy
[root@haproxy1 haproxy]# rz -E
rz waiting to receive.
[root@haproxy haproxy]# tar -zxvf haproxy-2.0.0.tar.gz

查询内核以便修改安装文件

[root@haproxy haproxy]# cd haproxy-2.0.0
[root@haproxy haproxy-2.0.0]# uname -r
3.10.0-957.el7.x86_64
[root@haproxy haproxy-2.0.0]# make TARGET=linux3100 PREFIX=/opt/haproxy20 #设置安装路径 #方法一:直接赋值安装
[root@haproxy haproxy-2.0.0]# vi Makefile #方法二:直接修改安装文件再安装也可以
136 PREFIX = /opt/haproxy
146 TARGET =linux3100
[root@haproxy haproxy-2.0.0]# make && make install #安装haproxy
[root@haproxy1 sbin]# cp -rf /opt/haproxy/sbin/haproxy /usr/sbin/
[root@haproxy1 sbin]# haproxy -version
HA-Proxy version 2.0.0 2019/06/16 - https://haproxy.org/

创建用户、组

[root@haproxy local]# groupadd -g 1500 lwxh
[root@haproxy local]# useradd -u 1500 -g 1500 lwxh
[root@haproxy local]# id lwxh
uid=1500(lwxh) gid=1500(lwxh) groups=1500(lwxh)

编写haproxy配置文件

log 127.0.0.1 local0
maxconn 4096
chroot /opt/haproxy20
#uid 1500 == #所属运行的用户uid==
#gid 1500 #所属运行的用户组
user lwxh #使用uid也可以(看自己创建的用户和组是什么就改成什么)
group lwxh
daemon #以后台形式运行haproxy
nbproc 1 #启动1个haproxy实例。# #工作进程数量(CPU数量) ,实际工作中,应该设置成和CPU核心数一样。 这样可以发挥出最大的性能。
pidfile /opt/haproxy20/run/haproxy.pid #将所有进程写入pid文件
#debug #调试错误时用
#quiet #安静
defaults
log global
log 127.0.0.1 local3 #日志文件的输出定向。产生的日志级别为local3. 系统中local1-7,用户自己定义
mode http #工作模式,所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
option httplog #日志类别,记载http日志
option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
option dontlognull #不记录空连接,产生的日志
option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch #当serverid对应的服务器挂掉后,强制定向到其他健康服务器
retries 2 #2次连接失败就认为服务器不可用,主要通过后面的check检查
maxconn 2000 #最大连接数
balance roundrobin #负载均衡算法(轮循算法)
stats uri /haproxy-stats #haproxy 监控页面的访问地址#可通过 http://localhost:80/haproxy-stats 访问
timeout connect 5000 #连接超时时间。 单位:ms 毫秒
timeout client 50000 #客户端连接超时时间
timeout server 50000 #服务器端连接超时时间
mode http
option httpchk GET /index.html #健康检测#注意实际工作中测试时,应该下载某一个页面来进行测试,因此这个页面应该是个小页面,而不要用首页面。这里是每隔一秒检查一次页面。

frontend http #前端配置,http名称可自定义
bind 192.168.100.100:9091 #发起http请求80端口,会被转发到设置的ip及端口,当前设置为keepalived的虚拟IP(用户访问的IP是什么就设置为什么,端口可修改)
default_backend http_back #转发到后端 写上后端名称

backend http_back #后端配置,名称上下关联
server haproxy1 192.168.43.88:80 weight 3 check #后端的主机 IP &权衡(具体web页面的主机名(不是主机名也可以,主要用于日志打印,建议设置为主机名);ip及端口看怎么配置web服务器,http默认端口80,其他参数看需求查资料设置)
server haproxy2 192.168.43.89:80 weight 3 check #后端的主机 IP &权衡
# inter 2000 健康检查时间间隔2秒
# rise 3 检测多少次才认为是正常的
# fall 3 失败多少次才认为是不可用的
# weight 30 权重

设置启动项及编写启动脚本

[root@haproxy local]# cp -r /data/software/haproxy/haproxy-2.0.0/examples/haproxy.init /etc/init.d/haproxy
[root@haproxy local]# chmod 755 /etc/init.d/haproxy
[root@haproxy1 local]# vi /etc/init.d/haproxy #主要修改涉及的文件路径
#脚本仅供参考,最好是自己修改
[root@haproxy1 local]# cat /etc/init.d/haproxy
#!/bin/sh
#chkconfig: - 85 15
#description: HA-Proxy server
#processname: haproxy
#config: /opt/haproxy20/etc/haproxy.cfg
#pidfile: /opt/haproxy20/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=/opt/haproxy20/etc/haproxy.cfg
[ -f $CFG ] || exit 1

PIDFILE=/opt/haproxy20/run/haproxy.pid
LOCKFILE=/opt/haproxy20/run/haproxy

RETVAL=0

start() {
quiet_check
if [ ? − n e 0 ] ;

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值