您的位置:首页 > 其它

HA集群配置

2017-01-12 11:35 281 查看
高可用
高可用 high available 也称为双机热备,用户关键性业务(一台机器挂了会影响到大部分业务)
有两台服务器AB,正常时A提供服务B待命,当A宕机或服务宕掉时,B继续提供服务。
常用开源软件heartbeat和keepalived,其中keepalived有负载均衡的功能。



心跳线:最好选择串口线或独立网卡直连
此操作需要AB服务器都操作(在干净的环境操作实验)
前期准备:
## 设置一下hostname(主和从)
[root@wy ~]# hostname master
[root@wy ~]# bash
[root@master ~]#

[root@y2 ~]# hostname slave
[root@y2 ~]# bash
[root@slave ~]#

## 关闭防火墙、selinux(主和从)
[root@master ~]# iptables -F
[root@master ~]# vim /etc/selinux/config
SELINUX=disabled
## 编辑hosts文件(主和从)
[root@master ~]# vim /etc/hosts
192.168.219.129 master
192.168.219.128 slave
安装:
## 查看是否有heartbeat包(主和从)
[root@master ~]# yum list | grep heartbeat
解释说明:
若是没有先要安装一下扩展源 wget www.lishiming.net/data/attachment/forum/epel-release-6-8_64.noarch.rpm
rpm -ivh epel-release-6-8_64.noarch.rpm
或者直接yum安装 yum install -y epel-release
## 安装heartbeat(主和从)
[root@master ~]# yum install -y heartbeat

## 安装libnet(主和从)
[root@master ~]# yum install -y libnet
解释说明:
heartbeat依赖于此包

配置:
## 拷贝配置文件样例
[root@master ~]# cd /usr/share/doc/heartbeat-3.0.4/
[root@master heartbeat-3.0.4]# ls ha.cf haresources authkeys
authkeys ha.cf haresources
[root@master heartbeat-3.0.4]# cp ha.cf haresources authkeys /etc/ha.d/

## 修改配置文件
[root@master heartbeat-3.0.4]# cd /etc/ha.d/
1、authkeys
[root@master ha.d]# vim authkeys
auth 3
#1 crc
#2 sha1 HI!
3 md5 Hello!
解释说明:
它是用来验证的,两个机器主从它们之间需要去通信,验证对方有没有存活,但是这个通信最好是加密的。从简单到难:1 3 2

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

2、haresources
[root@master ha.d]# vim haresources
master 192.168.219.110/24/eth0:0 nginx
解释说明:
用来指定主节点,虚拟ip,服务
对外提供服务的ip
eth0:0虚拟ip
nginx表示/etc/init.d/nginx服务,也就是指定资源
3、ha.cf
[root@master 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.219.128
auto_failback on
node master
node slave
ping 192.168.219.1
respawn hacluster /usr/lib64/heartbeat/ipfail
解释说明:
debugfile /var/log/ha-debug 调试日志文件
logfile /var/log/ha-log 日志
logfacility local0 与log相关的日志级别
keepalive 2 检测间隔时长
deadtime 30 响应时间,超时判定为死掉了
warntime 10 指定时间内不通发送警告,且记录日志
initdead 60 重启过程中所等待的时间
udpport 694 广播端口形式检测对方心跳是否存活
ucast eth0 192.168.219.128 设置对方ip检测心跳线
auto_failback on 当从检测到主恢复后交权
node master 主host
node slave 从host
ping 192.168.219.1 仲裁地址
respawn hacluster /usr/lib64/heartbeat/ipfail 注:系统是32位的就写lib,64的就写lib64
respawn 当/usr/lib64/heartbeat/ipfail进程死掉的时候,自动把它拉起来
hacluster 是启动heartbeat的用户
ipfail 用与检测网络连通性的服务
## 将主配置文件拷贝给从
[root@master ha.d]# scp authkeys haresources ha.cf slave:/etc/ha.d/
## 从上编辑ha.cf
[root@slave ha.d]# vim ha.cf
ucast eth0 192.168.219.129
## 安装nginx(主和从)
[root@masterha.d]# yum install -y nginx

启动:
## 先启动主
[root@master ha.d]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
Done
## 再启动从
[root@slave ha.d]# /etc/init.d/heartbeat start
验证:
## 查看虚拟ip
[root@master ha.d]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> 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 forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:72:12:f5 brd ff:ff:ff:ff:ff:ff
inet 192.168.219.129/24 brd 192.168.219.255 scope global eth0
inet 192.168.219.110/24 brd 192.168.219.255 scope global secondary eth0:0
inet6 fe80::20c:29ff:fe72:12f5/64 scope link
valid_lft forever preferred_lft forever
[root@master ha.d]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:72:12:F5
inet addr:192.168.219.129 Bcast:192.168.219.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe72:12f5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:804 errors:0 dropped:0 overruns:0 frame:0
TX packets:538 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:88757 (86.6 KiB) TX bytes:89816 (87.7 KiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:72:12:F5
inet addr:192.168.219.110 Bcast:192.168.219.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
解释说明:
从上不会看到
## 主上查看nginx进程
[root@master ha.d]# ps aux|grep nginx
root 2212 0.0 0.2 109340 2176 ? Ss 02:00 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 2214 0.0 0.2 109764 2828 ? S 02:00 0:00 nginx: worker process
root 2234 0.0 0.0 103248 868 pts/0 S+ 02:00 0:00 grep nginx
## 从上查看nginx进程
[root@slave ha.d]# ps aux|grep nginx
root 1824 0.0 0.1 103252 880 pts/0 S+ 18:08 0:00 grep nginx
解释说明:
它是不可能启动的,如果它真启动了nginx,说明这个HA做得不成功,想一想,怎么两个一主一从都可以
给你提供服务呢,做的又不是负载均衡,而是高可用,这点需要注意。
测试:
## 主上修改nginx的index.html
[root@master ha.d]# echo "11111111111master" > /usr/share/nginx/html/index.html
## 页面访问一下



## 从上修改nginx的index.html
[root@slave ha.d]# echo "222222222222slave" > /usr/share/nginx/html/index.html
## 屏蔽icmp
[root@master ha.d]# iptables -A INPUT -p icmp -j DROP
解释说明:
检测网络连通性它用的一个本职的工具其实就是ping ,那我们不妨把icmp给屏蔽掉。
## 主上查看日志
[root@master ha.d]# tail /var/log/ha-log
## 从上查看日志
[root@slave ha.d]# less /var/log/ha-log
## 访问页面



## 从上查看是否监听了80端口,启动了nginx
[root@slave ha.d]# ps aux|grep nginx
root 2235 0.0 0.4 109344 2176 ? Ss 18:40 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 2241 0.0 0.5 109768 2824 ? S 18:40 0:00 nginx: worker process
root 2254 0.0 0.1 103252 880 pts/0 S+ 18:40 0:00 grep nginx
## 从上是否有了虚拟ip
[root@slave ha.d]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:0F:4B:99
inet addr:192.168.219.128 Bcast:192.168.219.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe0f:4b99/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5840 errors:0 dropped:0 overruns:0 frame:0
TX packets:5251 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1844436 (1.7 MiB) TX bytes:895624 (874.6 KiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:0F:4B:99
inet addr:192.168.219.110 Bcast:192.168.219.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
## 去掉icmp这条规则
[root@master ha.d]# iptables -D INPUT -p icmp -j DROP
## 从上查看nginx服务
[root@slave ha.d]# ps aux|grep nginx
root 2510 0.0 0.1 103252 880 pts/0 S+ 18:51 0:00 grep nginx
## 主上再查看nginx服务已经启动了
[root@master ha.d]# ps aux|grep nginx
root 3008 0.0 0.2 109340 2176 ? Ss 02:51 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 3010 0.0 0.2 109764 2828 ? S 02:51 0:00 nginx: worker process
root 3012 0.0 0.0 103248 868 pts/0 S+ 02:51 0:00 grep nginx
## 再访问页面



## 主上把heartbeat服务停掉
[root@master ha.d]# /etc/init.d/heartbeat stop
## 主上查看到nginx服务已停掉
[root@master ha.d]# ps aux|grep nginx
## 再访问页面



备注:屏蔽icmp这条规则的操作没起作用,没做成功,不知道哪里出了问题?(低版本可能可以,新版本对icmp就不起作用了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HA