Load balancing of Haproxy to build Web cluster actual deployment

Load balancing of Haproxy to build Web cluster actual deployment

1. Experimental environment

Prepare four virtual machines

Haproxy服务器:192.168.126.10
Nginx 服务器1192.168.126.20 (nginx服务器可以使用之前的lnmp一键部署脚本部署)
Nginx 服务器2192.168.126.30
客户端:192.168.80.40

2. Experimental operation steps

(1) Haproxy server deployment

1. Turn off the firewall and upload the software packages required to install Haproxy to the /opt directory

systemctl stop firewalld
setenforce 

0

2. Compile and install Haproxy

yum install -y pcre-devel bzip2-devel gcc gcc-c++ make

tar zxvf haproxy-1.5.19.tar.gz
cd haproxy-1.5.19/
make TARGET=linux2628 ARCH=x86_64
make install

Parameter Description:

TARGET=linux26 #Kernel version, #Use
uname -r to view the kernel, such as: 2.6.18-371.el5, then use TARGET=linux26 for this parameter; use TARGET=linux2628 if the kernel is greater than 2.6.28

ARCH=x86_64 #System digits, 64-bit system

Insert picture description here

3. Haproxy server configuration

mkdir /etc/haproxy
cp examples/haproxy.cfg /etc/haproxy/
cd /etc/haproxy/
vim haproxy.cfg
global
--4~5行--修改,配置日志记录,local0为日志设备,默认存放到系统日志
		log /dev/log   local0 info		
        log /dev/log   local0 notice
        #log loghost    local0 info
        maxconn 4096					#最大连接数,需考虑ulimit-n限制
--8行--注释,chroot运行路径,为该服务自设置的根目录,一般需将此行注释掉
        #chroot /usr/share/haproxy
        uid 99							#用户UID
        gid 99							#用户GID
        daemon							#守护进程模式

defaults        
		log     global					#定义日志为global配置中的日志定义
        mode    http					#模式为http
        option  httplog					#采用http日志格式记录日志
        option  dontlognull				#不记录健康检查日志信息
        retries 3						#检查节点服务器失败次数,连续达到三次失败,则认为节点不可用
        redispatch						#当服务器负载很高时,自动结束当前队列处理比较久的连接
        maxconn 2000					#最大连接数
        contimeout      5000			#连接超时时间
        clitimeout      50000			#客户端超时时间
        srvtimeout      50000			#服务器超时时间

--删除下面所有listen项--,添加
listen  webcluster 0.0.0.0:80			#定义一个名为appli4-backup的应用
        option httpchk GET /test.html	#检查服务器的test.html文件
        balance roundrobin				#负载均衡调度算法使用轮询算法roundrobin
        server inst1 192.168.80.100:80 check inter 2000 fall 3		#定义在线节点
        server inst2 192.168.80.101:80 check inter 2000 fall 3

Insert picture description here

Insert picture description here

Insert picture description here

---------------------Parameter Description--------------------------- ------------------------------------------------
balance roundrobin
#Load Balance Scheduling Algorithm #Polling algorithm: roundrobin; minimum connection algorithm: leastconn; source access scheduling algorithm: source, similar to nginx's ip_hash

check inter 2000 # indicates a heartbeat frequency between haproxy server and node
fall 3 # indicates that the heartbeat frequency is not detected for three consecutive times, and the node is considered to be invalid.
If the node is configured with "backup", it means that the node is only a backup node, only the main The node will only go up when the node fails. Do not carry "backup", which means that the master node provides services together with other master nodes.

4. Add haproxy system service

cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
chmod +x haproxy
chkconfig --add /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy

service haproxy start		/etc/init.d/haproxy start

Insert picture description here

(2) Deployment of node server

You can refer to the previous blog post: Shell script one-click deployment of LNMP architecture, source code compilation and installation

systemctl stop firewalld
setenforce 0
yum install -y pcre-devel zlib-devel gcc gcc-c++ make 
useradd -M -s /sbin/nologin nginx
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/
cd nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
make && make install
--192.168.126.20---
echo "this is test111111" > /usr/local/nginx/html/test.html
--192.168.126.30---
echo "this is test222222" > /usr/local/nginx/html/test.html
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

nginx      #启动nginx 服务

Insert picture description here

(Three), test the Web cluster

Open http://192.168.126.10/test.html with a browser on the client, and constantly refresh the browser to test the load balancing effect

Insert picture description here

Insert picture description here

Three, log definition

#Default haproxy log is output to the syslog of the system, which is not very convenient to view. In order to better manage the haproxy log, we generally define it separately in the production environment. It is necessary to record the info and notice logs of haproxy into different log files.

vim /etc/haproxy/haproxy.cfg
global
log /dev/log local0 info
log /dev/log local0 notice

service haproxy restart

# Need to modify the rsyslog configuration, in order to facilitate management. Define haproxy-related configuration independently in haproxy.conf and put it under /etc/rsyslog.d/. When rsyslog starts, it will automatically load all configuration files in this directory.

vim /etc/rsyslog.d/haproxy.conf
if ($programname == ‘haproxy’ and KaTeX parse error: Expected 'EOF', got '&' at position 72: …proxy-info.log &̲~ if (programname == ‘haproxy’ and $syslogseverity-text == ‘notice’)
then -/var/log/haproxy/haproxy-notice.log
&~


#Note : This part of the configuration is to record the info log of haproxy to /var/log/haproxy/haproxy-info.log, and to record the notice log to /var/log/haproxy/haproxy-notice.log. "&~" means that when the log is written to the log file, rsyslog stops processing this information.

systemctl restart rsyslog.service

tail -f/var/log/haproxy/haproxy-info.log #View haproxy access request log information
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51573771/article/details/113000902