1、rabbitmq的搭建:參考rabbimq的安裝及集羣設置redis
2、安裝和配置haproxyvim
一、安裝haproxy
yum install haproxy網絡
二、安裝rsyslog
a. 檢查rsyslog是否已安裝
rpm -q rsyslog負載均衡
b. 添加haproxy的log配置
cd /etc/rsyslog.d
vim haproxy.confsocket
=== 文件內容
$ModLoad imudp
$UDPServerRun 514
local2.* /var/log/haproxy.log
local3.* /var/log/haproxy.log
local10.* /var/log/haproxy.log
==========tcp
c. 編輯/etc/sysconfig/rsyslog
vim /etc/sysconfig/rsyslog
添加內容:
SYSLOGD_OPTIONS="-c 2 -r -m 0"spa
d. 建立日誌文件,並受權
cd /var/log
touch haproxy.log
chmod a+w haproxy.logunix
e. 重啓rsyslog服務調試
用法: /etc/init.d/rsyslog {start|stop|restart|condrestart|try-restart|reload|force-reload|status}rest
/etc/init.d/rsyslog start
三、打開防火牆端口,增長兩個端口:
9188 用於haproxy的監控界面;5670 用於rabbimq的負載均衡端口
# 添加haproxy監控界面端口
# 打開9188,容許haproxy監控界面端口
iptables -I INPUT -p tcp --dport 9188 -j ACCEPT
# 打開9188,容許rabbimq負載均衡端口
iptables -I INPUT -p tcp --dport 5670 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart
/etc/init.d/iptables status
四、配置haproxy
vim /etc/haproxy/haproxy.cfg
=======================================================
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
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
#---------------------------------------------------------------------
# 注意:由於要使用tcp的負載,屏蔽掉與http相關的默認配置
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
# 打開haproxy的監測界面
listen status
bind 0.0.0.0:9188
mode http
stats enable
stats refresh 30s
stats uri /stats
stats auth admin:123456
stats realm (Haproxy\ statistic)
stats admin if TRUE
# 監聽rabbimq_cluster
listen rabbitmq_cluster
bind 0.0.0.0:5670
mode tcp
balance roundrobin
server rabbit200 192.168.200.200:5672 check inter 5000 rise 2 fall 2
server rabbit199 192.168.200.199:5672 check inter 5000 rise 2 fall 2
=========================================================
五、 啓動haproxy
haproxy -f /etc/haproxy/haproxy.cfg
/etc/init.d/haproxy restart|start
六、 中止haproxy
killall haproxy
3、安裝和設置keepalived
一、安裝keepalived
> yum install keepalived
> chkconfig --add keepalived
> chkconfig --level 35 keepalived on
二、修改配置文件
配置keepalived
/etc/keepalived/keepalived.conf
內容:
=====================================================================
! Configuration File for keepalived
# 全局配置,demo中配置了郵件信息,此處由於調試中,暫時沒有添加
global_defs {
}
# 集羣資源監控,組合track_script進行
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 2
}
vrrp_instance HAPROXY_HA {
# 設置當前主機爲主節點,若是是備用節點,則設置爲BACKUP
# 備用節點時,設置爲:
# state BACKUP
state MASTER
# 指定HA監測網絡接口,能夠用ifconfig查看來決定設置哪個
interface eth1
# 虛擬路由標識,同一個VRRP實例要使用同一個標識,主備機
virtual_router_id 80
# 由於當前環境中VRRP組播有問題,改成使用單播發送VRRP報文
# 這個地方須要關注,以前未作此設置,結果主備節點互相不能發現,所以主備節點都升級成了MASTER,而且綁定了VIP
# 備用節點時,內容爲:
# unicast_src_ip 192.168.200.199
# unicast_peer {
# 192.168.200.200
# }
unicast_src_ip 192.168.200.200
unicast_peer {
192.168.200.199
}
# 設置優先級,確保主節點的優先級高過備用節點
# 備用節點時,設置爲:
# priority 80
priority 100
# 用於設定主備節點間同步檢查時間間隔
advert_int 2
# 設置高可用集羣中不搶佔功能,在主機down後,從機接管,當主機從新恢復後,設置此功能,備機將繼續提供服務,從而避免因切換致使的隱患
nopreempt
# 設置主備節點間的通訊驗證類型及密碼,同一個VRRP實例中需一致
authentication {
auth_type PASS
auth_pass 1234
}
# 當keepalived切換狀態到MASTER時,執行腳本
notify_master "/etc/keepalived/master.sh"
# 當keepalived切換狀態到BACKUP時,執行腳本
notify_backup "/etc/keepalived/backup.sh"
# 當keepalived切換狀態到FAULT時,執行腳本
notify_fault "/etc/keepalived/fault.sh"
# 當keepalived切換狀態到STOP時,執行腳本
notify_fault "/etc/keepalived/stop.sh"
# 集羣資源監控,組合vrrp_script進行
track_script {
check_haproxy
}
# 設置虛擬IP地址,當keepalived狀態切換爲MASTER時,此IP會自動添加到系統中
# 當狀態切換到BACKUP時,此IP會自動從系統中刪除
# 能夠經過命令ip add查看切換後的狀態
virtual_ipaddress {
192.168.200.240/24 dev eth1
}
}
====================================================================
三、啓動服務
由於是爲了實現haproxy的高可用,啓動時須要順序啓動:
(1) 啓動haproxy:
> haproxy -f /etc/haproxy/haproxy.cfg
(2) 啓動keeepalived:先啓動master節點,後啓動BACKUP節點
> /etc/init.d/keepalived start
四、中止keeepalived服務
> /etc/init.d/keepalived stop
五、檢查keeepalived的運行日誌,默認keepalived的日誌位於/var/log/message
> tail -n100 /var/log/message