linux HAProxy及Keepalived熱備
HAProxy
它是免費,快速且可靠的一種解決方案沒,適用於那些負載特大的web站點
這些站點通常又需要會話保持或七層處理
提供高可用性,負載均衡及基於tcp和http應用的代理
衡量負載均衡器性能的因素
Session rate 會話率:
每秒鐘產生的會話數
Session concurrency 並發會話數:
服務器處理會話的時間越長,並發會話數越多
Data rate 數據速率:
以MB/s或Mbps衡量,大的對象導致並發會話數增加,
高會話數,高數據速率要求更多的內存
haproxy工作模式
mode http:
客戶端請求被深度分析後再發往服務器
http事務模型:
hrrp close,客戶端像服務器建立一個tcp連接,三次握手消耗相對較大的系統資源,延遲大
keep-alive,一次連接可以傳輸多個請求
開啟keep-alive
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
KeepAlive On
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
KeepAlive On
pipelining,任然使用keep-alive,適用於有大量圖片的頁面,降低了多次請求之間的網絡延遲
mode tcp:
客戶端與服務器之間建立會話,不檢查第七層信息
mode health:
僅做健康檢查,已經不建議使用
實例:配置haproxy
[[email protected] ~]# yum -y install pcre pcre-devel(安裝開發依賴包)
[[email protected] ~]# tar -xf haproxy-1.4.24.tar.gz
[[email protected] ~]# yum -y install gcc gcc-c++
[[email protected] haproxy-1.4.24]# make USE_PCRE=1 PREFIX=/usr/local/haproxy TARGET=linux2628 install(配置文件說明內核版本高於2.6以上要寫上內核版本)
[[email protected] ~]# ln -s /usr/local/haproxy/sbin/haproxy/ /usr/local/sbin/(建立符號鏈接)
[[email protected] ~]# mkdir /usr/local/haproxy/conf
[[email protected] haproxy-1.4.24]# cp examples/haproxy.cfg /usr/local/haproxy/conf/(拷貝安裝源目錄下的示例文件)
haproxy配置文件說明
命令行:總是具有最高優先級
global部分:全局設置進程級別參數
代理聲明部分:來自於default,listen,frontend和backend
default:為後續的其他部分設置缺省參數,缺省參數可以被後續部分重置
frontend:描述接受客戶端偵聽套接字(socket)集
backend:描述轉發鏈接的服務器集
listen:把frontend和backend結合到一起的完整申明
[[email protected] ~]# vim /usr/local/haproxy/conf/haproxy.cfg
global
....
maxconn 4096(最大連接數)
chroot /usr/share/haproxy
pidfile /var/run/haproxy.pid(添加pid文件)
.......
defaults
....
option redispatch(改為option)
stats uri /ha_mon(添加uri,路徑隨便寫)
...
listen appli1-rewrite 0.0.0.0:80(0.0.0.0 相當於虛擬ip)
cookie SERVERID rewrite
balance roundrobin
server web1 192.168.4.2:80 cookie app1inst1 check inter 2000 rise 2 fall 5(服務器名,ip地址)
server web2 192.168.4.3:80 cookie app1inst2 check inter 2000 rise 2 fall 5(把後面的不要的配置都刪除)
[[email protected] conf]# haproxy -f /usr/local/haproxy/conf/haproxy.cfg(開啟服務)
[[email protected] conf]# netstat -tlnp |grep :80(查看監聽端口)
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13810/haproxy
[[email protected] ~]# firefox http://192.168.4.4/ha_mon(測試訪問查看監測)
[[email protected] ~]# kill $(pidof haproxy)(關閉進程)
[[email protected] ~]# pidof haproxy
13810
[[email protected] ~]# haproxy -f /usr/local/haproxy/conf/haproxy.cfg -st 13810(重啟)
——————————————————————————————————————————————————————————————————————————————
Keepalived熱備
keepalived實現了高可用集群,最初為lvs設計的,專門監控各服務器節點的狀態
後來加入了vrrp功能,防止單點故障(VRRP 虛擬冗余路由協議)
Keepalived運行原理
keepalived檢測每個服務器節點狀態,服務器節點異常或工作出現故障,Keepalived將故障節點從集群系統中剔除,
故障節點恢復後,Keepalived再將其加入到集群系統中
所有工作自動完成,無需人工幹預
實例:配置高可用web集群(兩臺web服務器做相同的操作)
[[email protected] ~]# yum -y install gcc gcc-c++ openssl-devel popt popt-devel(安裝依賴)
[[email protected] ~]# tar -xf keepalived-1.3.6.tar.gz
[[email protected] keepalived-1.2.7]# ./configure --sysconf=/etc
[[email protected] keepalived-1.2.7]# make
[[email protected] keepalived-1.2.7]# make install
[[email protected] ~]# ln -s /usr/local/sbin/keepalived /usr/sbin/
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER(主服務器,另一個寫副服務器)
interface eth0(正用的網卡)
virtual_router_id 51(虛擬路由器id號)
priority 100(優先級)
advert_int 1(通告間隔)
authentication {
auth_type PASS(密碼認證方式)
auth_pass 1111(兩臺密碼一樣)
}
virtual_ipaddress {
192.168.4.200(對外提供的ip地址)
}
}(底下的配置全部dd)
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state SLAVE(副服務器)
interface eth0
virtual_router_id 51
priority 90(優先級比主服務器低)
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.200
}
}
[[email protected] ~]# service keepalived start
[[email protected] ~]# firefox http://192.168.4.200(訪問測試)
實例:搭建高可用負載均衡的web集群
環境:把兩臺的keepalived stop,實現的是DR模式,所以web服務仍然要在lo上配置vip
仍然需要調整參數,把第一臺lvs上的vip刪除
把第一臺調度器上的lvs規則清空
再準備一臺調度器,主機名為lvs2,ip地址為192.168.4.4
在第二臺調度器安裝lvs(ipvsadm)
刪除lvs上的vip,清空lvs規則
[[email protected] network-scripts]# rm -rf ifcfg-eth0:0
[[email protected] ~]# /etc/init.d/network restart
[[email protected] ~]# ipvsadm -D -t 192.168.4.100:80
安裝lvs
[[email protected] ~]# vim /etc/yum.repos.d/rhel6.repo
[rhel]
name=Linux NSD
baseurl=file:///root/myiso/LoadBalancer(需要加上這個路徑,yum默認找server這個庫)
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[[email protected] ~]# yum -y install ipvsadm
安裝依賴, 安裝keepalived(兩臺lvs做相同的操作)
[[email protected] ~]# yum -y install gcc gcc-c++ openssl-devel popt-devel
[[email protected] ~]# tar -xf keepalived-1.2.7.tar.gz
[[email protected] keepalived-1.2.7]# ./configure --sysconf=/etc/
[[email protected] keepalived-1.2.7]# make
[[email protected] keepalived-1.2.7]# make install
[[email protected] keepalived-1.2.7]# ln -s /usr/local/sbin/keepalived /usr/sbin/
[[email protected] keepalived-1.2.7]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected](服務器有問題會向這個郵箱發郵件)
}
notification_email_from [email protected](誰發的郵件)
smtp_server 127.0.0.1(發郵件的地址)
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER(主服務器,另一個lvs要寫副服務器)
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.100
}
}
virtual_server 192.168.4.100 80 {(真實機上的lo:0的虛擬ip)
delay_loop 6
lb_algo rr(rr算法)
lb_kind DR(DR模式)
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.4.2 80 {(真實服務器地址)
weight 1
TCP_CHECK {(這裏默認是ssl,改為tcp)
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.4.3 80 {(真實服務器地址)
weight 1
TCP_CHECK {(這裏默認是ssl,改為tcp)
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}(下面的配置文件全部dd)
[[email protected] keepalived-1.2.7]# scp /etc/keepalived/keepalived.conf 192.168.4.5:/etc/keepalived/(把配置好的服務拷貝到lvs2的配置目錄下)
[[email protected] keepalived-1.2.7]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state SLAVE(改為副服務器)
interface eth0
virtual_router_id 51
priority 90(優先級)
[[email protected] ~]# service keepalived start; chkconfig keepalived on
[[email protected] ~]# ipvsadm -Ln(驗證)
[[email protected] ~]# firefox http://192.168.4.100/bbs(訪問測試)
————————————————————————————————————————————————————————————————————————————————————————————-
linux HAProxy及Keepalived熱備