Hearbeat高可用

一、HA(high available)高可用集羣

常用HA方案:

keepalived使用VRRP(心跳可以走業務網絡)協議,安裝配置維護簡單,適用於小型網絡

Hearbeat使用心跳(必須有專門的心跳網絡)進行通信和選舉,安裝配置維護複雜,適用於大型網絡

使用建議:優先使用Keepalived,當Keepalived不夠用的時候才選擇Heartbeat


二、使用hearbeat實現

1.更改相應環境

/etc/init.d/iptables stop                 
getenforce 
hostname  master
logout
#關閉防火牆和Selinux,臨時配置主機名,登出生效
/etc/init.d/iptables stop
getenforce 
hostname  slave
logout
vim /etc/hosts
192.168.1.200 master
192.168.1.201 slave
#修改兩臺主機的hosts文件,添加如上內容
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo    
yum clean all
yum makecache
#安裝阿里yum源及擴展源
yum install -y  heartbeat   libnet
#安裝heartbeat及其依賴包libnet


三、配置heartbeat

cd /usr/share/doc/heartbeat-3.0.4/
cp authkeys ha.cf haresources /etc/ha.d/
#拷貝配置文件模板
 vim /etc/ha.d/authkeys
#編輯主從通信文件,使用md5加密
auth 3
#1 crc
#2 sha1 HI!
3 md5 Hello!
chmod 600 /etc/ha.d/authkeys 
#更改文件權限
 vim /etc/ha.d/haresources 
#找到#node1  10.0.0.170 ,修改成如下
master   192.168.1.205/24/eth0:0 nginx
 cp /etc/ha.d/ha.cf /etc/ha.d/ha.cf.bak
> /etc/ha.d/ha.cf
vim  /etc/ha.d/ha.cf
#添加如下內容
debugfile /var/log/ha-debug      #排錯日誌  
logfile /var/log/ha-log           
logfacility  local0              
keepalive 2         #探測間隔
deadtime 30       #死亡間隔
warntime 10       #警告間隔
initdead 60         #預留間隔   
udpport 694       #心跳通信端口           
ucast eth0 192.168.1.201           #指定單播,從網卡0出去,對端IP爲1.201         
auto_failback on               #是否允許主從強佔
node master            
node slave
ping 192.168.1.1              #仲裁IP,一般配置爲網關
respawn hacluster /usr/lib64/heartbeat/ipfail            #檢測網絡連通性,注意32位和64位路徑不同
cd  /etc/ha.d/
scp -P60522 authkeys haresources ha.cf slave:/etc/ha.d/ 
#將配置文件拷貝至slave
vim  /etc/ha.d/ha.cf
#到slave上,更改對端IP,若網卡不同,還需更改網卡
ucast eth0 192.168.1.200

四、測試

yum install nginx -y
#主從服務器上都安裝nginx作爲測試
/etc/init.d/heartbeat start         #先啓動主
echo "111master" > /usr/share/nginx/html/index.html 
ps aux | grep nginx              #nginx會自動啓動,需要等待
/etc/init.d/heartbeat start         #再啓動從
echo "222slave" > /usr/share/nginx/html/index.html 
/etc/init.d/heartbeat stop   #關閉主
#從上日誌信息
tail -f /var/log/ha-log 
Jan 25 08:46:26 slave heartbeat: [2100]: WARN: node master: is dead
Jan 25 08:46:26 slave heartbeat: [2100]: info: Dead node master gave up resources.
Jan 25 08:46:26 slave heartbeat: [2100]: info: Link master:eth0 dead.
Jan 25 08:46:26 slave ipfail: [2127]: info: Status update: Node master now has status dead
Jan 25 08:46:27 slave ipfail: [2127]: info: NS: We are still alive!
Jan 25 08:46:27 slave ipfail: [2127]: info: Link Status update: Link master/eth0 now has status dead
Jan 25 08:46:29 slave ipfail: [2127]: info: Asking other side for ping node count.
Jan 25 08:46:29 slave ipfail: [2127]: info: Checking remote count of ping nodes.
#從會自動啓動eth0:0虛擬網卡和Nginx服務
[root@slave ha.d]# ip add           
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:1b:4e:e9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.201/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.205/24 brd 192.168.1.255 scope global secondary eth0:0
    inet6 fe80::20c:29ff:fe1b:4ee9/64 scope link 
       valid_lft forever preferred_lft forever
[root@slave ha.d]# ps aux | grep nginx
root       2665  0.0  0.2  96464  2100 ?        Ss   08:45   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      2667  0.0  0.2  96852  2740 ?        S    08:45   0:00 nginx: worker process                   
root       2686  0.0  0.0 103248   872 pts/0    S+   08:46   0:00 grep nginx



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章