linux学习之使用heartbeat搭建高可用lvs集群服务

使用heartbeat搭建HA+LB集群实验环境,注意时间同步和解析,火墙和selinux关闭192.168.2.168 ankse.example.com192.168.2.72 ha1.example.com192.168.2.68 ha2.example.com192.168.2.78 lb1.example.com192.168.2.221 lb2.example.com主机的yum源为[root@ha1 yum.repos.d]# cat dvd.repo# repos on instructor for classroom use# Main rhel6.5 server[base]name=Instructor Server Repositorybaseurl=http://192.168.2.251/pub/rhel6.5gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release# HighAvailability rhel6.5[HighAvailability]name=Instructor HighAvailability Repositorybaseurl=http://192.168.2.251/pub/rhel6.5/HighAvailabilitygpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release# LoadBalancer packages[LoadBalancer]name=Instructor LoadBalancer Repositorybaseurl=http://192.168.2.251/pub/rhel6.5/LoadBalancergpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release# ResilientStorage[ResilientStorage]name=Instructor ResilientStorage Repositorybaseurl=http://192.168.2.251/pub/rhel6.5/ResilientStoragegpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release# ScalableFileSystem[ScalableFileSystem]name=Instructor ScalableFileSystem Repositorybaseurl=http://192.168.2.251/pub/rhel6.5/ScalableFileSystemgpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

1、HA节点安装和配置

lftp i:~> get pub/docs/heartbeat/rhel6/ldirectord-3.9.2-1.2.x86_64.rpmlftp i:/> mget pub/docs/heartbeat/rhel6/new/*[root@ha1 ~]# ls 在6.5系统中使用3.0.4-2版本。6.4可以使用3.0.4-1heartbeat-3.0.4-2.el6.x86_64.rpm heartbeat-libs-3.0.4-2.el6.x86_64.rpm ldirectord-3.9.2-1.2.x86_64.rpmheartbeat-devel-3.0.4-2.el6.x86_64.rpm[root@ha1 ~]# yum localinstall * -y[root@ha1 ~]# yum install httpd -y[root@ha1 html]# vim index.html 写入内容1或2[root@ha1 ~]# cd /usr/share/doc/heartbeat-3.0.4/[root@ha1 heartbeat-3.0.4]# cp ha.cf haresources authkeys /etc/ha.d/[root@ha1 heartbeat-3.0.4]# cd /etc/ha.d/ 可以查看README.conf修改配置文件[root@ha1 ha.d]# vim ha.cfdebugfile /var/log/ha-debug 记录日志信息,也可以打开logfile /var/log/ha-logkeepalive 2deadtime 30warntime 10initdead 60udpport 694 广播端口bcast eth0 # Linux 广播的接口watchdog /dev/watchdog 看门狗,长时间没反映重启node ha1.example.com 结点1,优先node ha2.example.comping 192.168.2.251 用来检测是否存活,一般ping自己的网关respawn hacluster /usr/lib64/heartbeat/ipfail 64位的在lib64下apiauth ipfail gid=haclient uid=hacluster[root@ha1 ha.d]# modprobe softdog添加看门狗模块可将其写在/etc/rc.local中[root@ha1 ha.d]# vim haresourcesha1.example.com IPaddr::192.168.2.232/24/eth0:0 httpd 脚本位置/etc/init.d/[root@ha1 ha.d]# vim authkeysauth 1 使用方式1,比较不安全1 crc[root@ha1 ha.d]# chmod 600 authkeys 这个文件的权限是600然后把配置文件发给另一个结点[root@ha1 ha.d]# scp ha.cf haresources authkeys ha2.example.com:/etc/ha.d/节点h1,h2依次开启hearbeat,其他都不用开,heartbeat会自动管理资源[root@ha1 ~]# /etc/init.d/heartbeat start[root@ha1 ~]# ip addr show查看232启动在哪个结点上测试[root@ha1 ~]# /etc/init.d/heartbeat stop 浏览器访问虚拟ip停止结点1的heartbeat,http会跳转到结点2,所以这个仅对heartbeat检测,不针对服务,再次开启会跳转回来。

2、添加LB做http负载均衡

[root@ha1 ~]# yum install -y ipvsadm 默认已经安装;暂时使用命令添加,整合使用ldirectord维护添加[root@ha1 ~]# ipvsadm -A -t 192.168.2.232:80 -s rr 添加虚拟IP 使用轮叫[root@ha1 ~]# ipvsadm -a -t 192.168.2.232:80 -r 192.168.2.78:80 -g 添加LB服务器1[root@ha1 ~]# ipvsadm -a -t 192.168.2.232:80 -r 192.168.2.221:80 -g 添加LB服务器2[root@ha1 ~]# ipvsadm -ln 查看LB服务器均做如下配置:[root@lb1 ~]# yum install httpd -y[root@lb1 ~]# vim /var/www/html/index.html 写入lb1或lb2[root@lb1 ~]# ifconfig eth0:0 192.168.2.232 netmask 255.255.255.255 使用DR方式,添加用于返回用户数据所使用的IP地址,写入rc.local,否则重启失效[root@lb1 ~]# yum install arptables_jf.x86_64 -y[root@lb1 ~]# arptables -A IN -d 192.168.2.232 -j DROP 进来的数据目的地址改为虚拟IP[root@lb1 ~]# arptables -A OUT -s 192.168.2.232 -j mangle –mangle-ip-s 192.168.2.78 出去的源地址改为虚拟IP[root@lb1 ~]# /etc/init.d/arptables_jf save[root@lb1 ~]# /etc/init.d/arptables_jf start[root@lb2 ~]# chkconfig httpd on[root@lb2 ~]# chkconfig arptables_jf on之后浏览器访问192.168.2.232就会轮寻。

3、优化整合HA+LB

lvs缺陷,不会对后端服务的状态进行检测,服务停止依然会调度,应该是把坏掉的服务器从调度列表中删掉。所以使用ldirectord维护调度列表,最后使用heartbeat管理ldirectord资源。整合ldirectord和交由heartbeat管理[root@ha1 ~]# cp /usr/share/doc/packages/ldirectord/ldirectord.cf /etc/ha.d/[root@ha1 ~]# vim /etc/ha.d/ldirectord.cfvirtual=192.168.2.232:80 虚拟IP地址 real=192.168.2.78:80 gate 服务器1 real=192.168.2.221:80 gate 服务器2 fallback=127.0.0.1:80 gate 当所有服务停掉之后,自己充当服务器 service=http scheduler=rr #persistent=600 #netmask=255.255.255.255 protocol=tcp checktype=negotiate checkport=80 request="index.html" 检测文件来判断http状态# receive="Test Page"# virtualhost=www.x.y.z[root@ha1 ~]# vim /etc/ha.d/haresources 添加资源ha1.example.com IPaddr::192.168.2.232/24/eth0:0 httpd ldirectord[root@ha1 ~]# ipvsadm -C 清除调度列表[root@ha1 ~]# yum install -y perl-IO-Socket-INET6[root@ha1 ~]# /etc/init.d/ldirectord start[root@ha1 ~]# ipvsadm -ln 查看调度列表,ldirectord自动维护,可以关闭LB服务器的HTTP测试TCP 192.168.2.232:80 rr -> 192.168.2.78:80 Route 1 0 0 -> 192.168.2.221:80 Route 1 0 0最后,交由heartbeat管理资源ldirectord[root@ha1 ~]# scp /etc/ha.d/ldirectord.cf /etc/ha.d/haresources ha2.example.com:/etc/ha.d/关闭ha所有资源[root@ha1 ~]# ifconfig eth0:0 down[root@ha1 ~]# /etc/init.d/httpd stop[root@ha1 ~]# /etc/init.d/ldirectord stop[root@ha1 ~]# /etc/init.d/heartbeat stop开启heartbeat[root@ha1 ~]# /etc/init.d/heartbeat start之后h1会自动开启eth0:0,http,ldirectord。最后测试,ha1,lb1,lb2工作访问,可以正常轮寻关闭ha1的heartbeat,ha2资源自动启动,可以正常轮寻关闭lb2的http,查看ha2的调度列表,ipvsadm -lnTCP 192.168.2.232:80 rr -> 192.168.2.78:80 Route 1 0 8 再关闭lb1的http,查看 -> 127.0.0.1:80 Local 1 0 0 再次访问,调度器自己充当http服务器;再次开启lb的http,可以正常轮寻,再开启ha1的heartbeat,ha1再次接管调度。

如果困难是堵砖墙,拍拍它说你还不够高。

linux学习之使用heartbeat搭建高可用lvs集群服务

相关文章:

你感兴趣的文章:

标签云: