您的位置:首页 > 运维架构 > Linux

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

2014-05-25 15:30 966 查看


使用heartbeat搭建HA+LB集群

实验环境,注意时间同步和解析,火墙和selinux关闭

192.168.2.168   ankse.example.com

192.168.2.72    ha1.example.com

192.168.2.68    ha2.example.com

192.168.2.78    lb1.example.com

192.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 Repository

baseurl=http://192.168.2.251/pub/rhel6.5

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

# HighAvailability rhel6.5

[HighAvailability]

name=Instructor HighAvailability Repository

baseurl=http://192.168.2.251/pub/rhel6.5/HighAvailability

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

# LoadBalancer packages

[LoadBalancer]

name=Instructor LoadBalancer Repository

baseurl=http://192.168.2.251/pub/rhel6.5/LoadBalancer

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

# ResilientStorage

[ResilientStorage]

name=Instructor ResilientStorage Repository

baseurl=http://192.168.2.251/pub/rhel6.5/ResilientStorage

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

# ScalableFileSystem

[ScalableFileSystem]

name=Instructor ScalableFileSystem Repository

baseurl=http://192.168.2.251/pub/rhel6.5/ScalableFileSystem

gpgcheck=1

gpgkey=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.rpm 

lftp i:/> mget pub/docs/heartbeat/rhel6/new/*

[root@ha1 ~]# ls 在6.5系统中使用3.0.4-2版本。6.4可以使用3.0.4-1

heartbeat-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.rpm

heartbeat-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.cf 

debugfile /var/log/ha-debug 记录日志信息,也可以打开logfile /var/log/ha-log

keepalive 2

deadtime 30

warntime 10

initdead 60

udpport 694 广播端口

bcast   eth0            # Linux 广播的接口

watchdog /dev/watchdog 看门狗,长时间没反映重启

node    ha1.example.com 结点1,优先

node    ha2.example.com

ping 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 haresources 

ha1.example.com IPaddr::192.168.2.232/24/eth0:0 httpd 脚本位置/etc/init.d/ 

[root@ha1 ha.d]# vim authkeys 

auth 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.cf 

virtual=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 -ln

TCP  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