HA集群配置

准备两台设备,分别为主从

 

主:

[root@client ~]# ifconfig

eth0       inet addr:192.168.137.21

从:

[root@Client ~]# ifconfig

eth0      inet addr:192.168.137.23

 

先开始配置

主上:

[root@client ~]# hostname master

[root@client ~]# iptables -F

[root@client ~]# setenforce 0

[root@client ~]# vim /etc/hosts

192.168.137.21 master

192.168.137.23 slave

root@client ~]# yum list |grep heartbeat

无显示,先安装扩展源

[root@client~]#wget www.lishiming.net/data/p_w_upload/forum/epel-release-6-8_32.noarch.rpm

[root@client ~]# rpm -ivh epel-release-6-8_32.noarch.rpm

[root@client ~]# yum  install -y heartbeat  libnet

 

从上类似操作:

[root@Client ~]# hostname slave

[root@Client ~]# iptables -F

[root@Client ~]# setenforce 0

[root@Client ~]# vim /etc/hosts

192.168.137.21 master

192.168.137.23 slave

[root@Client~]#wget www.lishiming.net/data/p_w_upload/forum/epel-release-6-8_32.noarch.rpm

[root@Client ~]#  rpm -ivh epel-release-6-8_32.noarch.rpm

[root@Client ~]# yum  install -y heartbeat  libnet

 

软件安装完成,现配置文件

 

 

 

 

 

主上:

[root@client ~]# cd /usr/share/doc/heartbeat-3.0.4/

[root@client heartbeat-3.0.4]# ls

[root@client heartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/

[root@client heartbeat-3.0.4]# cd /etc/ha.d/

[root@client ha.d]# vim authkeys

auth 3

#1 crc

#2 sha1 HI!

3 md5 Hello!

[root@client ha.d]# chmod 600 authkeys

 

[root@client ha.d]# vim haresources              //加入

master 192.168.137.100/24/eth0:0 nginx

 

// master (主的主机名)192.168.137.100/24/eth0:0(虚拟的IP,指定网段,网卡eth0:0 nginx(哪些资源做主从)

 

 

此不可不做,此不为一个网卡配置多个IP地址方法:

[root@client ha.d]# cd /etc/sysconfig/network-scripts/

[root@client network-scripts]# ls

[root@client network-scripts]# cp ifcfg-eth0 ifcfg-eth0\:1

[root@client network-scripts]# vim ifcfg-eth0\:1

DEVICE=eth0:1

HWADDR=00:0C:29:6A:A5:9B

TYPE=Ethernet

UUID=4e9798c5-cbbc-45d1-9483-51d8c50643f8

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.137.109

NETMASK=255.255.255.0

#GATEWAY=192.168.137.1

[root@client network-scripts]# /etc/init.d/network restart

[root@client network-scripts]# ifconfig

eth0      inet addr:192.168.137.21

eth0:1    inet addr:192.168.137.109

 

 

 

 

[root@client network-scripts]# cd -

/etc/ha.d

[root@client ha.d]# vim 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.137.23

 auto_failback on

 node    master

 node    slave

 ping 192.168.21.1

 respawn hacluster /usr/lib/heartbeat/ipfail

 

[root@client ha.d]# scp authkeys haresources ha.cf slave:/etc/ha.d/

//将这几份文件拷贝到从slave设备上

[root@client ha.d]# yum install -y  nginx

 

 

先配置从:

[root@Client init.d]# cd /etc/ha.d/

[root@Client ha.d]# cat authkeys           //该文件不用改

[root@Client ha.d]# cat haresources        //该文件也不用改

[root@Client ha.d]# vim ha.cf

ucast eth0 192.168.137.21                 //改成对端IP

[root@Client ha.d]#  yum install -y  nginx

 

 

 

 

 

 

测试:

启动heartbeat先启动主,再启动从

主:

[root@client ha.d]# /etc/init.d/heartbeat start

[root@client ha.d]# ps aux |grep nginx                    //nginx还未启动

[root@client ha.d]# ip add

inet 192.168.137.100/24 brd 192.168.137.255 scope global secondary eth0:0

//虚拟IP已有

[root@client ha.d]# ps aux |grep nginx

root      5309  0.0  0.1  15648  1512 ?        Ss   14:35   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

nginx     5311  0.0  0.1  15804  1960 ?        S    14:35   0:00 nginx: worker process

//nginx已启动

 

 

从:

[root@Client ha.d]# /etc/init.d/heartbeat start

[root@Client ha.d]# ip add

//虚拟IP未启动

[root@Client ha.d]# ps aux |grep nginx

//nginx也未启动

 

 

 

主:

[root@client ha.d]# echo "master" >/usr/share/nginx/html/index.html

//将网站默认页面改成master,方便测试区分

 

windows客户端访问192.168.137.100页面为master

[root@client ha.d]# curl 192.168.137.100

master

 

从上:

[root@Client ha.d]# echo "slave" > /usr/share/nginx/html/index.html

 

 

现模拟主设备宕机了,看是否能切换到从上

方法使主上禁ping,模拟宕机

主上:

[root@client ha.d]# iptables -A INPUT -p icmp -j DROP

//ping规则

[root@client ha.d]# tail /var/log/ha-log

[root@client ha.d]# tail -f /var/log/ha-log

[root@master ha.d]# ifconfig

eth0:0已释放

[root@master ha.d]# ps aux |grep nginx

//nginx进程

 

从上查看:

[root@Client ha.d]# less /var/log/ha-log

IPaddr(IPaddr_192.168.137.100)[3276]:   2015/07/25_19:15:47 INFO: Bringing device eth0 up

ResourceManager(default)[3115]: 2015/07/25_19:15:47 info: Running /etc/init.d/nginx  start

//eth0启动,nginx启动

[root@Client ha.d]# ifconfig

eth0:0    inet addr:192.168.137.100

//虚拟IP已启动

[root@Client ha.d]# ps aux |grep nginx

nginx     4010  0.0  0.2  15804  2324 ?     S    20:02   0:00 nginx: worker process

//nginx启动了

 

 

windows客户端访问:192.168.137.100出现slave页面

[root@Client ha.d]# curl 192.168.137.100

slave

 

 

主上打开禁ping规则

[root@master ha.d]#  iptables -D INPUT -p icmp -j DROP

从上:

[root@Client ha.d]# ifconfig

//虚拟IP释放

[root@Client ha.d]# ps aux |grep nginx

nginx进程停止

windows客户端访问192.168.137.100出现master页面

 

[root@Client ha.d]# curl 192.168.137.100

master

 

 

当主上停止heartbeat时,网站切换到从上

[root@master ha.d]# /etc/init.d/heartbeat stop

[root@master ha.d]# ifconfig

[root@master ha.d]# ps aux |grep nginx

window客户端访问192.168.137.100出现slave页面

 

 

[root@Client ha.d]# curl 192.168.137.100

slave