当前位置: > CentOS > CentOS运维 >

centos6服务器搭建heartbeat

时间:2017-11-17 01:38来源:linux.it.net.cn 作者:IT

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

centos6-1   192.168.1.130

 

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  root@192.168.1.128:/root/.ssh/authorized_keys

scp /root/.ssh/id_rsa.pub  root@192.168.1.130:/root/.ssh/authorized_keys ssh信任

 

 

scp /root/aa.txt root@192.168.1.128:/root/

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

scp /root/bb.txt root@192.168.1.130:/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 1

auth 3

#1 crc

#2 sha1 HI!

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 

       valid_lft forever preferred_lft forever

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端

 

 

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: 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

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

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

       valid_lft forever preferred_lft forever

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

 




(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容