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

HAproxy+Keepalived load balancing-detailed process introduction of high-availability web stations

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

The following brings you HAproxy+Keepalived load balancing-highly available web station detailed process introduction, hope to give you some help in practical application, load balancing involves more things, there are not many theories, there are many books on the Internet, today we use the accumulated experience in the industry to do an answer.

Haproxy+keepalived load balancer high availability web station

OS

IP

Subnet mask

Routing gateway

Centos6.6

HAproxy

Keepalived

Eth0:192.168.26.210

255.255.252.0

192.168.25.3

VIP:192.168.27.210

Centos6.6

HAporxy

Keepalived

Eth0:192.168.26.211

255.255.252.0

192.168.25.3

VIP:192.168.27.210

Centos6.6 (WEB)

Eth0:192.168.26.212

255.255.252.0

192.168.25.3

Centos6.6 (WEB)

Eth0:192.168.26.218

255.255.252.0

192.168.25.3

1. Install Apache services 192.168.26.212 and 192.168.26.218: (yum install httpd-y) start slightly

Start Apache, create WEB pages on the two CVMs, and ensure that the network can be accessed properly:

2. Install HAproxy:192.168.26.210 and 192.168.26.211:

Yum install-y haproxy

Edit configuration file: vim / etc/haproxy/haproxy.cfg

#-

# Example configuration for a possible web application. See the

# full configuration options online.

#

# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt

#

#-

#-

# Global settings

#-

Global

# to have these messages end up in / var/log/haproxy.log you will

# need to:

#

# 1) configure syslog to accept network log events. This is done

# by adding the'- r 'option to the SYSLOGD_OPTIONS in

# / etc/sysconfig/syslog

#

# 2) configure local2 events to go to the / var/log/haproxy.log

# file. A line like the following can be added to

# / etc/sysconfig/syslog

#

# local2.* / var/log/haproxy.log

#

Log 127.0.0.1 local2

Chroot / var/lib/haproxy

Pidfile / var/run/haproxy.pid

Maxconn 4000

User haproxy

Group haproxy

Daemon

# turn on stats unix socket

Stats socket / var/lib/haproxy/stats

#-

# common defaults that all the 'listen' and' backend' sections will

# use if not designated in their block

#-

Defaults

Mode http

Log global

Option httplog

Option dontlognull

Option http-server-close

Option forwardfor except 127.0.0.0/8

Option redispatch

Retries 3

Timeout http-request 10s

Timeout queue 1m

Timeout connect 10s

Timeout client 1m

Timeout server 1m

Timeout http-keep-alive 10s

Timeout check 10s

Maxconn 3000

#-

# main frontend which proxys to the backends

#-

# frontend main *: 5000

# acl url_static path_beg-I / static / p_w_picpaths / javascript / stylesheets

# acl url_static path_end-I .jpg .gif .png .css .js

#

# use_backend static if url_static

# default_backend app

#

# #-

# # static backend for serving up p_w_picpaths, stylesheets and such

# #-

# backend static

# balance roundrobin

# server static 127.0.0.1:4331 check

Frontend websrv *: 80

Default_backend webservers

Backend webservers

Balance roundrobin

Server node3 192.168.26.212:80 check

Server node4 192.168.26.218:80 check

Listen statick

Bind *: 1024

Stats enable

Stats uri / haadmin?stats

Stats auth admin:admin

Stats hide-version

Stats admin if TRUE

The same goes for the configuration file on 192.168.26.211: so we SCP it directly.

Scp-p haproxy.cfg node2:/etc/haproxy/

Start the HAproxy service on both servers.

Install keepalived:192.168.26.210 and 192.168.26.211

First, 192.168.26.210 configuration:

Yum install-y keepalived

Edit configuration file: vim / etc/keepalived/keepalived.conf

Configuration file:

! Configuration File for keepalived

Global_defs {

Notification_email {

Acassen@firewall.loc

Failover@firewall.loc

Sysadmin@firewall.loc

}

Notification_email_from jwenshan@163.com

Smtp_server 192.168.200.1

Smtp_connect_timeout 30

Router_id LVS_DEVEL

}

Vrrp_script chk_haproxy {

Script "/ etc/keepalived/chk.sh" / / check the script for haproxy

Interval 2 / / check every two seconds

}

Vrrp_instance VI_1 {

State BACKUP / / is defined as a BACKUP node

Nopreempt / / enable no preemption

Interface eth0

Virtual_router_id 51

Priority 100th / / No preemption is enabled, so the priority here must be higher than the other.

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass jerry

}

Virtual_ipaddress {

192.168.27.210 / / configure VIP

}

Track_script {

Chk_haproxy / / call check script

}

Notify_backup "/ etc/init.d/haproxy restart"

Notify_fault "/ etc/init.d/haproxy stop"

}

Create script files: mainly used to detect haproxy status.

Vim / etc/keepalived/Chk.sh

#! / bin/bash

#

If [$(ps-C haproxy-- no-header | wc-l)-eq 0]; then

/ etc/init.d/keepalived stop

Fi

Start the Keepalived service:

Service keepalived start

192.168.26.211 configuration:

Yum install-y keepalived

Edit configuration file: Vim / etc/keepalived/keepalived.conf

! Configuration File for keepalived

Global_defs {

Notification_email {

Acassen@firewall.loc

Failover@firewall.loc

Sysadmin@firewall.loc

}

Notification_email_from jwenshan@163.com

Smtp_server 192.168.200.1

Smtp_connect_timeout 30

Router_id LVS_DEVEL

}

Vrrp_script chk_haproxy {

Script "/ etc/keepalived/chk.sh"

Interval 2

}

Vrrp_instance VI_1 {

State BACKUP

Nopreempt

Interface eth0

Virtual_router_id 51

Priority 99

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass jerry

}

Virtual_ipaddress {

192.168.27.210

}

Track_script {

Chk_haproxy

}

Notify_backup "/ etc/init.d/haproxy restart"

Notify_fault "/ etc/init.d/haproxy stop"

}

Script file: vim / etc/keepalived/chk.sh

#! / bin/bash

#

If [$(ps-C haproxy-- no-header | wc-l)-eq 0]; then

/ etc/init.d/keepalived stop

Fi

Start the Keepalived service

Service keepalived start

Test whether the VIP network is smooth: ping 192.168.27.210-t

Access the VIP network:

Http://192.168.27.210

Constantly refresh the browser to observe the display results

The test accesses the backend monitoring page through VIP:

The visit was successful.

Test High availability:

Stop the Keepalived observation on 192.168.26.210:

Service keepalived stop

First of all, the VIP network fluctuates:

Access to VIP again successfully:

VIP has been transferred to 192.168.26.211:

Restore 192.168.26.210keepalived observation

Vip did not automatically transfer back to 192.168.26.210, which is related to our setting parameters.

In this experiment, several important parameters are involved in the keepalived configuration. Nopreempt state MASTER/ state BACKUP priority can change its settings and observe its changes to adapt to applications in different scenarios.

After reading the detailed process of HAproxy+Keepalived load balancing-highly available web station, if you have anything else you need to know, you can find out what you are interested in in the industry information or find our professional and technical engineer to answer it. Technical engineers have more than ten years of experience in the industry.

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

Network Security

Wechat

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

12
Report