HA集群配置
准备两台设备,分别为主从
主:
[[email protected] ~]# ifconfig
eth0 inet addr:192.168.137.21
从:
[[email protected] ~]# ifconfig
eth0 inet addr:192.168.137.23
先开始配置
主上:
[[email protected] ~]# hostname master
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# vim /etc/hosts
192.168.137.21 master
192.168.137.23 slave
[email protected] ~]# yum list |grep heartbeat
无显示,先安装扩展源
[[email protected]~]#wget www.lishiming.net/data/attachment/forum/epel-release-6-8_32.noarch.rpm
[[email protected] ~]# rpm -ivh epel-release-6-8_32.noarch.rpm
[[email protected] ~]# yum install -y heartbeat libnet
从上类似操作:
[[email protected] ~]# hostname slave
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# vim /etc/hosts
192.168.137.21 master
192.168.137.23 slave
[[email protected]~]#wget www.lishiming.net/data/attachment/forum/epel-release-6-8_32.noarch.rpm
[[email protected] ~]# rpm -ivh epel-release-6-8_32.noarch.rpm
[[email protected] ~]# yum install -y heartbeat libnet
软件安装完成,现配置文件
主上:
[[email protected] ~]# cd /usr/share/doc/heartbeat-3.0.4/
[[email protected] heartbeat-3.0.4]# ls
[[email protected] heartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/
[[email protected] heartbeat-3.0.4]# cd /etc/ha.d/
[[email protected] ha.d]# vim authkeys
auth 3
#1 crc
#2 sha1 HI!
3 md5 Hello!
[[email protected] ha.d]# chmod 600 authkeys
[[email protected] 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地址方法:
[[email protected] ha.d]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# ls
[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-eth0\:1
[[email protected] 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
[[email protected] network-scripts]# /etc/init.d/network restart
[[email protected] network-scripts]# ifconfig
eth0 inet addr:192.168.137.21
eth0:1 inet addr:192.168.137.109
[[email protected] network-scripts]# cd -
/etc/ha.d
[[email protected] 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
[[email protected] ha.d]# scp authkeys haresources ha.cf slave:/etc/ha.d/
//将这几份文件拷贝到从slave设备上
[[email protected] ha.d]# yum install -y nginx
先配置从:
[[email protected] init.d]# cd /etc/ha.d/
[[email protected] ha.d]# cat authkeys //该文件不用改
[[email protected] ha.d]# cat haresources //该文件也不用改
[[email protected] ha.d]# vim ha.cf
ucast eth0 192.168.137.21 //改成对端IP
[[email protected] ha.d]# yum install -y nginx
测试:
启动heartbeat先启动主,再启动从
主:
[[email protected] ha.d]# /etc/init.d/heartbeat start
[[email protected] ha.d]# ps aux |grep nginx //nginx还未启动
[[email protected] ha.d]# ip add
inet 192.168.137.100/24 brd 192.168.137.255 scope global secondary eth0:0
//虚拟IP已有
[[email protected] 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已启动
从:
[[email protected] ha.d]# /etc/init.d/heartbeat start
[[email protected] ha.d]# ip add
//虚拟IP未启动
[root[email protected] ha.d]# ps aux |grep nginx
//nginx也未启动
主:
[[email protected] ha.d]# echo "master" >/usr/share/nginx/html/index.html
//将网站默认页面改成master,方便测试区分
windows客户端访问192.168.137.100页面为master
[[email protected] ha.d]# curl 192.168.137.100
master
从上:
[[email protected] ha.d]# echo "slave" > /usr/share/nginx/html/index.html
现模拟主设备宕机了,看是否能切换到从上
方法使主上禁ping,模拟宕机
主上:
[[email protected] ha.d]# iptables -A INPUT -p icmp -j DROP
//禁ping规则
[[email protected] ha.d]# tail /var/log/ha-log
[[email protected] ha.d]# tail -f /var/log/ha-log
[[email protected] ha.d]# ifconfig
eth0:0已释放
[[email protected] ha.d]# ps aux |grep nginx
//无nginx进程
从上查看:
[[email protected] 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启动
[[email protected] ha.d]# ifconfig
eth0:0 inet addr:192.168.137.100
//虚拟IP已启动
[[email protected] 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页面
[[email protected] ha.d]# curl 192.168.137.100
slave
主上打开禁ping规则
[[email protected] ha.d]# iptables -D INPUT -p icmp -j DROP
从上:
[[email protected] ha.d]# ifconfig
//虚拟IP释放
[[email protected] ha.d]# ps aux |grep nginx
nginx进程停止
windows客户端访问192.168.137.100出现master页面
[[email protected] ha.d]# curl 192.168.137.100
master
当主上停止heartbeat时,网站切换到从上
[[email protected] ha.d]# /etc/init.d/heartbeat stop
[[email protected] ha.d]# ifconfig
[[email protected] ha.d]# ps aux |grep nginx
window客户端访问192.168.137.100出现slave页面
[[email protected] ha.d]# curl 192.168.137.100
slave