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

How to implement the master-slave hot standby function of haproxy+keepalived load balancing

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

Share

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

This article shows you how to carry out the master-slave hot backup function of haproxy+keepalived load balancing. the content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

VIP: 192.168.1.100

Master: 192.168.1.238

Slave: 192.168.1.239

Real IP:

RIP1: 192.168.1.235

RIP2: 192.168.1.236

The following are the installation steps

Step 1: install haproxy

Download http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.18.tar.gz

# tar zxvf haproxy-1.4.18.tar.gz

# make TARGET=linux26

# make install

Step 2: install ipvs

According to the ipvs official website, different versions of the kernel are suitable for different ipvs versions. Http://www.linuxvirtualserver.org/software/ipvs.html

2.6.18 for all versions of 1.24

Ipvsadm-1.24-6.src.rpm (for kernel between 2.6.10 and 2.6.27.4)-December 10, 2005

Ipvsadm-1.24-5.src.rpm (for 1.2.0 or later)-October 27, 2004

Ipvsadm-1.24-4.src.rpm (for 1.1.8 or later)-January 10, 2004

Ipvsadm-1.24-3.src.rpm (for 1.1.8 or later)-December 20, 2003

Ipvsadm-1.24.tar.gz-December 10, 2005

Before installation, softconnect the system kernel directory

Ln-s / usr/src/kernels/2.6.18-194.11.3.el5-i686/ / usr/src/linux

View the current kernel version of the system:

Uname-r

2.6.18-164.el5

Sometimes you can't find the path to this kernel, and there is no kernerls directory after the system is installed.

Solution: yum install kernel-devel

The next step is to compile the link on ok!

We use the latest ipvsadm-1.24.tar.gz to download and install.

Tar zxvf ipvsadm-1.24.tar.gz

Cd ipvsadm-1.24

Make

Make install

Step 3: install keepalived

Download the latest keepalived-1.2.2.tar.gz from http://www.keepalived.org.

Tar zxvf keepalived-1.2.2.tar.gz

Cd keepalived-1.2.2

. / configure

Make

Make install

Installation complete. Configuration

(1):

Cp / usr/local/etc/rc.d/init.d/keepalived / etc/rc.d/init.d/

Cp / usr/local/etc/sysconfig/keepalived / etc/sysconfig/

Mkdir / etc/keepalived

Cp / usr/local/etc/keepalived/keepalived.conf / etc/keepalived/

Ln-s / usr/local/sbin/keepalived / usr/sbin/

Service keepalived start

If the compilation parameter at installation time is:. / configure-- prefix=/usr/local/keepalived

The steps are as follows:

Cp / usr/local/keepalived/etc/rc.d/init.d/keepalived / etc/rc.d/init.d/

Cp / usr/local/keepalived/etc/sysconfig/keepalived / etc/sysconfig/

Mkdir / etc/keepalived

Cp / usr/local/keepalived/etc/keepalived/keepalived.conf / etc/keepalived/

Ln-s / usr/local/keepalived/sbin/keepalived / usr/sbin/

Service keepalived start

The following is a detailed configuration file

II. Haproxy

(1), haproxy configuration file haproxy.cfg

Global

Daemon

Chroot / usr/local/haproxy

Maxconn 256

User haproxy

Group haproxy

Log-send-hostname xiaokk.com

Log-tag my_xiaokk

Nbproc 1

Pidfile / var/run/haproxy.pid

Log 127.0.0.1 local0 info

Defaults

Maxconn 256

Mode http

Timeout connect 5000ms

Timeout client 50000ms

Timeout server 50000ms

Timeout check 2000

Retries 3

Option httpclose

Option httplog

Option dontlognull

Option forwardfor

Option redispatch

Option abortonclose

Frontend http-www

Bind 192.168.1.100:80

Acl ha_policy hdr_reg (host)-I ^ (www.test-haproxy.com | test-haproxy.com | demo.test-haproxy.com | www.hellobaby.com)

Acl ha_baby hdr_dom (host)-I hellobaby.com

Use_backend ha_www if ha_policy

Use_backend ha_www if ha_baby

Log 127.0.0.1 local0 info

Listen admin_status

Bind 192.168.1.100:1080

Mode http

Log 127.0.0.1 local0 info

Stats uri / admin?status

Stats refresh 30s

Stats realm Haproxy\ Admin\ Center

Stats auth admin:admin

Stats hide-version

Backend ha_www

Mode http

Balance source

Cookie SERVERID

Option httpchk HEAD / index.html

Server w1 192.168.1.235:80 cookie 1 check inter 1500 rise 3 fall 3 weight 1

Server w2 192.168.1.236:85 cookie 1 check inter 1500 rise 3 fall 3 weight 2

(2) haproxy startup script haproxy.sh

#! / bin/sh

#

# chkconfig:-85 15

# description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly\

# suited for high availability environments.

#

# processname: haproxy

# config: / usr/local/haproxy/conf/haproxy.cfg

# pidfile: / var/run/haproyx.pid

#

# Source function library.

If [- f / etc/init.d/functions]; then

. / etc/init.d/functions

Elif [- f / etc/rc.d/init.d/functions]; then

. / etc/rc.d/init.d/functions

Else

Exit 0

Fi

# Source networking configuration.

. / etc/sysconfig/network

# Check that networking is up

[${NETWORKING} = "no"] & & exit 0

[- f / usr/local/haproxy/conf/haproxy.cfg] | | exit 1

RETVAL=0

# start Haproxy

Start () {

/ usr/local/haproxy/sbin/haproxy-c-Q-f / usr/local/haproxy/conf/haproxy.cfg

If [$?-ne 0]; then

Echo "Errors found in configuration file."

Return 1

Fi

Echo-n "Starting Haproxy:"

Daemon / usr/local/haproxy/sbin/haproxy-D-f / usr/local/haproxy/conf/haproxy.cfg-p / var/run/haproxy.pid

RETVAL=$?

Echo

[$RETVAL-eq 0] & & touch / var/lock/subsys/haproxy

Return $RETVAL

}

# stop Haproxy

Stop () {

Echo-n "Shutting down Haproxy:"

Killproc haproxy-USR1

RETVAL=$?

Echo

[$RETVAL-eq 0] & & rm-f / var/lock/subsys/haproxy

[$RETVAL-eq 0] & & rm-f / var/run/haproxy.pid

Return $RETVAL

}

# restart Haproxy

Restart () {

/ usr/local/haproxy/sbin/haproxy-c-Q-f / usr/local/haproxy/conf/haproxy.cfg

If [$?-ne 0]; then

Echo "ERR found in configuration file, check it with 'haproxy check'."

Return 1

Fi

Stop

Start

}

# check haproxy

Check () {

/ usr/local/haproxy/sbin/haproxy-c-Q-V-f / usr/local/haproxy/conf/haproxy.cfg

}

# rsstatus

Rhstatus () {

Status haproxy

}

# condrestart

Condrestart () {

[- e / var/lock/subsys/haproxy] & & restart | |:

}

# See how we were called.

Case "$1" in

Start)

Start

Stop)

Stop

Restart)

Restart

Reload)

Restart

Condrestart)

Condrestart

Status)

Rhstatus

Check)

Check

*)

Echo $"Usage: haproxy {start | stop | restart | reload | condrestart | status | check}"

RETVAL

Esac

Exit $RETVAL

III. Keepalived configuration file

Configuration file for Master:

! Configuration File for keepalived

Global_defs {

Router_id LVS_1

}

Vrrp_script chk_haproxy {

Script "/ etc/keepalived/check_haproxy.sh"

Interval 2

Weight 2

}

Vrrp_instance VI_1 {

State MASTER

Priority 99

Advert_int 1

Virtual_router_id 50

Garp_master_delay 1

Interface eth0

Authentication {

Auth_type PASS

Auth_pass Kxiaokk345Pix

}

Track_interface {

Eth0

}

Virtual_ipaddress {

192.168.1.100

}

Track_script {

Chk_haproxy

}

Notify_master "/ etc/keepalived/mailnotify.py master"

Notify_backup "/ etc/keepalived/mailnotify.py backup"

Notify_fault "/ etc/keepalived/mailnotify.py fault"

}

Configuration file for Slave

! Configuration File for keepalived

Global_defs {

Router_id LVS_2

}

Vrrp_script chk_haproxy {

Script "/ etc/keepalived/check_haproxy.sh"

Interval 5

Weight 2

}

Vrrp_instance VI_1 {

State BACKUP

Priority 96

Advert_int 1

Virtual_router_id 50

Garp_master_delay 1

Interface eth0

Authentication {

Auth_type PASS

Auth_pass Kxiaokk345Pix

}

Track_interface {

Eth0

}

Virtual_ipaddress {

192.168.1.100

}

Track_script {

Chk_haproxy

}

Notify_master "/ etc/keepalived/mailnotify.py master"

Notify_backup "/ etc/keepalived/mailnotify.py backup"

Notify_fault "/ etc/keepalived/mailnotify.py fault"

}

#-haproxy Survival status Detection script

#! / bin/bash

#

# desc: check haproxy service

#

A = `ip address show eth0 | grep 192.168.1.100 | wc-l`

B = `ps-C haproxy-- no-heading | wc-l`

If [$A-eq 1]; then

If [$B-eq 0]; then

/ usr/local/haproxy/haproxy.sh start

Sleep 3

Fi

Fi

#-

Email reminder program when switching between master and slave (this program is from litus, with minor changes, debugging passed)

#! / usr/bin/python

# coding: utf-8

From email.MIMEMultipart import MIMEMultipart

From email.MIMEText import MIMEText

From email.MIMEImage import MIMEImage

From email.Header import Header

Import sys

Import smtplib

#--

# file: mailnotify.py

# desc: send notify email

#

#--

StrFrom = 'toplover@sina.com'

StrTo = 'toplover@126.com'

Smtp_server = 'smtp.sina.com'

Smtp_user = 'toplover'

Smtp_pass = 'xxx***xxx'

If sys.argv [1]! = "master" and sys.argv [1]! = "backup" and sys.argv [1]! = "fault":

Sys.exit ()

Else:

Notify_type = sys.argv [1]

Mail_title = "[crt] Haproxy-notify-info"

Mail_body_plain = notify_type + 'alive,please check it now'

Mail_body_html =''+ notify_type + 'alive,please check it now'

MsgRoot = MIMEMultipart ('related')

MsgRoot ['Subject'] = Header (mail_title,'utf-8')

MsgRoot ['From'] = strFrom

MsgRoot ['To'] = strTo

MsgAlternative = MIMEMultipart ('alternative')

MsgRoot.attach (msgAlternative)

MsgText = MIMEText (mail_body_plain, 'plain',' utf-8')

MsgAlternative.attach (msgText)

MsgText = MIMEText (mail_body_html, 'html',' utf-8')

MsgAlternative.attach (msgText)

Smtp = smtplib.SMTP ()

Smtp.connect (smtp_server)

Smtp.login (smtp_user,smtp_pass)

Smtp.sendmail (strFrom, strTo, msgRoot.as_string ())

Smtp.quit ()

So far, the simple master-slave hot backup function of haproxy+keepalived load balancer has been completed.

The above content is how to carry out the master-slave hot backup function of haproxy+keepalived load balancer. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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