CentOS   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了CentOS7.4下安装部署HAProxy高可用群集大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

目录 第一部分 实验环境 第二部分 搭建配置web服务器 第三部分 安装配置haproxy服务器 第四部分 测试验证 第五部分 haproxy配置相关详细解释 第一部分 实验环境 1.一台harpoxy调度服务器 IP地址:192.168.80.10 需要软件:haproxy-1.7.10.tar 2.两台Web服务器(基于nginx) IP地址:192.168.80.20(web01) IP地址

目录
第一部分 实验环境
第二部分 搭建配置web服务器
第三部分 安装配置haproxy服务器
第四部分 测试验证
第五部分 haproxy配置相关详细解释

第一部分 实验环境
1.一台harpoxy调度服务器
IP地址:192.168.80.10
需要软件:haproxy-1.7.10.tar
2.两台Web服务器(基于Nginx
IP地址:192.168.80.20(web01)
IP地址:192.168.80.30(web02)
需要软件:Nginx-1.13.9.tar.gz
//三台服务器系统:linux—CentOS7.4

CentOS7.4下安装部署HAProxy高可用群集

//软件:

CentOS7.4下安装部署HAProxy高可用群集

CentOS7.4下安装部署HAProxy高可用群集

3.Win7客户端一台(验证测试用)
IP地址:192.168.80.2

第二部分 搭建配置web服务器
第一步:配置web01
[[email protected] ~]# yum install -y \ //安装相关插件及编译安装工具

pcre-devel \
zlib-devel \@H_430_19@make \
gcc \
gcc-c++
[[email protected] ~]# useradd -M -s /sbin/logogin Nginx //创建Nginx程序用户
[[email protected] ~]# tar xzvf Nginx-1.13.9.tar.gz
[[email protected] ~]# cd Nginx-1.13.9
[[email protected] Nginx-1.13.9]# ./configure \ //定义配置
--prefix=/usr/local/Nginx \
--user=Nginx \
--group=Nginx
[[email protected] Nginx-1.13.9]# make && make install //编译及安装
[[email protected] Nginx-1.13.9]# ln -s /usr/local/Nginx/sbin/Nginx /usr/local/sbin/ //将Nginx可执行程序放到系统环境中
[[email protected] Nginx-1.13.9]# echo "<h1>SERVER AA</h1>" > /usr/local/Nginx/html/index.html
//修改认主页显示内容便于后面测试)
[[email protected] Nginx-1.13.9]# Nginx //启动Nginx服务
[[email protected] Nginx-1.13.9]# netstat -anpt | grep Nginx

CentOS7.4下安装部署HAProxy高可用群集

Win7访问http://192.168.80.10

CentOS7.4下安装部署HAProxy高可用群集

第二步:配置web02(与web01一样配置)
[[email protected] ~]# yum install -y \
pcre-devel \
zlib-devel \
gcc \
gcc-c++ \@H_430_19@make
[[email protected] ~]# useradd -M -s /sbin/nologin Nginx
[[email protected] ~]# tar xzvf Nginx-1.13.9.tar.gz
[[email protected] ~]# cd Nginx-1.13.9
[[email protected] Nginx-1.13.9]# ./configure \
--prefix=/usr/local/Nginx \
--user=Nginx \
--group=Nginx
[[email protected] Nginx-1.13.9]# make && make install
[[email protected] Nginx-1.13.9]# ln -s /usr/local/Nginx/sbin/Nginx /usr/local/sbin/
[[email protected] Nginx-1.13.9]# echo "<h1>SERVER BB</h1>" > /usr/local/Nginx/html/index.html
[[email protected] Nginx-1.13.9]# Nginx
[[email protected] Nginx-1.13.9]# netstat -anpt | grep Nginx

CentOS7.4下安装部署HAProxy高可用群集

Win7访问http://192.168.80.20

CentOS7.4下安装部署HAProxy高可用群集

第三部分 安装配置haproxy服务器
[[email protected] ~]# yum install -y \ //安装插件及编译工具

pcre-devel \
bzip2-devel \
gcc \
gcc-c++ \@H_430_19@make
[[email protected] ~]# tar xzvf haproxy-1.7.10.tar.gz
[[email protected] ~]# cd haproxy-1.7.10
[[email protected] haproxy-1.7.10]# make TARGET=linux26 //标识64位系统
[[email protected] haproxy-1.7.10]# make install
[[email protected] haproxy-1.7.10]# mkdir /etc/haproxy
[[email protected] haproxy-1.7.10]# groupadd haproxy
[[email protected] haproxy-1.7.10]# useradd -s /sbin/nologin -M -g haproxy haproxy //添加haproxy运行haproxy账号并设置及属主与属组
[[email protected] haproxy-1.7.10]# vi /etc/haproxy/haproxy.cfg //创建并编辑haproxy配置文件
--------------全局配置----------------
global
log 127.0.0.1 local2
#chroot /usr/local/haproxy-1.7.10
pidfile /var/run/haproxy.pid@H_430_19@maxconn 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@H_430_19@mode http //认模式,tcp是四层,http是七层,health只会返回OK,若是混合模式则mode不需要设置
log global //采用全局定义的日志
option dontlognull //不记录健康检查的日志信息
option httpclose //每次请求完毕后主动关闭http通道
option httplog //日志类别http日志格式;如果是混合模式,此处还需要加上tcpclog
#option forWARDfor //如果后端服务器需要获得客户端真实ip需要配置的参数,可以从http Header中获得客户端ip
option redispatch //serverId对应的服务器挂掉后,强制定向到其他健康的服务器
timeout connect 10s //超时连接10s
timeout client 10s //客户端超时连接10s
timeout server 10s //服务器连接超时@H_430_19@maxconn 60000 //最大连接数
retries 3 //3次连接失败就认为服务不可用
--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8089 //监听端口
stats enable //启用监听端口@H_430_19@mode http
log global
stats uri /stats //统计页面url
stats realm Haproxy\ Statistics //统计页面密码框上提示文本
stats auth admin:admin //统计页面用户名和密码设置
#stats hide-version //隐藏统计页面上HAProxy的版本信息
stats admin if TRUE //当通过认证才可管理
stats refresh 30s //页面自动刷新时间30s
---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:80@H_430_19@mode http
option httpchk GET /index.html
log global@H_430_19@maxconn 3000
balance roundrobin
server web01 192.168.80.10:80 check inter 2000 fall 3
server web02 192.168.80.20:80 check inter 2000 fall 3
保存退出
[[email protected] haproxy-1.7.10]# cp examples/haproxy.init /etc/init.d/haproxy
[[email protected] haproxy-1.7.10]# chmod 755 /etc/init.d/haproxy
[[email protected] haproxy-1.7.10]# chkconfig --add haproxy
[[email protected] haproxy-1.7.10]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[[email protected] haproxy-1.7.10]# service haproxy start
[[email protected] haproxy-1.7.10]# netstat -anpt | grep haproxy

CentOS7.4下安装部署HAProxy高可用群集

[[email protected] haproxy-1.7.10]# systemctl stop firewalld
[[email protected] haproxy-1.7.10]# setenforce 0

第四部分 验证测试
Win7访问调度器地址http://192.168.80.30

CentOS7.4下安装部署HAProxy高可用群集

等待一会,再次访问

CentOS7.4下安装部署HAProxy高可用群集

//验证成功

第五部分 haproxy配置相关详细解释#全局配置,用于设定义全局参数,属于进程级的配置,通常与操作系统配置有关.global#定义全局日志,配置在本地,通过local0 输出,认是info级别,可配置两条log 127.0.0.1 local0 warning#定义日志级别【error warning info debug】#log 127.0.0.1 local1 info#运行路径chroot /usr/local/haproxy#PID 文件存放路径pidfile /var/run/haproxy.pid#设置每haproxy进程的最大并发连接数,其等同于命令行选项“-n”; “ulimit -n”自动计算的结果参照此参数设定.maxconn 4096#运行haproxy 用户,或者使用关键字uiduser haproxy#运行haproxy 用户组,或者使用关键字gidgroup haproxy#后台运行haproxydaemon#设置启动的haproxy进程数量,只能用于守护进程模式的haproxy;#认只启动一个进程,鉴于调试困难等多方面的原因,一般只在单进程仅能打开少数文件描述符的场景中才使用多进程模式.nbproc 1#设置每进程所能够打开的最大文件描述符数目,认情况其会自动进行计算,因此不推荐修改此选项.#ulimit-n 819200#调试级别,一般只在开启单进程时调试,且生产环境禁用.#debug#haproxy启动后不会显示任何相关信息,这与在命令行启动haproxy时加上参数“-q”相同#quiet#定义统计信息保存位置stats socket /usr/local/haproxy/stats#认配置 defaults#认的模式【tcp:4层; http:7层; health:只返回OK】mode http#继承全局的日志定义输出log global#日志类别,httplog#option httplog#如果后端服务器需要记录客户端真实ip,需要在http请求中添加”X-ForWARDed-For”字段;#但haproxy自身的健康检测机制访问后端服务器时,不应将记录访问日志,可用except来排除127.0.0.0,即haproxy本身.#option forWARDfor except 127.0.0.0/8option forWARDfor#开启http协议中服务器端关闭功能,每个请求完毕后主动关闭http通道,使得支持长连接,使得会话可以被重用,使得每一个日志记录都会被记录.option httpclose#如果产生了一个空连接,那这个空连接的日志将不会记录.option dontlognull#当与后端服务器的会话失败(服务器故障或其他原因)时,把会话重新分发到其他健康的服务器上; 当故障服务器恢复时,会话又被定向到已恢复的服务器上;#还可以用”retries”关键字来设定在判定会话失败时的尝试连接的次数option redispatchretries 3#当haproxy负载很高时,自动结束掉当前队列处理比较久的链接.option abortonclose#http请求超时时间timeout http-request 10s#认队列超时时间,后端服务器在高负载时,会将haproxy发来的请求放进一个队列中.timeout queue 1m#haproxy与后端服务器连接超时时间.timeout connect 5s#客户端与haproxy连接后,数据传输完毕,不再有数据传输,即非活动连接的超时时间.timeout client 1m#haproxy与后端服务器非活动连接的超时时间.timeout server 1m#认新的http请求连接建立的超时时间,时间较短时可以尽快释放出资源,节约资源.timeout http-keep-alive 10s#心跳检测超时时间timeout check 10s#最大并发连接数maxconn 2000#设置认的负载均衡方式#balance source#balnace leastconn#统计页面配置,frontend和BACkend的组合体,监控组的名称可按需自定义listen admin_status#配置监控运行模式mode http#配置统计页面访问端口bind 0.0.0.0:1080#统计页面认最大连接数maxconn 10#http日志格式option httplog#开启统计stats enable#隐藏统计页面上的haproxy版本信息stats hide-version#监控页面自动刷新时间stats refresh 30s#统计页面访问urlstats uri /stats#统计页面密码框提示文本stats realm mCloud\ Haproxy#监控页面用户和密码:admin,可设置多个用户名stats auth admin:admin#手工启动/禁用后端服务器,可通过web管理节点stats admin if TRUE#设置haproxy错误页面errorfile 400 /usr/local/haproxy/errorfiles/400.httperrorfile 403 /usr/local/haproxy/errorfiles/403.httperrorfile 408 /usr/local/haproxy/errorfiles/408.httperrorfile 500 /usr/local/haproxy/errorfiles/500.httperrorfile 502 /usr/local/haproxy/errorfiles/502.httperrorfile 503 /usr/local/haproxy/errorfiles/503.httperrorfile 504 /usr/local/haproxy/errorfiles/504.http#监控haproxy后端服务器的监控状态listen site_statusbind 0.0.0.0:1081 #监听端口mode http #http的7层模式log 127.0.0.1 local2 err #[err warning info debug]monitor-uri /site_status #网站健康检测URL,用来检测HAProxy管理的网站是否可以用,正常返回200,不正常返回503acl site_dead nbsrv(PHP_server) lt 1 #定义网站down时的策略当挂在负载均衡上的指定BACkend的中有效机器数小于1台时返回trueacl site_dead nbsrv(html_server) lt 1acl site_dead nbsrv(BACkend_default) lt 1 monitor fail if site_dead #当满足策略的时候返回503,网上文档说的是500,实际测试为503monitor-net 192.168.4.171/32 #来自192.168.4.152的日志信息不会被记录和转发monitor-net 192.168.4.172/32#frontend,名字自定义frontend HAproxy_Cluster#定义前端监听端口,建议采用bind :80的形式,否则做集群高可用的时候有问题,vip切换到其余机器就不能访问.bind 0.0.0.0:80#acl后面是规则名称,当请求的url末尾是以.PHP结尾时,匹配触发PHP_web规则,以下两种写法均可.#当请求的url末尾是以.css、.jpg、.png、.jpeg、.js、.gif结尾时,匹配并触发static_web规则.#acl static_web path_end .gif .png .jpg .css .js .jpeg#acl static_web url_reg /.(css|jpg|png|jpeg|js|gif)$#-i为忽略大小写,当被请求的是以www.test.com开头的主机时,匹配并触发dns_name规则.acl html_web hdr_beg(host) -i www.haproxytest.com#acl html_web hdr_beg(host) 10.11.4.152#当客户端的IP是x.x.x.x时,匹配并触发src_ip规则.#acl src_ip src x.x.x.x#如果匹配acl规则PHP_web,将请求转交到PHP_server组处理;如果匹配acl规则html_web,将请求转交到html_server组处理.use_BACkend PHP_server if PHP_webuse_BACkend html_server if html_web#如果以上规则都不匹配时,将请求转交到defaulT_Backend组处理.defaulT_Backend BACkend_default#BACkend后端配置,配置PHP_server组与html_server组BACkend PHP_server#定义负载均衡方式为roundrobin方式,即基于权重进行轮询调度的算法,在服务器性能分布较均匀情况下推荐.#另有如下几种负载均衡方式:#-- static-rr: 也是基于权重进行轮转调度,但属于静态方法,运行时调整后端机组权重不会使用新的权重;#-- source: 基于请求源IP进行hash运算匹配后端服务器组;#-- leastconn: 不适合会话较短的环境,如基于http的应用;#-- uri: 对整个URI进行hash运算;#-- uri_param: 对URI中的参数进行转发;#-- hdr(<name>):根据http头进行转发,无该头部则转为使用roundrobin.balance roundrobinmode http#允许插入serverid到cookie中,serverid后面可定义cookie SERVERID#心跳检测方式为检测后端服务器index.html文件,还有其他方式option httpchk GET /index.html#后端服务器定义,maxconn 1024表示该服务器的最大连接数,cookie 1表示serverid为1,weight代表权重(认1,最大为265,0则表示不参与负载均衡),#check inter 1500是检测心跳频率,rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用.server PHP1 192.168.4.171:80 maxconn 1024 cookie 1 weight 3 check inter 1500 rise 2 fall 3BACkend html_serverbalance sourcemode httpserver html1 192.168.4.172:80 maxconn 1024 cookie 1 weight 3 check inter 1500 rise 2 fall 3BACkend BACkend_defaultbalance sourcemode httpserver default1 192.168.4.171:80 maxconn 1024 cookie 1 weight 3 check inter 1500 rise 2 fall 3

大佬总结

以上是大佬教程为你收集整理的CentOS7.4下安装部署HAProxy高可用群集全部内容,希望文章能够帮你解决CentOS7.4下安装部署HAProxy高可用群集所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。