VIP: 192.168.1.100
Master: 192.168.1.238
Slave: 192.168.1.239
真實IP:
RIP1: 192.168.1.235
RIP2: 192.168.1.236
以下為安裝步驟
第一步:安裝haproxy
下載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
第二步:安裝ipvs
根據ipvs官網介紹,不同版本內核適用的ipvs版本不一樣。http://www.linuxvirtualserver.org/software/ipvs.html
2.6.18 適用于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
安裝之前,軟連接系統內核目錄
ln -s /usr/src/kernels/2.6.18-194.11.3.el5-i686/ /usr/src/linux
查看系統當前的內核版本:
uname -r
2.6.18-164.el5
有時你找不到這個內核的路徑,系統安裝后并沒有kernerls的目錄
解決方法:yum install kernel-devel
接下來在進行連接 編譯 就ok了!
我們使用最新的 ipvsadm-1.24.tar.gz,下載安裝。
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make
make install
第三步,安裝keepalived
從http://www.keepalived.org上下載最新的keepalived-1.2.2.tar.gz。
tar zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure
make
make install
安裝完畢。配置
(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
如果安裝時的編譯參數為: ./configure --prefix=/usr/local/keepalived
則操作步驟如下:
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
以下為詳細的配置文件
二、haproxy
(1)、haproxy配置文件 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啟卻腳本 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
三、keepalived配置文件
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"
}
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存活狀態檢測腳本
#!/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
######----
主備切換時郵件提醒程序(此程序源自litus,有小改動,調試通過)
#!/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 = '[email protected]'
strTo = '[email protected]'
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 = '<b><font color=red>' + 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()
到此完成了簡單的haproxy+keepalived負載均衡主從熱備功能。
不明之處請參閱haproxy官方cofiguration.txt文件及keepalived官方文檔。
haproxy+keepalived負載均衡之主備切換(centos)
2019-10-08 15:53centos教程網 Centos
本文介紹haproxy+keepalived雙機熱備負載均衡,在centos5.4上調試通過
延伸 · 閱讀
- 2022-03-10簡單談談centos7中配置php
- 2022-03-10centos7下安裝java及環境變量配置技巧
- 2022-03-10CentOs下安裝gcc/g++/gdb的方法
- 2022-03-09CentOS 7中 Minimal 安裝JDK 1.8的教程
- 2022-03-09CentOS環境下安裝PHPUnit的方法分析
- 2022-03-09CentOS6.5與CentOS7 ssh修改默認端口號的方法
- Centos
CentOS 6.6實現永久修改DNS地址的方法
這篇文章主要介紹了CentOS 6.6實現永久修改DNS地址的方法,涉及針對CentOS配置文件的相關設置技巧,具有一定參考借鑒價值,需要的朋友可以參考下 ...
- Centos
CentOS6.5下Redis安裝與配置詳細步驟
本篇文章主要介紹了CentOS6.5下Redis安裝與配置詳細步驟,詳細介紹redis單機單實例安裝與配置,服務及開機自啟動。有興趣的可以了解一下。...
- Centos
Centos 7開啟網卡自動獲取IP的詳細方法
本篇文章主要介紹了Centos 7開啟網卡自動獲取IP的詳細方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧...
- Centos
Centos7運用/dev/shm進行網站優化
這篇文章主要介紹了LINUX中Centos7運用/dev/shm進行網站優化相關知識點,對此有興趣的朋友參考學習下。...
- Centos
centos 安裝與操作方法
這篇文章主要介紹了centos 安裝與操作方法,需要的朋友可以參考下...
- Centos
centos不小心刪除/root目錄該如何解決?
一些朋友最近在問小編centos不小心刪除/root目錄該如何解決?今天小編就為大家分享centos不小心刪除/root目錄解決辦法;希望對大家會有幫助,有需要的朋友...
- Centos
CentOS下Uptime命令詳解
在Linux下,我們可以使用uptime命令,而且此命令不必使用root權限。uptime命令在系統中已經默認安裝了。今天小編為大家帶來的是CentOS下Uptime命令詳解;希望...
- Centos
CentOS7設置日期和時間方法以及基本概念介紹
這篇文章主要介紹了CentOS7設置日期和時間方法以及基本概念介紹,本文講解使用CentOS7中的新命令timedatectl設置日期時間方法,需要的朋友可以參考下 ...