Keepalived + Haproxy builds high available web clusters

Haproxy is currently a popular cluster scheduling tool. There are many similar cluster scheduling tools, such as LVS and Nginx. In comparison, LVS has the best performance, but it is relatively complicated to build an LVS cluster. You can refer to the blog post to build an LVS cluster: Build: LVS+Keepalived high-availability Web service cluster environment; Nginx’s upstream module supports the cluster function, but the relative cluster node health check function is not Strong, performance is not as good as Haproxy, more is used in the corporate intranet environment. Nginx cluster can refer to the blog post: Centos 7 deployment of Tomcat and detailed load balancing configuration.

The above several web cluster schedulers belong to the software type, and there are many hardware cluster schedulers. The hardware generally used is F5, and many companies use some domestic products, such as shuttles. Fish, NSFOCUS, etc. The hardware cluster scheduler has some obvious shortcomings. If there is a problem, the manufacturer’s technical support is required. The manufacturer also needs our error log for maintenance. It may take a month from the time a problem occurs to when the problem is solved. it’s already over. If you use a software-based cluster scheduler, as long as our operation and maintenance personnel are skilled, it will be very fast if we find the problem and solve the problem.

For the commonly used scheduling algorithms, configuration files and parameter optimization of Haproxy, please refer to the blog post:

Now use the following environment to build a keepalived high-availability web cluster (about high-availability storage The server, here is omitted, I will write how to build a highly available storage server in a future article), the environment is as follows:

keepalived+Haproxy to build a highly available Web cluster

1. Preparations:

1, transfer Network, the firewall releases related traffic (I turned off the firewall directly here);

2, prepare the system image, configure the local yum (self-configuration).

3. Download the haproxy source code package, which can be downloaded from the web disk link I provided: haproxy download link
Extraction code: 54iv.

4. The web site can be built using apache, Nginx, Tomcat, as long as you can access it, you can build it yourself here, for the convenience of testing, I directly use the httpd service that comes with the system image, and the web site can be built Reference: Detailed explanation of Apache website service configuration based on Linux; Nginx website server based on centos 7

5. I use all centos 7 systems here. Note that this environment is not a production environment, if it is in a production environment , There must be back-end storage to store webpage files, and the web server reads the webpage on the storage server and returns it to the client. Only in this way can the consistency of web content be guaranteed.

Two, start to build:

1, configure the keepalived+haproxy main server:

[ [emailprotected] ~]# yum -y install keepalived pcre-devel bzip2-devel 
#Mount the system image and install related software packages
[[emailprotected] media]# tar zxf haproxy-1.5. 19.tar.gz -C /usr/src/ #Unpack
[[emailprotected] media]# cd /usr/src/haproxy-1.5.19/ #Switch to the source package directory
[ [email protected] haproxy-1.5.19]# make TARGET=linux26 && make install
#Compile and install, the TARGET configuration item means a 64-bit system. haproxy does not need ./configure configuration.
[[email Protected] haproxy-1.5.19]# mkdir /etc/haproxy #Create configuration file directory
[[email protected] haproxy-1.5.19]# cp examples/haproxy.cfg /etc /haproxy/
#Copy the configuration file directory that comes with the source package.
[[email protected] haproxy-1.5.19]# cd
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg
#Edit the main configuration file, according to the current Environment, modify the main configuration file as follows:
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
log /dev/log local0 info
log /dev/log local0 notice
#log loghost local0 info
maxconn 4096
#chroot /usr/share/haproxy #Comment out this line
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 #webcluster is the cluster name, which can be customized and the port number behind it can be modified.
option httpchk /index.html
balance roundrobin # indicates that the polling algorithm is used.
server inst1 192.168.1.3:80 check inter 2000 fall 3 #web node 1
server inst2 192.168.1.4:80 check inter 2000 fall 3 #web node 2, pay attention to the relevant port number
# Note that under the configuration file, there are many listen configuration items, find the listen item that is similar to what we need and copy it to the
#default configuration item, and then delete all the following configuration items. If you don’t delete it, the service may start An error will be reported.
[[email Protected] ~]# cd /usr/src/haproxy-1.5.19/examples/ #Switch to the specified directory
[[email protected] examples]# cp haproxy.init /etc/ init.d/haproxy #Copy service control script
[[emailprotected] examples]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
#Create soft connection
[[email protected] examples]# chmod +x /etc/init.d/haproxy #Give file execution permissions
[[email protected] examples]# chkconfig --add /etc/init.d/haproxy # Add as a system service
[[emailprotected] examples]# systemctl restart haproxy #Start haproxy service
[[emailprotected] examples]# netstat -anpt | grep 80 #Check if it is listening
#haproxy service listens on port 80 by default
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12640/haproxy
[[emailprotected] ~]# vim /etc/rsyslog.d/haproxy. conf #Configure haproxy log, write the following content

if ($programname =='haproxy' and $syslogseverity-text =='info') then -/var/log/haproxy/haproxy-info .log
& ~
if ($programname =='haproxy' and $syslogseverity-text =='notice') t hen -/var/log/haproxy/haproxy-notice.log
& ~
#After writing, save and exit. The following part starts to configure keepalived, haproxy has been configured,
#Now the client can access the server IP address to see if it can scan the webpages provided by the two web servers.
#You need to refresh several times, the web server prepares different web pages to see the effect.
[[email Protected] ~]# vim /etc/keepalived/keepalived.conf #Edit the keepalived configuration file
#Only change the configuration items marked below
! Configuration File for keepalived< br />
global_defs {
notification_email {
[emailprotected]
[emailprotected]
[emailprotected]
}
notification_email_from [ email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL1 #Define the server name, which cannot conflict with other server names
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state MASTER
interface ens33 #Modify the physical network card that carries the drifting IP address
virtual_router_id 51< br /> priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100 #Specify drift IP address
}
}
#The configuration item can be saved and exited at this point. Delete all the following configuration items so as not to affect The service starts.
[[email Protected] ~]# systemctl restart keepalived #Restart the keepalived service.

2. Configure keepalived+haproxy backup server:

[[emailprotected] ~]# systemctl stop firewalld #Close the firewall
[[ email protected] ~]# yum -y install keepalived pcre-devel bzip2-devel
#Mount the system image and install related software packages
[[emailprotected] media]# tar zxf haproxy-1.5.19 .tar.gz -C /usr/src #Unpack
[[emailprotected] media]# cd /usr/src/haproxy-1.5.19/
[[emailprotected] haproxy-1.5. 19]# make TARGET=linux26 && make install #Install
[[emailprotected] haproxy-1.5.19]# cd
[[emailprotected] ~]# mkdir /etc/haproxy #Create configuration file Directory
[[email protected] ~]# scp [email protected]:/etc/haproxy/haproxy.cfg /etc/haproxy/
#Copy the haproxy configuration file of the main server directly
[email protected] s password: #Enter the user password of the main server
haproxy.cfg 100% 566 460.5KB/s 00:00
[[email protected] ~]# scp [email protected]: /etc/init.d/haproxy /etc/init.d/haproxy
[email protected] password:
haproxy 100% 2553 2.1MB/s 00:00
[[email protected] ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy #Create link file
[[email protected] ~]# chkconfig --add /etc/init.d/haproxy #Add as a system service
[[emailprotected] ~]# systemctl start haproxy #Start service
[[emailprotected] examples ]# netstat -anpt | grep 80 #Check if it is listening
#haproxy service listens on port 80 by default
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12640/haproxy
[ [email protected] ~]# vim /etc/rsyslog.d/haproxy.conf #Configure haproxy log, write the following content

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
& ~
# After writing, save and exit.
[[email Protected] ~]# systemctl restart rsyslog #Restart log service
[[emailprotected] ~]# scp [emailprotected]:/etc/keepalived/keepalived.conf /etc/keepalived/
#Copy the keepalived configuration file of the main server over
[emailprotected] password: #Enter the user password of the main server
keepalived.conf 100% 630 622.3KB/s 00:00
[[email Protected] ~]# vim /etc/keepalived/keepalived.conf
#Modify the following three configuration items:
............... .
router_id LVS_DEVEL2 #Change the server name, don’t conflict with the main server
..............
state BACKUP #Change the state to BACKUP< br /> ...............
priority 90 #Modify the priority, which is lower than the priority of the main server

#After changing the above three OK, save and exit.
[[email Protected] ~]# systemctl start keepalived #Start the keepalived service

At this point, keepalived+haproxy is configured, and you can use the client to perform access tests to simulate the main server downtime and other issues , Test high availability.

Leave a Comment

Your email address will not be published.