Technology Sharing

Haproxy builds web cluster

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Case Analysis

1: Case Overview

2: Case Prerequisites

3: Case environment

2. Case Implementation

1: Compile and install the nginx server (the steps for the two nginx websites are exactly the same, only the test pages are different)

[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc*

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

[root@localhost ~]# tar zxvf nginx-1.12.0.tar.gz

[root@localhost ~]# cd nginx-1.12.0/

[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

[root@localhost nginx-1.12.0]# make && make install

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

[root@localhost nginx-1.12.0]# cd /usr/local/nginx/html/0

[root@localhost html]# echo "test web01" > test.html

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# nginx -t

[root@localhost ~]# nginx ##Start the nginx process

[root@localhost ~]# netstat -anpt | grep nginx

2: Compile and install Haproxy

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# setenforce 0

[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc*

[root@localhost ~]# tar zxvf haproxy-1.5.19.tar.gz

[root@localhost ~]# cd haproxy-1.5.19/

[root@localhost haproxy-1.5.19]# make TARGET=linux26

[root@localhost haproxy-1.5.19]# make install

Notes:

linux22     for Linux 2.2

linux24     for Linux 2.4 and above (default)

linux24e    for Linux 2.4 with support for a working epoll (> 0.21)

linux26     for Linux 2.6 and above

solaris     for Solaris 8 or 10 (others untested)

freebsd     for FreeBSD 5 to 8.0 (others untested)

openbsd     for OpenBSD 3.1 to 4.6 (others untested)

cygwin      for Cygwin

generic     for any other OS.

custom      to manually adjust every setting

3: Haproxy server configuration

(1) Create a haproxy configuration file

[root@localhost haproxy-1.5.19]# mkdir /etc/haproxy

[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/

(2) Create a service script

[root@localhost haproxy-1.5.19]# cp examples/haproxy.init /etc/init.d/haproxy

[root@localhost haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy

[root@localhost haproxy-1.5.19]# chmod +x /etc/init.d/haproxy

[root@localhost ~]# chkconfig --add haproxy

(3) Haproxy configuration introduction

[root@localhost haproxy-1.5.19]# vi /etc/haproxy/haproxy.cfg

global

        log 127.0.0.1   local0

        log 127.0.0.1   local1 notice

        #log loghost    local0 info

        maxconn 4096

        uid 99

        gid 99

        daemon

        #debug

        #quiet

defaults

        log     global

        mode    http

        option  httplog

        option  dontlognull

        retries 3

       # redispatch          

        maxconn 2000

        contimeout      5000

        clitimeout      50000

        srvtimeout      50000

listen  webcluster 0.0.0.0:80

        option  httpchk GET /index.html

        balance roundrobin

        server  inst1 192.168.1.61:80 check inter 2000 fall 3

        server  inst2 192.168.1.62:80 check inter 2000 fall 3

You can add weight after each server to set the weight value

        server  inst1 192.168.1.61:80 check inter 2000 fall 3 weight 1

server  inst2 192.168.1.62:80 check inter 2000 fall 3 weight 2

Explanation of each statement

global

        log 127.0.0.1   local0     \Configure logging,local0For the log device, the default is the system log

        log 127.0.0.1   local1 notice      \The log level isnotice

        #log loghost    local0 info    

        maxconn 4096       \Maximum number of connections

        uid 99           \useruid

        gid 99           \usergid

        daemon        \Run as a daemon

        #debug      \Debug mode, output startup information to standard output

        #quiet     \Quiet mode, no output at startup

defaults

        log     global           \useglobleLogs defined in

        mode    http           \Mode ishttp

        option  httplog         \usehttpFormat of logging

        option  dontlognull     \ensureHAProxyDo not record the heartbeat packets sent by the upper-level load balancing for detecting status data

        retries 3             \Check the number of node connection failures that exceed3The node is considered unavailable

       # redispatch         \When LoadWhen it is very high, automatically terminate the connection that has been processing for a long time in the current queue

        maxconn 2000      \Maximum number of connections

        contimeout      5000         \Connection timeoutms

        clitimeout      50000        Client timeoutms

        srvtimeout      50000              Server timeoutms

listen  webcluster 0.0.0.0:80        \Define the cluster and the port number to listen on

        option  httpchk GET /index.html    \Check the serverindex.htmldocument,Heartbeat detection URL settings

        balance roundrobin      \The load balancing scheduling algorithm is polling

        server  inst1 192.168.1.61:80 check inter 2000 fall 3        \Defining online nodes

        server  inst2 192.168.1.62:80 check inter 2000 fall 3          

check inter 2000Is to detect the heart rate (per2000msDetect once),fall 3yes3Failures considered server unavailable

In the new version, the timeout settings have been adjusted as follows

contimeout is replaced by timeout connect:Defines the timeout period that haproxy waits for when forwarding client requests to the backend server

clitimeout is replaced by timeout client:The client inactivity timeout is the time the app spends connecting to haproxy.

srvtimeout is replaced by timeout server:After the client establishes a connection with the server, the timeout period for waiting for the server is the time it takes for haproxy to connect to the backend web server.

Notes:

haproxyThere are eight scheduling algorithms

    1. balance leastconn   Minimum number of connections
    2. balance roundrobin  polling
    3. balance source      According to the clientIPHow to do hashing
    4. static-rr    According to weight
    5. uri    By requestURI
    6. url_param    By requestURlparameter
    7. hdr(name)   according toHTTPRequest header to lock each timeHTTPask
    8. rdp-cookie(name)   according tocookie(name)To lock and hash eachTCPask

chroot /usr/share/haproxy  \That is, change the root directory location referenced when the program is executed. If there is such code, you need to create this directory

About log levels

static Level DEBUG

The DEBUG Level indicates fine-grained information events that are very helpful for debugging applications.

static Level INFO

INFO level indicates that the messages highlight the application's progress at a coarse-grained level.

static Level WARN

The WARN level indicates a potentially error-prone situation.

static Level ERROR

The ERROR level indicates that although an error event occurs, it does not affect the continued operation of the system.

static Level FATAL

The FATAL level indicates that any serious error event will cause the application to exit.

Additionally, there are two special logging levels available:

static Level ALL

ALL Level is the lowest level and is used to turn on all logging.

static Level OFF

OFF Level is the highest level and is used to turn off all logging.

4: Start

[root@localhost haproxy-1.5.19]# /etc/init.d/haproxy start

5: Test the web cluster

http://192.168.1.60/test.html

Refresh the page to test

Or use script test

[root@localhost ~]# for i in $(seq 10); do curl http://192.168.1.60/test.html  ;done

6: Haproxy log

Haproxy does not record logs by default. In addition to specifying the log output in the global section of haproxy.conf, you also need to configure the system log configuration file.

method one:

[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg

global

#       log 127.0.0.1   local0

#       log 127.0.0.1   local1 notice

        #log loghost    local0 info

        maxconn 4096

        chroot /usr/share/haproxy

        uid 99

        gid 99

        daemon

        #debug

        #quiet

        log /dev/log local0 info

        log /dev/log local0 notice

[root@localhost haproxy-1.4.24]# touch /etc/rsyslog.d/haproxy.conf

[root@localhost haproxy-1.4.24]# vi /etc/rsyslog.d/haproxy.conf

if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log

& ~

if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log

& ~

[root@localhost haproxy-1.4.24]# service rsyslog restart

[root@localhost ~]#/etc/init.d/haproxy restart

[root@localhost ~]# cat /var/log/haproxy/haproxy-info.log

Method Two:

(1) Edit /etc/haproxy/haproxy.conf

[root@localhost ~]# vi /etc/haproxy/haproxy.cfg

global

 log 127.0.0.1 local3     

#local3 is the device, corresponding to the configuration in /etc/rsyslog.conf, the default log level is info

(2) Write haproxy log files

[root@localhost ~]# vim  /etc/rsyslog.d/haproxy.conf

$ModLoad imudp

$UDPServerRun 514

local3.*     /var/log/haproxy.log

&~

Notes:

$ModLoad imudpLog collection protocol UDP

$UDPServerRun 514Specify the port number used for log collection

local3.*     /var/log/haproxy.logSpecify the log storage location

(3) Configure the main configuration file of rsyslog and enable remote logging (optional)

[root@localhost ~]# vim /etc/sysconfig/rsyslog

SYSLOGD_OPTIONS=”-c 2 -r -m 0″

#-c 2 Use compatibility mode, default is -c 5

#-r Enable remote logging

#-m 0 Mark the timestamp. The unit is minutes. When it is 0, it means that this function is disabled.

(4) Restart haproxy and rsyslog services

[root@localhost ~]# systemctl restart rsyslog

[root@localhost ~]# systemctl restart haproxy

(5) Check the log after visiting the website

[root@localhost ~]# cat /var/log/haproxy.log

Extension: Proxy mysql

listen mysql 0.0.0.0:3306

server mysql1 192.168.10.205:3306 check port 3306 maxconn 300