一、HA(high available)高可用集群
常用HA方案:
keepalived使用VRRP(心跳可以走业务网络)协议,安装配置维护简单,适用于小型网络
Hearbeat使用心跳(必须有专门的心跳网络)进行通信和选举,安装配置维护复杂,适用于大型网络
使用建议:优先使用Keepalived,当Keepalived不够用的时候才选择Heartbeat
二、使用hearbeat实现
1.更改相应环境
/etc/init.d/iptables stop getenforce hostname masterlogout#关闭防火墙和Selinux,临时配置主机名,登出生效/etc/init.d/iptables stopgetenforce hostname slavelogoutvim /etc/hosts192.168.1.200 master192.168.1.201 slave#修改两台主机的hosts文件,添加如上内容wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repowget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo yum clean allyum 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.cfvim /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 slaveping 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/ #将配置文件拷贝至slavevim /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 deadJan 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 deadJan 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 deadJan 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: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 forever2: eth0: 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 nginxroot 2665 0.0 0.2 96464 2100 ? Ss 08:45 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.confnginx 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