天天看點

centos6 搭建heartbeat

Heartbeat是High-Availability Linux Project (Linux下的高可用性項目)的産物,是一套提供防止業務主機因不可避免的意外性或計劃性當機問題的高可用性軟體。Heartbeat可以從Linux-HA 項目Web 站點免費獲得,它提供了所有HA (高可用性)系統所需要的基本功能,如啟動和停止資源、監測群集中系統的可用性、在群集中的節點間轉移共享IP 位址的所有者等。它通過串行線、以太網接口或同時使用兩者來監測特定服務(或多個服務)的運作狀況。

    Heartbeat實作了HA 功能中的核心功能——心跳,将Heartbeat軟體同時安裝在兩台伺服器上,用于監視系統的狀态,協調主從伺服器的工作,維護系統的可用性。它能偵測伺服器應用級系統軟體、硬體發生的故障,及時地進行錯誤隔絕、恢複;通過系統監控、服務監控、IP自動遷移等技術實作在整個應用中無單點故障,簡單、經濟地確定重要的服務持續高可用性。

   Heartbeat采用虛拟IP位址映射技術實作主從伺服器的切換對用戶端透明的功能。

實驗環境

centos6.5_x64

master  eth0  192.168.1.128

      eth1  192.168.1.129

slave   eth0  192.168.1.130

      eth1  192.168.1.131

vip         192.168.1.200

實驗軟體

epel-release-6-8.noarch.rpm

nginx.x86_64

heartbeat.x86_64

軟體安裝

rpm -ivh  epel-release-6-8.noarch.rpm

yum install -y libnet heartbeat* nginx* glib*

vim /etc/hosts

centos6    192.168.1.128  添加兩行配置 master/slave相同操作 uname -n名字

centos6-1   192.168.1.130

less /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1       localhost localhost.localdomain localhost6 localhost6.localdomain6

centos6    192.168.1.128

ssh-keygen -t rsa      master/slave 相同操作

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): 

Enter passphrase (empty for no passphrase): 

Enter same passphrase again: 

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

scp /root/.ssh/id_rsa.pub  [email protected]:/root/.ssh/authorized_keys

scp /root/.ssh/id_rsa.pub  [email protected]:/root/.ssh/authorized_keys ssh信任

scp /root/aa.txt [email protected]:/root/

aa.txt                                        100%    0     0.0KB/s   00:00 

scp /root/bb.txt [email protected]:/root/

bb.txt                                        100%    0     0.0KB/s   00:00

如果使用scp指令沒有提示輸入密碼表示ssh信任成功

service nginx start

chkconfig  --level 35 nginx on 

cd /usr/share/doc/heartbeat-3.0.4/      master/slave相同操作

cp authkeys ha.cf haresources /etc/ha.d/

vim /etc/ha.d/authkeys

#auth 1

#1 crc

#2 sha1 HI!

#3 md5 Hello! 找到

auth 3

3 md5 Hello! 改為  第一種是CRC循環備援校驗,第二種是SHA1雜湊演算法,第三種是MD3雜湊演算法

chmod 600 /etc/ha.d/authkeys

vim /etc/ha.d/haresources

centos6    192.168.1.200/24/eth0:0 nginx  master端配置

centos6-1   192.168.1.200/24/eth0:0 nginx  slave端配置

設定虛拟IP和對應的接口,并且指定啟動虛拟IP時啟動NGINX服務

vim /etc/ha.d/ha.cf

#debugfile /var/log/ha-debug  找到

debugfile /var/log/ha-debug   改為

touch  /var/log/ha-debug

#logfile  /var/log/ha-log  找到

logfile   /var/log/ha-log  改為 日志檔案位置

touch    /var/log/ha-log

logfacility     local0

#keepalive 2 找到   檢查時間間隔

keepalive 2 改為

#deadtime 30 找到  設定死亡時間

deadtime 30 改為  

#warntime 10  找到  設定告警時間  

warntime 10   改為

#initdead 120 找到 設定初始化時間

initdead 20 改為

#udpport  694 找到

udpport   694 改為  udp端口号

#ucast eth0 192.168.1.2  找到

ucast eth1 192.168.1.131 改為 master端配置 eth1為心跳線ip 192.168.1.131為slave端ip

ucast eth1 192.168.1.129 改為 slave端配置  eth1為心跳線ip 192.168.1.129為master端ip

auto_failback on 找到 啟動搶占模式(主在挂了以後重新起來後備會自動切換成備)auto_failback auto_failback on 改為

node centos6   指定負載節點

node centos6-1

#ping 10.10.10.254 找到

ping 192.168.1.101 改為

#respawn hacluster /usr/lib/heartbeat/ipfail 找到

respawn hacluster /usr/lib64/heartbeat/ipfail 改為

chmod 600 /etc/ha.d/ha.cf

service heartbeat start  master/slave端相同操作

netstat -tuplna | grep heartbeat

udp        0      0 0.0.0.0:56611               0.0.0.0:*                               6831/heartbeat: wri 

udp        0      0 0.0.0.0:694                 0.0.0.0:*                               6831/heartbeat: wri 

ps -ef | grep heartbest

root      8034  8015  0 00:27 pts/0    00:00:00 grep heartbest

tail -f /var/log/ha-log

ip-request-resp(default)[12892]:        2017/10/25_01:25:59 received ip-request-resp 192.168.1.200/24/eth0:0 OK yes

ResourceManager(default)[12915]:        2017/10/25_01:26:00 info: Acquiring resource group: centos6 192.168.1.200/24/eth0:0 nginx

/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.1.200)[12943]: 2017/10/25_01:26:00 INFO:  Resource is stopped

ResourceManager(default)[12915]:        2017/10/25_01:26:00 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.200/24/eth0:0 start

IPaddr(IPaddr_192.168.1.200)[13076]:    2017/10/25_01:26:00 INFO: Adding inet address 192.168.1.200/24 with broadcast address 192.168.1.255 to device eth0 (with label eth0:0)

IPaddr(IPaddr_192.168.1.200)[13076]:    2017/10/25_01:26:00 INFO: Bringing device eth0 up

IPaddr(IPaddr_192.168.1.200)[13076]:    2017/10/25_01:26:00 INFO: /usr/libexec/heartbeat/send_arp -i 200 -r 5 -p /var/run/resource-agents/send_arp-192.168.1.200 eth0 192.168.1.200 auto not_used not_used

ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:45:17:73 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.129/24 brd 192.168.1.255 scope global eth1

    inet6 fe80::20c:29ff:fe45:1773/64 scope link 

3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:45:17:69 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.128/24 brd 192.168.1.255 scope global eth0

    inet 192.168.1.200/24 brd 192.168.1.255 scope global secondary eth0:0

    inet6 fe80::20c:29ff:fe45:1769/64 scope link 

       valid_lft forever preferred_lft forever   master端

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:3d:5b:1b brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.130/24 brd 192.168.1.255 scope global eth0

    inet6 fe80::20c:29ff:fe3d:5b1b/64 scope link 

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:3d:5b:25 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.131/24 brd 192.168.1.255 scope global eth1

    inet6 fe80::20c:29ff:fe3d:5b25/64 scope link 

       valid_lft forever preferred_lft forever   slave端

看到eth0:0 說明配置成功

echo nginx_master > /usr/share/nginx/html/index.html  master端

echo nginx_slave > /usr/share/nginx/html/index.html   slave端

nginx -s reload

<a href="https://s2.51cto.com/oss/201710/25/750b3664141c30ea13eea498bc2278cb.jpg-wh_500x0-wm_3-wmp_4-s_2828371637.jpg" target="_blank"></a>

本文轉自 mailfile 51CTO部落格,原文連結:http://blog.51cto.com/mailfile/1975846,如需轉載請自行聯系原作者

繼續閱讀