haproxy1.7编译安装配置

时间:2023-03-10 02:11:59
haproxy1.7编译安装配置

#haproxy1.7编译安装配置
#高可用、负载均衡 使用

#haproxy1.7编译安装配置
#centos7 x86_64
#高可用、负载均衡 使用 #下载
#http://www.haproxy.org/download/1.7/src/haproxy-1.7.8.tar.gz
#cd /data/nfs/download
#yum install gcc gcc-c++ autoconf automake -y #安装haproxy
Hadir=/www/server/haproxy #安装目录
mkdir -p $Hadir
tar -axf haproxy-* && cd ./haproxy-*
make TARGET=linux2628 ARCH=x86_64 PREFIX=$Hadir
make install PREFIX=$Hadir
$Hadir/sbin/haproxy -v #增加用户
useradd -s /sbin/nologin -M haproxy
id haproxy #内核优化
# #NAT转发
# sed -i 's@net.ipv4.ip_forward = 0@net.ipv4.ip_forward = 1@g' /etc/sysctl.conf
# grep ip_forward /etc/sysctl.conf
echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf #允许没监听IP时启动
sysctl -p #启动脚本配置
cp ./examples/haproxy.init $Hadir/haproxy
chmod $Hadir/haproxy
sed -i '/^BIN=/cBIN='$Hadir'/sbin/$BASENAME' $Hadir/haproxy
sed -i '/^CFG=/cCFG='$Hadir'/$BASENAME.cfg' $Hadir/haproxy #日志配置
sed -i 's/^#$ModLoad imudp/$ModLoad imudp/g' /etc/rsyslog.conf
sed -i 's/^#$UDPServerRun 514/$UDPServerRun 514/g' /etc/rsyslog.conf
echo 'local0.* /var/log/haproxy.log'>>/etc/rsyslog.conf #添加haproxy日志路径
systemctl restart rsyslog #生成配置文件
echo " ###########全局配置#########
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
daemon
#nbproc #进程数量
maxconn #最大连接数
user haproxy #运行用户
group haproxy #运行组
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
########默认配置############
defaults
log global
mode http #默认模式{ tcp|http|health }
option httplog #日志类别,采用httplog
option dontlognull #不记录健康检查日志信息
retries #2次连接失败不可用
option forwardfor #后端服务获得真实ip
option httpclose #请求完毕后主动关闭http通道
option abortonclose #服务器负载很高,自动结束比较久的链接
maxconn #最大连接数
timeout connect 5m #连接超时
timeout client 1m #客户端超时
timeout server 31m #服务器超时
timeout check 10s #心跳检测超时
balance roundrobin #负载均衡方式,轮询
########统计页面配置########
listen stats
bind 0.0.0.0:
mode http
option httplog
log 127.0.0.1 local0 err
stats refresh 30s
maxconn #最大连接数
stats uri /admin #状态页面 http//ip:1080/admin访问
stats realm Haproxy\ Statistics
stats auth admin:admin #用户和密码:admin
stats hide-version #隐藏版本信息
stats admin if TRUE #设置手工启动/禁用 ########设置haproxy 错误页面#####
#errorfile /opt/haproxy/errorfiles/.http
#errorfile /opt/haproxy/errorfiles/.http
#errorfile /opt/haproxy/errorfiles/.http
errorloc https://www.baidu.com/
#errorfile /opt/errorfiles/.http ########frontend前端配置##############
frontend http_main
bind *:
option forwardfor
acl web hdr(host) -i elven.win #acl规则,-i忽略大小写,访问*就触发web规则
use_backend web1 if web acl web_kvm path_beg -i /kvm
use_backend kvm if web_kvm default_backend web1 #不满足则响应的默认页面 ########backend后端配置##############
backend web1 #www1作用域 service haproxy restart
cookie SERVERID
balance roundrobin
option httpchk HEAD /index.html HTTP/1.0
server web1 192.168.20.81: weight check inter rise fall
server web2 192.168.18.12: weight check inter rise fall backend kvm
server kvm1 127.0.0.1: ">$Hadir/haproxy.cfg
chown -R haproxy:haproxy $Hadir/ #启动
/www/server/haproxy/haproxy start
netstat -antp|grep haproxy
ps -ef|grep haproxy #添加自启动
ln -sf /www/server/haproxy/haproxy /etc/init.d/haproxy
chkconfig --add haproxy
chkconfig haproxy on
chkconfig --list haproxy
service haproxy restart #查看监控
http://ip:1080/admin
#cat /var/log/haproxy.log ##启动报错 第 行:[: =: 期待一元表达式,[]里的变量用加上引号“”
#修改为如下:
[ "${NETWORKING}" = "no" ] && exit