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

The process of installing and configuring HAProxy on a Linux system

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "the process of installing and configuring HAProxy in the Linux system". In the daily operation, I believe that many people have doubts about the process of installing and configuring HAProxy in the Linux system. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "the process of installing and configuring HAProxy in the Linux system". Next, please follow the editor to study!

I. the concept of Haproxy

Haproxy provides high availability, load balancing, agents based on TCP and HTTP applications, and supports virtual hosts. It is a free, fast and reliable solution. Haproxy is especially suitable for heavily loaded web sites, which usually require maintenance or seven-tier processing. Haproxy runs on current hardware and can support tens of thousands of concurrent connections. And its mode of operation makes it easy and secure to integrate into your current architecture, while protecting your web server from being exposed to the network.

Second, install Haproxy

The code is as follows:

[root@node3 app] # tar zxvf haproxy-1.4.24.tar.gz

[root@node3 app] # mv haproxy-1.4.24 haproxy

[root@node3 app] # cd haproxy

[root@node3 hadoop] # uname-a

Linux node3 2.6.18-164.el5 # 1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux

[root@node3 haproxy] # make TARGET=linux24 prefix=/usr/local/haproxy

Gcc-Iinclude-Iebtree-Wall-O2-g-fno-strict-aliasing-DTPROXY-DCONFIG_HAP_CRYPT-DENABLE_POLL-DNETFILTER-DUSE_GETSOCKNAME-DCONFIG_HAPROXY_VERSION=\ "1.4.24\"-DCONFIG_HAPROXY_DATE=\ "2013-06-17\"

-DBUILD_TARGET=' "linux24"\

-DBUILD_ARCH=' "'\"

-DBUILD_CPU=' "generic"\

-DBUILD_CC=' "gcc"\

-DBUILD_CFLAGS=' "- O2-g-fno-strict-aliasing"'\

-DBUILD_OPTIONS=' "'\"

-c-o src/haproxy.o src/haproxy.c

[root@node3 haproxy] # make install PREFIX=/usr/local/haproxy

Install-d / usr/local/haproxy/sbin

Install haproxy/ usr/local/haproxy/sbin

Install-d / usr/local/haproxy/share/man/man1

Install-m 644 doc/haproxy.1 / usr/local/haproxy/share/man/man1

Install-d / usr/local/haproxy/doc/haproxy

For x in configuration architecture haproxy-en haproxy-fr; do\

Install-m 644 doc/$x.txt / usr/local/haproxy/doc/haproxy;\

Done

3. Configure Haproxy

The Haproxy configuration is divided into five parts. Of course, these components are not required and can be selected as configurations as needed.

Global: parameters are process-level, usually related to the operating system (OS). These parameters are usually set only once. If the configuration is correct, there is no need to modify the configuration again.

Default: configure default parameters that can be used to configure to frontend,backend,listen components

Frontend: the front-end virtual node that receives the request. Frontend can directly specify the specific use of the backend backend according to the rules (can be dynamically selected)

Backend: the configuration of the backend service cluster is a real server. A Backend corresponds to one or more physical servers.

Combination of listen:Frontend and Backend.

Write a haproxy.cfg file:

The code is as follows:

Global

Maxconn 51200

Chroot / usr/local/haproxy

Uid 99

Gid 99

Daemon

# quiet

Number of nbproc 1 # processes

Pidfile / usr/local/haproxy/logs/haproxy.pid

Defaults

Mode http # default mode mode {tcp | http | health}, tcp is layer 4, http is layer 7, and health only returns OK

# retries 2 # if the connection fails twice, the server is considered unavailable, or you can set it later.

Option redispatch # forcibly direct to another healthy server when the server corresponding to serverId is down

Option abortonclose # automatically end the links that have been processed for a long time in the current queue when the server load is high

Timeout connect 5000ms # connection timeout

Timeout client 30000ms # client timeout

Timeout server 30000ms # server timed out

# timeout check 2000 # = heartbeat detection timeout

Log 127.0.0.1 local0 err # [err warning info debug]

Balance roundrobin # load balancing algorithm

# option httplog # Log category, using httplog

# option httpclose # actively close the http channel after each request. Ha-proxy does not support keep-alive and can only simulate the implementation of this mode.

# option dontlognull

# option forwardfor # if the backend server needs to obtain the parameters that the client real ip needs to configure, you can obtain the client ip from Http Header

Listen admin_stats

Bind 0.0.0.0 8888 # listening port

Option httplog # uses http log format

Stats refresh 30s # automatic refresh time of statistics page

Stats uri / stats # Statistics Page url

Stats realm Haproxy Manager # prompt text on the password box of the statistics page

Stats auth admin:admin # user name and password settings on the statistics page

# stats hide-version # hide the version information of HAProxy on the statistics page

Listen test1

Bind: 12345

Mode tcp

Server t1 192.168.1.101:8881

Server t2 192.168.1.102:8881

Listen test2: 80

Option httpclose

Option forwardfor

Server s1 192.168.1.101:8080 check weight 1 minconn 1 maxconn 3 check inter 40000

Server s2 192.168.1.102:8080 check weight 1 minconn 1 maxconn 3 check inter 40000

4. Start Haproxy

The code is as follows:

[root@node3 haproxy] # / usr/local/haproxy/sbin/haproxy-f / usr/local/haproxy/haproxy.cfg

[root@node3 app] # ps-ef | grep haproxy | grep-v grep

Root 6950 1 0 19:35? 00:00:00 / usr/local/haproxy/sbin/haproxy-f / usr/local/haproxy/haproxy.cfg

Start and shut down haproxy by script

1. Write haproxy script

The code is as follows:

[root@node3 ~] # cat / etc/rc.d/init.d/haproxy

#! / bin/bash

BASE_DIR= "/ usr/local/haproxy"

ARGV= "$@"

Start ()

{

Echo "START HAPoxy SERVERS"

$BASE_DIR/sbin/haproxy-f $BASE_DIR/haproxy.cfg

}

Stop ()

{

Echo "STOP HAPoxy Listen"

Kill-TTOU $(cat $BASE_DIR/logs/haproxy.pid)

Echo "STOP HAPoxy process"

Kill-USR1 $(cat $BASE_DIR/logs/haproxy.pid)

}

Case $ARGV in

Start)

Start

ERROR=$?

Stop)

Stop

ERROR=$?

Restart)

Stop

Start

ERROR=$?

*)

Echo "hactl.sh [start | restart | stop]"

Esac

Exit $ERROR

2. Let the script start automatically with the system

The code is as follows:

[root@node3 ~] # chmod + x / etc/rc.d/init.d/haproxy

[root@node3] # chkconfig-- add haproxy

Service haproxy does not support chkconfig

-the way to solve the above problem is to add the following two sentences to / etc/rc.d/init.d/haproxy after #! / bin/bash

[root@node3 ~] # cat / etc/rc.d/init.d/haproxy

#! / bin/bash

# chkconfig: 2345 10 90

# description:haproxy

BASE_DIR= "/ usr/local/haproxy"

ARGV= "$@"

Start ()

-where 2345 is the default startup level, with a total of 7 levels of 0-6.

-level 0 indicates shutdown

-level 1 means: single user mode

-level 2 means: multi-user command line mode without network connection

-level 3 means: multi-user command line mode with network connection

-level 4 indicates that it is not available

-level 5 means: multi-user mode with graphical interface

-level 6 means: restart

-10 is the start priority, 90 is the stop priority, and the priority range is 0-100. the higher the number, the lower the priority.

3. Start and stop haproxy

The code is as follows:

[root@node3 ~] # service haproxy stop

STOP HAPoxy Listen

STOP HAPoxy process

[root@node3 ~] # ps-ef | grep haproxy | grep-v grep

[root@node3 ~] # service haproxy start

START HAPoxy SERVERS

[root@node3 ~] # ps-ef | grep haproxy | grep-v grep

Root 11259 1 0 15:33? 00:00:00 / usr/local/haproxy/sbin/haproxy-f / usr/local/haproxy/haproxy.cfg

VI. Haproxy log configuration

Haproxy does not log by default, and in addition to specifying the output of the log in the global section of the haproxy.conf, you need to configure the configuration file for the system log. Take centos6.4 as an example. Haproxy uses the rpm that comes with the system to report version 1.4.

1 、 vim / etc/haproxy/haproxy.conf

The code is as follows:

Global

Log 127.0.0.1 local3 # local3 is a device. Corresponding to the configuration in / etc/rsyslog.conf, the log level of info is recycled by default.

Maxconn 1024

User haproxy

Group haproxy

Daemon

Pidfile / var/run/haproxy.pid

Defaults

Mode http

Log global

Option httplog

Option dontlognull

Option http-server-close

Option forwardfor except 127.0.0.0/8

Retries 2

Option redispatch

Maxconn 1024

2. Edit Syslog configuration

The code is as follows:

Vim / etc/rsyslog.conf

The following settings are set by default, and the configuration files in the / etc/rsyslog.d/*.conf directory are read.

The code is as follows:

$IncludeConfig / etc/rsyslog.d/*.conf

Create a separate configuration file for haproxy

The code is as follows:

Vim / etc/rsyslog.d/haproxy.conf

$ModLoad imudp

$UDPServerRun 514

Local3.* / var/log/haproxy.log

# if the following configuration is not added, it will be written to the message file in addition to the log in / var/log/haproxy.log

& ~

3. Configure the main configuration file of rsyslog and open the remote log.

The code is as follows:

Vim / etc/sysconfig/rsyslog

SYSLOGD_OPTIONS= "- c 2-r-m 0"

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

#-r enable remote logging

#-m 0 marks the timestamp. The unit is minutes. When it is 0, the feature is disabled.

Restart haproxy and rsyslog services after configuration is complete

The code is as follows:

/ etc/init.d/rsyslog restart

/ etc/init.d/haproxy restart

4. Configuring the system log on Red Hat 5 is different from Red Hat 6. The / etc/rsyslog.conf of Red Hat 5 does not contain / etc/rsyslog.d/*.conf. The configuration is as follows

The code is as follows:

# joining haproxy.none means not writing haproxy logs to message

$ModLoad imudp

$UDPServerRun 514

* .info;mail.none;authpriv.none;cron.none;local3.none / var/log/messages

Local3.* / var/log/haproxy.log

At this point, the study on "the process of installing and configuring HAProxy in a Linux system" 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

Servers

Wechat

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

12
Report