linux學習之使用heartbeat搭建高可用lvs集羣服務

使用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再次接管調度。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章