这篇文章主要介绍“在流复制主备环境下怎么使用HAProxy搭建负载均衡环境”,在日常操作中,相信很多人在在流复制主备环境下怎么使用HAProxy搭建负载均衡环境问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”在流复制主备环境下怎么使用HAProxy搭建负载均衡环境”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

安装
使用yum安装相关软件,在数据库服务器上安装xinted和telnet

yum-yinstallxinetdtelnet

配置xinetd
编写识别主从库的脚本pgsqlchk

[pg12@localhost~]$catpgsqlchk#!/bin/bash#Thisscriptchecksifapostgresserverishealthyrunningonlocalhost.Itwillreturn:#"HTTP/1.x200OK\r"(ifpostgresisrunningsmoothly)#-OR-#"HTTP/1.x500InternalServerError\r"(else)#Thepurposeofthisscriptismakehaproxycapableofmonitoringpostgresproperly#Itisrecommendedthatalow-privilegedpostgresuseriscreatedtobeusedbythisscript.#Foreg.createuserhealthchkusrloginpassword'hc321';PGBIN=/appdb/pg12/pg12.0/binPGSQL_HOST="localhost"PGSQL_PORT="5432"PGSQL_DATABASE="testdb"PGSQL_USERNAME="pg12"exportPGPASSWORD="root"TMP_FILE="/tmp/pgsqlchk.out"ERR_FILE="/tmp/pgsqlchk.err"#WeperformasimplequerythatshouldreturnafewresultsVALUE=`$PGBIN/psql-qt-hlocalhost-Upg12-p5432-c"selectpg_is_in_recovery()"2>/dev/null`#Checktheoutput.Ifitisnotemptytheneverythingisfineandwereturnsomething.Else,wejustdonotreturnanything.if[$VALUE=="t"]then/bin/echo-e"HTTP/1.1206OK\r\n"/bin/echo-e"Content-Type:Content-Type:text/plain\r\n"/bin/echo-e"\r\n"/bin/echo"Standby"/bin/echo-e"\r\n"elif[$VALUE=="f"]then/bin/echo-e"HTTP/1.1200OK\r\n"/bin/echo-e"Content-Type:Content-Type:text/plain\r\n"/bin/echo-e"\r\n"/bin/echo"Primary"/bin/echo-e"\r\n"else/bin/echo-e"HTTP/1.1503ServiceUnavailable\r\n"/bin/echo-e"Content-Type:Content-Type:text/plain\r\n"/bin/echo-e"\r\n"/bin/echo"DBDown"/bin/echo-e"\r\n"fi[pg12@localhost~]$

如为主库,该脚本模拟接收http请求返回http 200响应,如为从库则返回206,如数据库不可用则返回503

#主库[pg12@localhost~]$ifconfig|grep'inet'inet192.168.26.28netmask255.255.0.0broadcast192.168.255.255...[pg12@localhost~]$./pgsqlchkHTTP/1.1200OKContent-Type:Content-Type:text/plainPrimary#从库[pg12@localhost~]$ifconfig|grep'inet'inet192.168.26.25netmask255.255.0.0broadcast192.168.255.255...[pg12@localhost~]$./pgsqlchkHTTP/1.1206OKContent-Type:Content-Type:text/plainStandby

创建xinetd配置文件,端口使用23267,指向刚才配置的pgsqlchk执行文件

[pg12@localhost~]$cat/etc/xinetd.d/pgsqlchkservicepgsqlchk{flags=REUSEsocket_type=streamport=23267wait=nouser=nobodyserver=/home/pg12/pgsqlchklog_on_failure+=USERIDdisable=noonly_from=0.0.0.0/0per_source=UNLIMITED}

添加服务,并启动xinetd

[pg12@localhost~]$sudobash-c'echo"pgsqlchk23267/tcp#pgsqlchk">>/etc/services'[pg12@localhost~]$sudosystemctlstartxinetd[pg12@localhost~]$systemctlstatusxinetd●xinetd.service-XinetdAPowerfulReplacementForInetdLoaded:loaded(/usr/lib/systemd/system/xinetd.service;enabled;vendorpreset:enabled)Active:active(running)sinceThu2019-11-1412:04:37CST;23sagoProcess:2847ExecStart=/usr/sbin/xinetd-stayalive-pidfile/var/run/xinetd.pid$EXTRAOPTIONS(code=exited,status=0/SUCCESS)MainPID:2848(xinetd)CGroup:/system.slice/xinetd.service└─2848/usr/sbin/xinetd-stayalive-pidfile/var/run/xinetd.pid[pg12@localhost~]$

检查服务和监听,(可通过设置flags=IPv4调整为监听IPv4地址)

[pg12@localhost~]$sudonetstat-antup|grep23267tcp600:::23267:::*LISTEN6837/xinetd[pg12@localhost~]$sudosystemctlrestartxinetd[pg12@localhost~]$sudosystemctlstatusxinetd-l●xinetd.service-XinetdAPowerfulReplacementForInetdLoaded:loaded(/usr/lib/systemd/system/xinetd.service;enabled;vendorpreset:enabled)Active:active(running)sinceThu2019-11-1415:43:49CST;6sagoProcess:7461ExecStart=/usr/sbin/xinetd-stayalive-pidfile/var/run/xinetd.pid$EXTRAOPTIONS(code=exited,status=0/SUCCESS)MainPID:7462(xinetd)CGroup:/system.slice/xinetd.service└─7462/usr/sbin/xinetd-stayalive-pidfile/var/run/xinetd.pidNov1415:43:49localhost.localdomainxinetd[7462]:START:pgsqlchkpid=7463from=192.168.26.26Nov1415:43:49localhost.localdomainxinetd[7462]:EXIT:pgsqlchkstatus=0pid=7463duration=0(sec)Nov1415:43:52localhost.localdomainxinetd[7462]:START:pgsqlchkpid=7465from=192.168.26.26Nov1415:43:52localhost.localdomainxinetd[7462]:EXIT:pgsqlchkstatus=0pid=7465duration=0(sec)Nov1415:43:52localhost.localdomainxinetd[7462]:START:pgsqlchkpid=7466from=192.168.26.26Nov1415:43:52localhost.localdomainxinetd[7462]:EXIT:pgsqlchkstatus=0pid=7466duration=0(sec)Nov1415:43:55localhost.localdomainxinetd[7462]:START:pgsqlchkpid=7467from=192.168.26.26Nov1415:43:55localhost.localdomainxinetd[7462]:EXIT:pgsqlchkstatus=0pid=7467duration=0(sec)Nov1415:43:55localhost.localdomainxinetd[7462]:START:pgsqlchkpid=7468from=192.168.26.26Nov1415:43:55localhost.localdomainxinetd[7462]:EXIT:pgsqlchkstatus=0pid=7468duration=0(sec)[pg12@localhost~]$sudonetstat-antup|grep23267tcp000.0.0.0:232670.0.0.0:*LISTEN7462/xinetd[pg12@localhost~]$

配置HAProxy with xinetd
在代理服务器上安装HAProxy

yum-yinstallhaproxytelnet

配置HAProxy(/etc/haproxy/haproxy.cfg)

[pg12@localhost~]$cat/etc/haproxy/haproxy.cfg#---------------------------------------------------------------------#Exampleconfigurationforapossiblewebapplication.Seethe#fullconfigurationoptionsonline.##http://haproxy.1wt.eu/download/1.4/doc/configuration.txt##---------------------------------------------------------------------#---------------------------------------------------------------------#Globalsettings#---------------------------------------------------------------------global#tohavethesemessagesendupin/var/log/haproxy.logyouwill#needto:##1)configuresyslogtoacceptnetworklogevents.Thisisdone#byaddingthe'-r'optiontotheSYSLOGD_OPTIONSin#/etc/sysconfig/syslog##2)configurelocal2eventstogotothe/var/log/haproxy.log#file.Alinelikethefollowingcanbeaddedto#/etc/sysconfig/syslog##local2.*/var/log/haproxy.log#log127.0.0.1local2chroot/var/lib/haproxypidfile/var/run/haproxy.pidmaxconn4000userhaproxygrouphaproxydaemon#turnonstatsunixsocketstatssocket/var/lib/haproxy/stats#---------------------------------------------------------------------#commondefaultsthatallthe'listen'and'backend'sectionswill#useifnotdesignatedintheirblock#---------------------------------------------------------------------defaultsmodehttplogglobaloptionhttplogoptionhttp-server-closeoptionforwardforexcept127.0.0.0/8retries3timeouthttp-request10stimeoutqueue1mtimeoutconnect10stimeoutclient1mtimeoutserver1mtimeouthttp-keep-alive10stimeoutcheck10smaxconn3000listenstatsmodehttpbind*:7000statsenablestatsuri/listenReadWritebind*:5000optionhttpchkhttp-checkexpectstatus200default-serverinter3sfall3rise2on-marked-downshutdown-sessionsserverpg0pg0:5432maxconn100checkport23267listenReadOnlybind*:5001optionhttpchkhttp-checkexpectstatus206default-serverinter3sfall3rise2on-marked-downshutdown-sessionsserverpg0pg0:5432maxconn100checkport23267serverpg1pg1:5432maxconn100checkport23267[pg12@localhost~]$

启动HAProxy

[pg12@localhost~]$sudosystemctlstarthaproxy[pg12@localhost~]$

划重点:
1.HAProxy使用TCP模式而不是默认的http模式
2.HAProxy监听的端口是5000-5001
3.端口5000用于RW而5001用于Read-only
4.使用http-check(端口为23267)判断状态
5.pg0可用于读写,pg1仅用于读
6.基于http-check,确定服务器状态(根据服务响应确定:200/206/503)

验证haproxy服务是否正常启动,如出现bind socket的错误,则需调整内核参数和SELinux策略

1.设置内核参数:net.ipv4.ip_nonlocal_bind=12.设置SELinux策略setsebool-Phaproxy_connect_any=1vim/etc/sysconfig/selinuxSELINUX=enforcing-->SELINUX=permissive

正常启动HAProxy的日志

[root@localhost~]#systemctlstatushaproxy-l●haproxy.service-HAProxyLoadBalancerLoaded:loaded(/usr/lib/systemd/system/haproxy.service;disabled;vendorpreset:disabled)Active:active(running)sinceThu2019-11-1414:41:09CST;12minagoMainPID:1963(haproxy-systemd)CGroup:/system.slice/haproxy.service├─1963/usr/sbin/haproxy-systemd-wrapper-f/etc/haproxy/haproxy.cfg-p/run/haproxy.pid├─1964/usr/sbin/haproxy-f/etc/haproxy/haproxy.cfg-p/run/haproxy.pid-Ds└─1965/usr/sbin/haproxy-f/etc/haproxy/haproxy.cfg-p/run/haproxy.pid-DsNov1414:41:09localhost.localdomainsystemd[1]:StartedHAProxyLoadBalancer.Nov1414:41:09localhost.localdomainhaproxy-systemd-wrapper[1963]:haproxy-systemd-wrapper:executing/usr/sbin/haproxy-f/etc/haproxy/haproxy.cfg-p/run/haproxy.pid-Ds[root@localhost~]#

到此,关于“在流复制主备环境下怎么使用HAProxy搭建负载均衡环境”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!