The whole process of haproxy single machine installation and deployment

Load balancing is the most common way of system design. Nginx, HAProxy, LVS and F5 are widely used. However, nginx can only load in HTTP layer, while HAProxy can load in 7 layer or 4 layer. LVS configuration is a little too cumbersome.
HAProxy is a free, fast and reliable solution for providing high availability, load balancing and proxy services for TCP and HTTP based applications, especially for web sites with high load and requiring persistent connection or 7-layer processing mechanism. HAProxy can also isolate the back-end servers from the network and protect the back-end servers. HAProxy's load balancing ability is not as good as LVS, but it is also quite good. Because it works at layer 7, it can make in-depth analysis on HTTP request message, and forward the message to different servers at the back end (such as dynamic and static separation) according to its own needs. LVS at layer 4 cannot complete this work.
For the installation of haproxy, download address on the official website of haproxy: http://www.haproxy.org/download/

haproxy common commands:

command Explain
/app/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg Start haproxy service
ps -ef |grep haproxy View the process status of haproxy service
killall haproxy Stop haproxy service
Note: The premise of executing the kill haproxy command is that it cannot call the haproxy service again. For example, keepalived is calling the haproxy service, and executing the kill haproxy command is invalid

Restart the haproxy service

/app/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -st `cat /var/run/haproxy.pid`
Note: installation path of haproxy

Article directory

1, About Haproxy

HAProxy is a proxy software that provides high availability, load balancing and application based on TCP and HTTP. HAProxy is completely free. With HAProxy, it can provide a fast and reliable proxy solution based on TCP and HTTP application.
HAProxy is suitable for web sites with heavy load, which usually need session persistence or seven layers of processing. 
HAProxy can support tens of thousands of concurrent connections, and the running mode of HAProxy makes it easy and safe to integrate into the architecture, while protecting the web server from being exposed to the network.

2, Practical operation of Haproxy installation

PS:119 and 120 nodes install Haproxy at the same time. Follow the steps to synchronize

2.1. Download environment + tool dependency package

yum install gcc vim wget

2.2. Download haproxy

wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-2.1.2.tar.gz/sha512/929d244a67f93337e3c791b1a29f9643ac288acdf0090dc7a05c72caf61c3b04ef376b84737351f0c54d58bca66ed8af18faa20ec3adc97067313732e0dfcd7d/haproxy-2.1.2.tar.gz

Note: download on official website is too slow
It is recommended to use the conveyor door
The versions are linked as follows:
https://src.fedoraproject.org/repo/pkgs/haproxy/

2.3. Decompress haproxy

tar -zxf haproxy-2.1.2.tar.gz -C /app/

2.4. Enter the decompression directory, compile and install haproxy

#Enter the specified decompression directory
cd /app/haproxy-2.1.2/
#Source compilation of haproxy
make TARGET=linux31 PREFIX=/app/haproxy
#Install the compiled haproxy and specify the installation directory(According to company requirements)
make install PREFIX=/app/haproxy
#Create the haproxy directory to place the configuration file
mkdir /etc/haproxy
#Empowerment
groupadd -r -g 149 haproxy
useradd -g haproxy -r -s /sbin/nologin -u 149 haproxy
#Create haproxy profile
touch /etc/haproxy/haproxy.cfg
Haproxy To configure
PS:haproxy configuration file haproxy.cfg Detailed explanation
vim /etc/haproxy/haproxy.cfg

Synchronize the configuration file to the / etc/haproxy directory for the 120 server
Remember to change the ip address of haproxy

2.5. Create + configure haproxy configuration file

#Create haproxy profile
touch /etc/haproxy/haproxy.cfg
#Haproxy configuration
PS:haproxy configuration file haproxy.cfg Detailed explanation
vim /etc/haproxy/haproxy.cfg

Add the following:

#logging options
global
	log 127.0.0.1 local0 info #Log output configuration,All logs are recorded locally,adopt local0 output
	maxconn 5120
	chroot /app/haproxy #haproxy installation path
	uid 99 #Running user uid
	gid 99 #Running user group
	daemon  #Background operation
	quiet
	nbproc 20
	pidfile /var/run/haproxy.pid #Specify PID file path

defaults
	log global
	#Use4Layer agent mode,"mode http"by7Layer agent mode
	mode tcp
	#if you set mode to tcp,then you nust change tcplog into httplog
	option tcplog
	option dontlognull
	retries 3
	option redispatch
	maxconn 2000
	#Connection timeout
	timeout connect 5s
        #The client idle timeout is 60Seconds HA Initiate reconnection mechanism
        timeout client 60s
        #The server connection timeout is 15Seconds HA Initiate reconnection mechanism
        timeout server 15s	

#front-end IP for consumers and producters
listen rabbitmq_cluster
	bind 0.0.0.0:5672  #Binding protocol port
	#Configure TCP mode
	#Categories processed,Default adoption http Pattern,Configurable tcp do4Layer message forwarding
	mode tcp
	#balance url_param userid
	#balance url_param session_id check_post 64
	#balance hdr(User-Agent)
	#balance hdr(host)
	#balance hdr(Host) use_domain_only
	#balance rdp-cookie
	#balance leastconn
	#balance source //ip
	#Simple polling
	balance roundrobin
	#Load balancing strategy
	#rabbitmq Cluster node configuration #inter checks the health of mq cluster every five seconds, proves that the server is available correctly twice, and fails twice to prove that the server is unavailable, and configures the active and standby mechanisms
        server mq-01 192.168.0.115:5672 check inter 5000 rise 2 fall 2
        server mq-02 192.168.0.117:5672 check inter 5000 rise 2 fall 2
        server mq-03 192.168.0.118:5672 check inter 5000 rise 2 fall 2
#Configure haproxy web monitoring to view statistics
listen stats
	bind 192.168.0.119:8100 #Address of WEB interface to view statistics in front-end browser
	mode http
	option httplog #Log class,Use httplog
	stats enable
	stats auth admin:123456  #Set account password for viewing statistics
	#Set the monitoring address of haproxy to http://localhost:8100/rabbitmq-stats
	stats uri /rabbitmq-stats
	stats refresh 5s  #5s Refresh once

Note: the above configuration is based on haproxy-2.1.2. There are some differences in the lower version

2.6. Start haproxy

/app/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
 Note: - f specifies the profile to start

2.7. View haproxy process status

ps -ef | grep haproxy

119 haproxy server process status:

3, Browser access + authentication haproxy

PS: access the following address to monitor the rmq node:
http://192.168.0.119:8100/rabbitmq-stats

Published 897 original articles, praised 123, visited 190000+
His message board follow

Tags: RabbitMQ vim Nginx network

Posted on Tue, 10 Mar 2020 22:23:39 -0400 by briansol