HAProxy
HAProxy是一款提供高可用性裁着、負(fù)載均衡以及基于TCP(第四層)和HTTP(第七層)應(yīng)用的代理軟件(PS:nginx最新版也可以基于第四層和第七層的負(fù)載均衡)。
HAProxy和Keepalived 都采用源碼方式安裝踢京,如果沒有g(shù)cc編譯器色建,需要先安裝gcc編譯工具哺呜。
下載解壓安裝
下載haproxy:http://www.haproxy.org/download/1.4/src/
tar zxvf haproxy-1.4.24.tar.gz
cd haproxy-1.4.24
make install
mkdir -p /usr/local/haproxy/etc
mkdir -p /usr/local/haproxy/sbin
cp examples/haproxy.cfg /usr/local/haproxy/etc
ln -s /usr/local/sbin/haproxy /usr/local/haproxy/sbin/haproxy
修改配置文件
`vim /usr/local/haproxy/etc/haproxy.cfg`
global
maxconn 51200 #最大連接數(shù)
chroot /usr/local/haproxy #改變當(dāng)前工作目錄
uid 99
gid 99
daemon #后臺(tái)方式運(yùn)行
#quiet
nbproc 1 #并發(fā)進(jìn)程數(shù)
pidfile /usr/local/haproxy/logs/haproxy.pid #定義haproxy的pid
defaults #默認(rèn)部分的定義
mode http #mode {http|tcp|health} 。
#http是七層模式箕戳,tcp是四層模式某残,health是健康檢測(cè)返回OK
#retries 2
option redispatch
option abortonclose
timeout connect 5000ms #連接超時(shí)
timeout client 30000ms #客戶端超時(shí)
timeout server 30000ms #服務(wù)器超時(shí)
#timeout check 2000 #心跳檢測(cè)超時(shí)
log 127.0.0.1 local0 err #[err warning info debug]
#使用本機(jī)syslog服務(wù)的local3設(shè)備記錄錯(cuò)誤信息
balance roundrobin
# option httplog
# option httpclose
# option dontlognull
# option forwardfor
listen admin_stats
#定義一個(gè)名為status的部分,可以在listen指令指定的區(qū)域中定義匹配規(guī)則和后端服務(wù)器ip陵吸,
bind 0.0.0.0:8888 # 定義監(jiān)聽的套接字
option httplog
stats refresh 30s #統(tǒng)計(jì)頁(yè)面的刷新間隔為30s
stats uri /stats #登陸統(tǒng)計(jì)頁(yè)面是的uri
stats realm Haproxy Manager
stats auth admin:admin #登陸統(tǒng)計(jì)頁(yè)面是的用戶名和密碼
#stats hide-version # 隱藏統(tǒng)計(jì)頁(yè)面上的haproxy版本信息
listen tcp_test
bind :12345
mode tcp
server t1 127.0.0.1:9000
server t2 192.168.15.13:9000
listen zzs_dzfp_proxy:90
mode http
balance roundrobin #輪詢
cookie LBN insert indirect nocache
option httpclose
server web01 192.168.15.12:9000 check inter 2000 fall 3 weight 20
server web02 192.168.15.13:9000 check inter 2000 fall 3 weight 20
啟停haproxy
啟動(dòng)Haproxy
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
停止Haproxy:
killall haproxy
訪問統(tǒng)計(jì)頁(yè)面:
http://10.10.3.163:1080/stats
Keepalived
keepalived是集群管理中保證集群高可用的一個(gè)服務(wù)軟件玻墅,其功能類似于heartbeat,用來防止單點(diǎn)故障壮虫。
keepalived是以VRRP協(xié)議為實(shí)現(xiàn)基礎(chǔ)的澳厢,VRRP全稱Virtual Router Redundancy Protocol环础,即虛擬路由冗余協(xié)議。
虛擬路由冗余協(xié)議剩拢,可以認(rèn)為是實(shí)現(xiàn)路由器高可用的協(xié)議线得,即將N臺(tái)提供相同功能的路由器組成一個(gè)路由器組,這個(gè)組里面有一個(gè)master和多個(gè)backup徐伐,master上面有一個(gè)對(duì)外提供服務(wù)的vip(該路由器所在局域網(wǎng)內(nèi)其他機(jī)器的默認(rèn)路由為該vip)贯钩,master會(huì)發(fā)組播,當(dāng)backup收不到vrrp包時(shí)就認(rèn)為master宕掉了办素,這時(shí)就需要根據(jù)VRRP的優(yōu)先級(jí)來選舉一個(gè)backup當(dāng)master角雷。這樣的話就可以保證路由器的高可用了。
下載解壓編譯安裝
下載地址:http://www.keepalived.org/download.html
或者 :
wget http://www.keepalived.org/software/keepalived-1.2.8.tar.gz
安裝:
tar zxvf keepalived-1.2.8.tar.gz
cd keepalived-1.2.8
./configure --prefix=/usr/local/keepalived
make
make install
安裝成功后做成服務(wù)模式性穿。
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir -p /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
chmod +x /etc/init.d/keepalived
vi /etc/keepalived/keepalived.conf
修改文件配置keepalived.conf
global_defs {
router_id LVS_DEVEL
}
#監(jiān)測(cè)haproxy進(jìn)程狀態(tài)勺三,健康檢查,每2秒執(zhí)行一次
vrrp_script chk_haproxy {
script "/etc/keepalived/chk_haproxy.sh" #監(jiān)控腳本
interval 2 #每?jī)擅脒M(jìn)行一次
weight -10 #如果script中的指令執(zhí)行失敗需曾,vrrp_instance的優(yōu)先級(jí)會(huì)減少10個(gè)點(diǎn)
}
vrrp_instance VI_1 {
state MASTER #主服務(wù)器MASTER檩咱,從服務(wù)器為BACKUP
interface eth0 #服務(wù)器固有IP(非VIP)的網(wǎng)卡
virtual_router_id 51 #取值在0-255之間,用來區(qū)分多個(gè)instance的VRRP組播胯舷,同一網(wǎng)段中virtual_router_id的值不能重復(fù)刻蚯,否則會(huì)出錯(cuò)。
priority 100 #用來選舉master的桑嘶,要成為master炊汹,那么這個(gè)選項(xiàng)的值最好高于其他機(jī)器50個(gè)點(diǎn)。此時(shí)逃顶,從服務(wù)器要低于100讨便;
advert_int 1 #健康查檢時(shí)間間隔
mcast_src_ip 192.168.15.12 #MASTER服務(wù)器IP,從服務(wù)器寫從服務(wù)器的IP
authentication { #認(rèn)證區(qū)域
auth_type PASS #推薦使用PASS(密碼只識(shí)別前8位)
auth_pass 12345678
}
track_script {
chk_haproxy #監(jiān)測(cè)haproxy進(jìn)程狀態(tài)
}
virtual_ipaddress {
192.168.15.235 #虛擬IP,作IP漂移使用
}
}
監(jiān)控腳本配置
`vi /etc/keepalived/chk_haproxy.sh`
tatus=$(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l)
if [ "${status}" = "0" ]; then
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
status2=$(ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l)
if [ "${status2}" = "0" ]; then
/etc/init.d/keepalived stop
fi
fi
這個(gè)配置文件意思:檢查haproxy是否掛掉以政,如果掛掉啟動(dòng)haproxy霸褒;若啟動(dòng)之后還是沒有檢測(cè)到啟動(dòng)狀態(tài),則關(guān)閉keepalived盈蛮,讓IP飄移到備機(jī)上废菱。
啟動(dòng)停止keepalived命令
service keepalived start #啟動(dòng)
service keepalived stop #關(guān)閉服務(wù)
歡迎訪問我的博客:http://atecher.com/,有更多精彩文章抖誉!