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

Use Haproxy to build a cluster

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Common web Cluster Scheduler

At present, the common web cluster scheduler is divided into software and hardware, the software usually uses the open source LVS,Haproxy,Nginx, the hardware is generally used by F5, and many people use some domestic products, such as barracuda, Green League and so on.

Application Analysis of Haproxy

LVS has strong anti-load ability in enterprise applications, but it has some shortcomings.

LVS does not support regular processing and cannot achieve static and dynamic separation.

For large websites, the implementation and configuration of LVS is complex, and the maintenance cost is relatively high.

Haproxy is a software that provides high availability, load balancing, and agents based on TCP and HTTP applications

It is especially suitable for web sites with heavy load.

Running on current hardware can support tens of thousands of concurrent connection requests

Principle of Haproxy scheduling algorithm

RR: the simplest and most commonly used, polling scheduling

LC: minimum number of connections algorithm, which dynamically allocates front-end requests according to the number of back-end node connections

SH: source access scheduling algorithm, which is used to record session sessions on the server. Cluster scheduling can be done based on the source ip,cookie.

Haproxy log management

Haproxy logs are output to the syslog of the system by default and are generally defined separately in the production environment.

Defined method steps

Modify the options for log configuration in the Haproxy configuration file to add the configuration:

Log / devlog local0 info

Log / dev/log local0 notice

Modify the rsyslog configuration, define the Haproxy-related configuration to haproxy.conf independently, and put it under / etc/rsyslog.d/

Save the configuration file and restart the rsyslog service to complete the rsyslog configuration

As the load of enterprise websites increases, the optimization of haproxy parameters is very important.

Maxconn: the maximum number of connections, which is adjusted according to the actual situation of the application. 10240 is recommended.

Daemon: daemon mode. Haproxy can be started in non-daemon mode. It is recommended to start in daemon mode.

Nbproc: the number of concurrent processes of load balancer, which is recommended to be equal to or twice the number of CPU cores of the current server

Retries: the number of retries, mainly used to check the cluster nodes. If there are many nodes and the concurrency is large, set it to 2 or 3 times.

Option http-server-close: actively turn off the http request option, which is recommended in a production environment

Timeout http-keep-alive: long connection timeout. You can set the long connection timeout to 10s.

Timeout http-request:http request timeout. It is recommended to set this time to 5: 10s to increase the release speed of http connections.

Timeout client: client timeout. If the number of visits is too large and the response of the node is slow, you can set this time to be shorter. It is recommended to set it to 1min or so.

Experimental environment

Haporxy server: 192.168.100.210

Nginx server 1VR 192.168.100.201

Nginx Server 2VR 192.168.100.202

Client testing machine: 192.168.100.100

Install the environment package required for the lab on three Linux servers and change the network mode of the four virtual machines to host-only mode.

Nginx Server 1 configuration

1. Configure a fixed IP address

[root@localhost ~] # vim / etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=static # dhcp changed to static

DEFROUTE=yes

IPV4_FAILURE_FATAL=no

IPV6INIT=yes

IPV6_AUTOCONF=yes

IPV6_DEFROUTE=yes

IPV6_FAILURE_FATAL=no

IPV6_ADDR_GEN_MODE=stable-privacy

NAME=ens33

UUID=8ecd53ce-afdb-46f8-b7ff-b2f428a3bc8f

DEVICE=ens33

ONBOOT=yes

IPADDR=192.168.100.201 # IP address

NETMASK=255.255.255.0 # Subnet Mask

GATEWAY=192.168.100.1 # Gateway

[root@localhost ~] # systemctl restart network # restart the network service

[root@localhost ~] # ifconfig # View Nic information

Ens33: flags=4163 mtu 1500

Inet 192.168.100.201 netmask 255.255.255.0 broadcast 192.168.100.255 # IP address configured successfully

Inet6 fe80::c776:9d00:618:88f2 prefixlen 64 scopeid 0x20

Ether 00:0c:29:eb:34:07 txqueuelen 1000 (Ethernet)

RX packets 51789 bytes 75688349 (72.1 MiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 7987 bytes 543001 (530.2 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Mount the toolkit and extract the Nginx source code package

[root@localhost ~] # mkdir / mnt/tools # create mount point

[root@localhost ~] # mount.cifs / / 192.168.100.100/tools / mnt/tools/ # mount

Password for root@//192.168.100.100/tools:

[root@localhost ~] # cd / mnt/tools/LNMP/

[root@localhost LNMP] # ls

Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz php-7.1.20.tar.gz

Mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2

[root@localhost LNMP] # tar zxf nginx-1.12.2.tar.gz-C / opt/ # decompress the source package

[root@localhost LNMP] # cd / opt/

[root@localhost opt] # ls

Nginx-1.12.2 rh

[root@localhost opt] #

Compile and install the Nginx service

[root@localhost opt] # cd nginx-1.12.2/

[root@localhost nginx-1.12.2] # ls

Auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src

[root@localhost nginx-1.12.2] # useradd-M-s / sbin/nologin nginx # create nginx user

[root@localhost nginx-1.12.2] #. / configure\ # configure Nginx service

-- prefix=/usr/local/nginx\ # installation path

-- user=nginx\ # owner

-- group=nginx\ # Group

-- with-http_stub_status_module

# enable ngx_http_stub_status_module support (get the working status of nginx since it was last started)

[root@localhost nginx-1.12.2] # make & & make install # compile and install the Nginx service

Configure the home page of the Nginx service site and start the service

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

[root@localhost html] # echo "this is abc web" > index.html # create site home page

[root@localhost html] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ # create soft links

[root@localhost html] # nginx-t # check configuration file syntax

Nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is ok

Nginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful

[root@localhost html] # nginx # enable the service

[root@localhost html] # netstat-ntap | grep 80 # View Port

Tcp 0 0 0.0.0 0 master 80 0.0.0 0. 0. 0 master

[root@localhost html] # systemctl stop firewalld.service # turn off the firewall

[root@localhost html] # setenforce 0 # turn off enhanced security features

[root@localhost html] #

Test access to the Nginx service

Nginx Server 2 configuration

Configure fixed IP addr

[root@localhost ~] # vim / etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=static # dhcp changed to static

DEFROUTE=yes

IPV4_FAILURE_FATAL=no

IPV6INIT=yes

IPV6_AUTOCONF=yes

IPV6_DEFROUTE=yes

IPV6_FAILURE_FATAL=no

IPV6_ADDR_GEN_MODE=stable-privacy

NAME=ens33

UUID=09073596-a7cf-404c-a098-28c3ff58c44b

DEVICE=ens33

ONBOOT=yes

IPADDR=192.168.100.202 # IP address

NETMASK=255.255.255.0 # Subnet Mask

GATEWAY=192.168.100.1 # Gateway

[root@localhost ~] # systemctl restart network # restart the network service

[root@localhost ~] # ifconfig # View Nic information

Ens33: flags=4163 mtu 1500

Inet 192.168.100.202 netmask 255.255.255.0 broadcast 192.168.100.255 # IP address configured successfully

Inet6 fe80::5134:22f5:842b:5201 prefixlen 64 scopeid 0x20

Ether 00:0c:29:0d:f1:75 txqueuelen 1000 (Ethernet)

RX packets 50790 bytes 74736905 (71.2 MiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 7366 bytes 466513 (455.5 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Mount the toolkit and extract the Nginx source code package

[root@localhost ~] # mkdir / mnt/tools # create mount point

[root@localhost ~] # mount.cifs / / 192.168.100.100/tools / mnt/tools/ # mount

Password for root@//192.168.100.100/tools:

[root@localhost ~] # cd / mnt/tools/LNMP/

[root@localhost LNMP] # ls

Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz php-7.1.20.tar.gz

Mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2

[root@localhost LNMP] # tar zxf nginx-1.12.2.tar.gz-C / opt/ # decompress the source package

[root@localhost LNMP] # cd / opt/

[root@localhost opt] # ls

Nginx-1.12.2 rh

[root@localhost opt] #

Compile and install the Nginx service

[root@localhost opt] # cd nginx-1.12.2/

[root@localhost nginx-1.12.2] # ls

Auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src

[root@localhost nginx-1.12.2] # useradd-M-s / sbin/nologin nginx # create nginx user

[root@localhost nginx-1.12.2] #. / configure\ # configure Nginx service

-- prefix=/usr/local/nginx\ # installation path

-- user=nginx\ # owner

-- group=nginx\ # Group

-- with-http_stub_status_module

# enable ngx_http_stub_status_module support (get the working status of nginx since it was last started)

[root@localhost nginx-1.12.2] # make & & make install # compile and install the Nginx service

Configure the home page of the Nginx service site and start the service

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

[root@localhost html] # echo "this is xyz web" > index.html # create site home page

[root@localhost html] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ # create soft links

[root@localhost html] # nginx-t # check configuration file syntax

Nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is ok

Nginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful

[root@localhost html] # nginx # enable the service

[root@localhost html] # netstat-ntap | grep 80 # View Port

Tcp 0 0 0.0.0 0 master 80 0.0.0 0. 0. 0 master

[root@localhost html] # systemctl stop firewalld.service # turn off the firewall

[root@localhost html] # setenforce 0 # turn off enhanced security features

[root@localhost html] #

Test access to the Nginx service

Haproxy server configuration

Configure fixed IP addr

[root@localhost ~] # vim / etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=static # dhcp changed to static

DEFROUTE=yes

IPV4_FAILURE_FATAL=no

IPV6INIT=yes

IPV6_AUTOCONF=yes

IPV6_DEFROUTE=yes

IPV6_FAILURE_FATAL=no

IPV6_ADDR_GEN_MODE=stable-privacy

NAME=ens33

UUID=3ceed540-b04c-48d6-a4f7-79951f09ea1d

DEVICE=ens33

ONBOOT=yes

IPADDR=192.168.100.210 # IP address

NETMASK=255.255.255.0 # Subnet Mask

GATEWAY=192.168.100.1 # Gateway

[root@localhost ~] # systemctl restart network # restart the network service

[root@localhost ~] # ifconfig # View Nic information

Ens33: flags=4163 mtu 1500

Inet 192.168.100.210 netmask 255.255.255.0 broadcast 192.168.100.255 # IP address configured successfully

Inet6 fe80::3e1d:31ba:f66a:6f80 prefixlen 64 scopeid 0x20

Ether 00:0c:29:95:9b:1b txqueuelen 1000 (Ethernet)

RX packets 51747 bytes 75871654 (72.3 MiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 7407 bytes 470471 (459.4 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Compile and install the haproxy service

[root@localhost ~] # mkdir / mnt/tools # create mount point

[root@localhost ~] # mount.cifs / / 192.168.100.100/tools / mnt/tools/ # mount

Password for root@//192.168.100.100/tools:

[root@localhost ~] # cd / mnt/tools/MySQL/

[root@localhost MySQL] # ls

Amoeba-mysql-binary-2.2.0.tar.gz haproxy-1.5.19.tar.gz mysql-5.5.24.tar.gz

Boost_1_59_0.tar.gz jdk-6u14-linux-x64.bin mysql-5.7.17.tar.gz

[root@localhost MySQL] # tar zxf haproxy-1.5.19.tar.gz-C / opt/ # decompress haproxy source code package

[root@localhost MySQL] # cd / opt/

[root@localhost opt] # ls

Haproxy-1.5.19 rh

[root@localhost opt] #

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

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

[root@localhost haproxy-1.5.19] # make install # install haproxy service

Configure the haproxy service

[root@localhost haproxy-1.5.19] # mkdir / etc/haproxy # create configuration file directory

[root@localhost haproxy-1.5.19] # cp examples/haproxy.cfg / etc/haproxy/ # template is copied to the configuration directory

[root@localhost haproxy-1.5.19] # vim / etc/haproxy/haproxy.cfg # Edit configuration file

Chroot / usr/share/haproxy # # comment out redispatch # # comment out

# for global configuration

Global

Log 127.0.0.1 local0 # configure log. Local0 is a log device, which is stored in the system log by default.

Log 127.0.0.1 local1 notice # notice is a log level, which usually has 24 levels

# log loghost local0 info

Maxconn 4096 # maximum connections

Uid 99 # user uid

Gid 99 # user gid

Daemon

# debug

# quiet

# defaults configuration item configuration default parameters are generally inherited by the application component. If there is no special declaration in the application component, the default configuration parameter settings will be installed.

Defaults

Log global # definition logs are defined as logs in the global configuration

Mode http # mode is http

Option httplog # logs in http log format

Option dontlognull # log is not empty

If retries 3 # checks that the node server fails three times in a row, the node is considered unavailable.

Maxconn 2000 # maximum connections

Contimeout 5000 # connection timeout

Clitimeout 50000 # client timeout

Srvtimeout 50000 # server timeout

Listen webcluster 0.0.0.0:80

Option httpchk GET / index.html # check the server's index.html file

Balance roundrobin # load balancing scheduling algorithm using polling algorithm

Server inst1 192.168.100.201 80 check inter 2000 fall 3 # defines a backup node with three health check requests

Server inst2 192.168.100.202:80 check inter 2000 fall 3

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

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

[root@localhost haproxy-1.5.19] # chkconfig-- add haproxy # added to service

[root@localhost haproxy-1.5.19] # ln-s / usr/local/sbin/haproxy / usr/sbin/ # is easy for system identification

[root@localhost haproxy-1.5.19] #

[root@localhost haproxy-1.5.19] # service haproxy start # enable the service

Starting haproxy (via systemctl): [OK]

[root@localhost haproxy-1.5.19] # systemctl stop firewalld.service # turn off the firewall

[root@localhost haproxy-1.5.19] # setenforce 0 # turn off enhanced security features

[root@localhost haproxy-1.5.19] #

Access the haproxy server IP address with the test host

Log definition, modifying haproxy configuration file

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

[root@localhost haproxy] # vim haproxy.cfg

Log / dev/log local0 info # add

Log / dev/log local0 notice # add

# log 127.0.0.1 local0 # comments

# log 127.0.0.1 local1 notice # comments

[root@haproxy haproxy] # service haproxy restart # # restart the service

[root@haproxy haproxy] # touch / etc/rsyslog.d/haproxy.conf # # create Syslog haproxy configuration file

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

If ($programname = = 'haproxy' and $syslogseverity-text = =' info') # # create different log files according to level

Then-/ var/log/haproxy/haproxy-info.log

& ~

If ($programname = = 'haproxy' and $syslogseverity-text = =' notice')

Then-/ var/log/haproxy/haproxy-notice.log

& ~

[root@haproxy haproxy] # systemctl restart rsyslog.service # # restart Syslog service

[root@haproxy haproxy] # cd / var/log/ # # there is no haproxy log at this time

# # revisit the web page

[root@haproxy haproxy] # cd / var/log/haproxy/

[root@haproxy haproxy] # ls # # info-level log files are generated at this time

Haproxy-info.log

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

Servers

Wechat

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

12
Report