Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use HAProxy to build load balancing environment in the active and standby environment of stream replication

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use HAProxy to build load balancing environment in stream replication active and standby environment". In daily operation, it is believed that many people have doubts about how to use HAProxy to build load balancing environment in stream replication active and standby environment. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "how to use HAProxy to build a load balancing environment in the active and standby environment of stream replication". Next, please follow the editor to study!

Installation

Install the relevant software using yum, and install xinted and telnet on the database server

Yum-y install xinetd telnet

Configure xinetd

Write a script pgsqlchk that identifies the master-slave library

[pg12@localhost ~] $cat pgsqlchk #! / bin/bash# This script checks if a postgres server is healthy running on localhost. It will return:# "HTTP/1.x 200 OK\ r" (if postgres is running smoothly) #-OR-# "HTTP/1.x 500 Internal Server Error\ r" (else) # The purpose of this script is make haproxy capable of monitoring postgres properly# It is recommended that a low-privileged postgres user is created to be used by this script.# For eg. Create user healthchkusr login password 'hc321';PGBIN=/appdb/pg12/pg12.0/binPGSQL_HOST= "localhost" PGSQL_PORT= "5432" PGSQL_DATABASE= "testdb" PGSQL_USERNAME= "pg12" export PGPASSWORD= "root" TMP_FILE= "/ tmp/pgsqlchk.out" ERR_FILE= "/ tmp/pgsqlchk.err" # We perform a simple query that should return a few resultsVALUE= `$PGBIN/psql-qt-h localhost-U pg12-p 5432-c "select pg_is_in_recovery ()" 2 > / dev/ null` # Check the output. If it is not empty then everything is fine and we return something. Else We just do not return anything.if [$VALUE = = "t"] then / bin/echo-e "HTTP/1.1 206 OK\ 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.1.200 OK\ 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.1 503 Service Unavailable\ r\ n "/ bin/echo-e" Content-Type: Content-Type: text/plain\ r\ n "/ bin/echo-e"\ r\ n "/ bin/echo" DB Down "/ bin/echo-e"\ r\ n "fi [pg12@localhost ~] $

If it is a master library, the script simulates receiving a http request and returns a response of http 2000.If it is a slave library, it returns 206,503 if the database is not available.

# Master library [pg12@localhost ~] $ifconfig | grep 'inet' inet 192.168.26.28 netmask 255.255.0.0 broadcast 192.168.255.255... [pg12@localhost ~] $. / pgsqlchk HTTP/1.1 200 OKContent-Type: Content-Type: text/plainPrimary# slave library [pg12@localhost ~] $ifconfig | grep' inet' inet 192.168.26.25 netmask 255.255.0.0 broadcast 192.168.255 .255... [pg12@localhost ~] $. / pgsqlchk HTTP/1.1 206OKContent-Type: Content-Type: text/plainStandby

Create a xinetd profile with port 23267, and point to the pgsqlchk executable that you just configured

[pg12@localhost ~] $cat / etc/xinetd.d/pgsqlchkservice pgsqlchk {flags = REUSE socket_type = stream port = 23267 wait = no user = nobody server = / home/pg12/pgsqlchk log_on_failure + = USERID disable = no only_from = 0. 0. 0. 0 per_source = UNLIMITED}

Add services and start xinetd

[pg12@localhost ~] $sudo bash-c 'echo "pgsqlchk 23267/tcp # pgsqlchk" > > / etc/services' [pg12@localhost ~] $sudo systemctl start xinetd [pg12@localhost ~] $systemctl status xinetd ● xinetd.service-Xinetd A Powerful Replacement For Inetd Loaded: loaded (/ usr/lib/systemd/system/xinetd.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2019-11-14 12:04:37 CST 23s ago Process: 2847 ExecStart=/usr/sbin/xinetd-stayalive-pidfile / var/run/xinetd.pid $EXTRAOPTIONS (code=exited, status=0/SUCCESS) Main PID: 2848 (xinetd) CGroup: / system.slice/xinetd.service └─ 2848 / usr/sbin/xinetd-stayalive-pidfile / var/run/xinetd.pid [pg12@localhost] $

Check service and snooping, (can be adjusted to listen IPv4 address by setting flags=IPv4)

[pg12@localhost ~] $sudo netstat-antup | grep 23267tcp6 0 0: 23267: * LISTEN 6837/xinetd [pg12@localhost ~] $sudo systemctl restart xinetd [pg12@localhost ~] $sudo systemctl status xinetd-l ● xinetd.service-Xinetd A Powerful Replacement For Inetd Loaded: loaded (/ usr/lib/systemd/system/xinetd.service; enabled Vendor preset: enabled) Active: active (running) since Thu 2019-11-14 15:43:49 CST 6s ago Process: 7461 ExecStart=/usr/sbin/xinetd-stayalive-pidfile / var/run/xinetd.pid $EXTRAOPTIONS (code=exited Status=0/SUCCESS) Main PID: 7462 (xinetd) CGroup: / system.slice/xinetd.service └─ 7462 / usr/sbin/xinetd-stayalive-pidfile / var/run/xinetd.pidNov 14 15:43:49 localhost.localdomain xinetd [7462]: START: pgsqlchk pid=7463 from=192.168.26.26Nov 14 15:43:49 localhost.localdomain xinetd [7462]: EXIT: pgsqlchk status=0 pid=7463 duration=0 (sec) Nov 14 15:43:52 localhost.localdomain xinetd [7462]: START: pgsqlchk pid=7465 from=192.168. 26.26Nov 14 15:43:52 localhost.localdomain xinetd [7462]: EXIT: pgsqlchk status=0 pid=7465 duration=0 (sec) Nov 14 15:43:52 localhost.localdomain xinetd [7462]: START: pgsqlchk pid=7466 from=192.168.26.26Nov 14 15:43:52 localhost.localdomain xinetd [7462]: EXIT: pgsqlchk status=0 pid=7466 duration=0 (sec) Nov 14 15:43:55 localhost.localdomain xinetd [7462]: START: pgsqlchk pid=7467 from=192.168.26.26Nov 14 15:43:55 localhost.localdomain xinetd [7462]: EXIT: pgsqlchk status=0 pid=7467 Duration=0 (sec) Nov 14 15:43:55 localhost.localdomain xinetd [7462]: START: pgsqlchk pid=7468 from=192.168.26.26Nov 14 15:43:55 localhost.localdomain xinetd [7462]: EXIT: pgsqlchk status=0 pid=7468 duration=0 (sec) [pg12@localhost ~] $sudo netstat-antup | grep 23267tcp 00 0.0.0.0 pg12@localhost 23267 0.0.0 LISTEN 7462/xinetd [pg12@localhost] $

Configure HAProxy with xinetd

Install HAProxy on the proxy server

Yum-y install haproxy telnet

Configure HAProxy (/ etc/haproxy/haproxy.cfg)

[pg12@localhost] $cat / etc/haproxy/haproxy.cfg#----# Example configuration for a possible web application. See the# full configuration options online.## http://haproxy.1wt.eu/download/1.4/doc/configuration.txt##---------------------------------------------------------------------# -# Global settings# -global # to have these messages end up in / var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the'- r 'option to the SYSLOGD_OPTIONS in # / etc/sysconfig/syslog # # 2) configure local2 events to go to the / var/log/haproxy.log # file. A line like the following can be added to # / etc/sysconfig/syslog # # local2.* / var/log/haproxy.log # log 127.0.0.1 local2 chroot / var/lib/haproxy pidfile / var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket Stats socket / var/lib/haproxy/stats#----# common defaults that all the 'listen' and' backend' sections will# use if not designated in their block#- -defaults mode httplog global option httplog option http-server-close option forwardfor except 127.0.0.0 Universe 8 retries 3 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 3000listen stats mode http bind *: 7000 stats enable stats uri / listen ReadWrite bind *: 5000 option httpchk http-check expect status 200 default-server inter 3s fall 3 rise 2 on-marked-down Shutdown-sessions server pg0 pg0:5432 maxconn 100check port 23267listen ReadOnly bind *: 5001 option httpchk http-check expect status 206default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions server pg0 pg0:5432 maxconn 100check port 23267 server pg1 pg1:5432 maxconn 100check port 23267 [pg12@localhost] $

Start HAProxy

[pg12@localhost ~] $sudo systemctl start haproxy [pg12@localhost ~] $

Highlight:

1.HAProxy uses TCP mode instead of the default http mode

The port on which 2.HAProxy listens is 5000-5001.

3. Port 5000 for RW and 5001 for Read-only

4. Use http-check (port 23267) to determine the status

5.pg0 can be used for reading and writing, and pg1 is only used for reading.

6. Based on the http-check, determine the server status (based on the service response: 200Gram 206T503)

Verify whether the haproxy service starts normally. If a bind socket error occurs, you need to adjust the kernel parameters and SELinux policy.

1. Set kernel parameters: net.ipv4.ip_nonlocal_bind=12. Set SELinux policy setsebool-P haproxy_connect_any=1vim / etc/sysconfig/selinuxSELINUX=enforcing-- > SELINUX=permissive

Log for normal startup of HAProxy

[root@localhost] # systemctl status haproxy-l ● haproxy.service-HAProxy Load Balancer Loaded: loaded (/ usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2019-11-14 14:41:09 CST 12min ago Main PID: 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-DsNov 14 14:41:09 localhost.localdomain systemd [1]: Started HAProxy Load Balancer.Nov 14 14:41:09 localhost.localdomain haproxy-systemd-wrapper [1963]: haproxy-systemd-wrapper: executing / usr/sbin/haproxy-f / etc/haproxy/haproxy.cfg-p / run/haproxy.pid-Ds [root@localhost ~] # so far The study on "how to use HAProxy to build a load balancing environment in the stream replication active and standby environment" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report