利用heartbeat構建高可用http

一、實驗準備:

 1)實驗環境:     

  1.png

 2)同步時間;確??梢允褂弥鳈C名通信;確保可以使用ssh秘鑰方式進行彼此登錄;由于是兩臺設備,需要仲裁;

 確保可以使用主機名通信

web1修改hosts文件如下:
172.16.2.12 web1.linux.com web1
172.16.2.14 web2.linux.com web2

web2修改hosts文件如下:
172.16.2.12 web1.linux.com web1
172.16.2.14 web2.linux.com web2

 確保主機可以使用ssh秘鑰彼此登錄

[root@web1~]# ssh-keygen -P '' \\生成秘鑰

[root@web1~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.2.14  \\將公鑰復制給web2

[root@web2 ~]# ssh-keygen -P ''  \\生成秘鑰

[root@web2 ~]# ssh-copy-id  -i .ssh/id_rsa.pub  root@172.16.2.12    \\將公鑰復制給web1

確保主機之間時間同步

[root@web1 ~]# crontab -e
*/5 * * * * /usr/sbin/ntpdate   172.16.2.13
   
[root@web2 ~]# crontab -e                                              
*/5 * * * * /usr/sbin/ntpdate 172.16.2.13

 測試

[root@web1 ~]# date; ssh web2 'date'
Sat Jun 27 11:32:56 CST 2015
Sat Jun 27 11:32:56 CST 2015

[root@web2 ~]# date; ssh web1 'date'
Sat Jun 27 11:33:42 CST 2015
Sat Jun 27 11:33:42 CST 2015

二、安裝配置heartbeat v1

 1)安裝依賴包;前提準備好epel源

阿里云eple源
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo
[root@web1 heartbeat]# yum -y install net-snmp-libs PyXML gettext-devel libtool-ltdl pygtk2-devel  libnet
[root@web1 heartbeat]# rpm -ivh heartbeat-pils-2.1.4-12.el6.x86_64.rpm 
[root@web1 heartbeat]# rpm -ivh heartbeat-stonith-2.1.4-12.el6.x86_64.rpm 
[root@web1 heartbeat]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm 

[root@web2 heartbeat]# yum -y install net-snmp-libs PyXML gettext-devel libtool-ltdl pygtk2-devel  libnet
[root@web2 heartbeat]# rpm -vih heartbeat-pils-2.1.4-12.el6.x86_64.rpm
[root@web2 heartbeat]# rpm -ivh heartbeat-stonith-2.1.4-12.el6.x86_64.rpm 
[root@web2 heartbeat]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm

 2)對heartbeat進行配置

[root@web1 ha.d]# rpm -ql heartbeat \\查看heartbeat生成文件

[root@web1 ~]# cd  /usr/share/doc/heartbeat-2.1.4/

[root@web1 heartbeat-2.1.4]# cp authkeys ha.cf haresources  /etc/ha.d/
      authkeys :是主機之間信息傳遞使用的秘鑰
      ha.cf:是heartbeat的主配置文件
      haresource: 資源配置文件
      
[root@web1 heartbeat-2.1.4]# cd /etc/ha.d/

[root@web1 ha.d]# openssl rand -hex 6
e5b50e897cb4   \\生成秘鑰

[root@web1 ha.d]# vim authkeys  \\配置秘鑰
auth 1
1  md5 e5b50e897cb4

[root@web1 ha.d]# chmod 400 authkeys  \\修改秘鑰文件的權限;建議使用400;如果不修改權限heartbeat無法啟動

[root@web1 ha.d]# vim ha.cf
 logfile /var/log/ha-log \\ 定義日志文件
 mcast eth0 226.10.10.1 694 1 0 \\定義組播地址以及傳遞的相關屬性(mcast組播;bcast廣播;ucast單播)
 auto_failback on  \\搶占模式;主節點恢復故障后從備用節點那里搶回主節點功能
 node   web1.linux.com  \\添加ha節點;此名稱必須是“uname -n”看到的主機名
  node   web2.linux.com   \\添加ha節點;此名稱必須是“uname -n”看到的主機名
 ping 172.16.2.1  \\ 定義仲裁設備
 compression     bz2 \\啟用壓縮功能
 compression_threshold 2 \\大于多少k之后開始壓縮
 
[root@web1 ha.d]# vim haresources 
 web1.linux.com  172.16.2.100/24/eth0/172.16.2.100 httpd \\定義資源(定義VIP,httpd)
 
[root@web1 ha.d]# scp -p ha.cf haresources authkeys  web2:/etc/ha.d \\將配置好的文件復制給web2一份

 3)安裝httpd

[root@web1 ~]# yum -y install httpd ; ssh web2 'yum -y install httpd'  \\安裝httpd
[root@web1 ~]# echo  "<h1>web1</h1>" > /var/www/html/index.html    \\添加默認文檔
[root@web2 ~]#  echo  "<h1>web2</h1>" > /var/www/html/index.html    \\添加默認文檔
 
[root@web1 ~]# service httpd start   \\啟動http
Starting httpd:                                            [  OK  ]
[root@web1 ~]# curl     \\測試httpd 
<h1>web1</h1>
[root@web1 ~]# service httpd stop     \\停止httpd
Stopping httpd:                                            [  OK  ]
[root@web1 ~]# chkconfig httpd off      \\禁止開機啟動httpd

[root@web2 ~]# service httpd start
Starting httpd:                                            [  OK  ]
[root@web2 ~]# curl  http://172.16.2.14
<h1>web2</h1>
[root@web2 ~]# service httpd stop
Stopping httpd:                                            [  OK  ]
[root@web2 ~]# chkconfig httpd off

 4)啟動heartbeat

[root@web1 ~]# service heartbeat start; ssh web2 'service heartbeat start'
Starting High-Availability services: 
2015/06/27_12:40:25 INFO:  Resource is stopped
Done.

Starting High-Availability services: 
2015/06/27_12:40:25 INFO:  Resource is stopped
Done.

 5)查看啟動結果:

[root@web1 ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:0E:C4:29  
          inet addr:172.16.2.12  Bcast:172.16.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe0e:c429/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:55852 errors:0 dropped:0 overruns:0 frame:0
          TX packets:32064 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:64541607 (61.5 MiB)  TX bytes:9222342 (8.7 MiB)

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:0E:C4:29  
          inet addr:172.16.2.100  Bcast:172.16.2.100  Mask:255.255.255.0     \\VIP地址
          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:453 errors:0 dropped:0 overruns:0 frame:0
          TX packets:453 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:44156 (43.1 KiB)  TX bytes:44156 (43.1 KiB)
          
 
[root@web1 ~]# service httpd status
httpd (pid  11984) is running...         \\httd服務已啟動

[root@web1 ~]# ss -upln    \\查看heartbet v1版監聽端口,為udp的694
State       Recv-Q Send-Q                                                               Local Address:Port                                                                 Peer Address:Port 
UNCONN      0      0                                                                                *:647                                                                             *:*      users:(("portreserve",934,6))
UNCONN      0      0                                                                      226.10.10.1:694                                                                             *:*      users:(("heartbeat",12952,7),("heartbeat",12953,7))
UNCONN      0      0                                                                                *:847                                                                             *:*      users:(("portreserve",934,8))
UNCONN      0      0                                                                                *:42975                                                                           *:*      users:(("heartbeat",12952,6),("heartbeat",12953,6))

 6)web1是主節點,web2是備節點;訪問測試

  2.png

  將web1切換為備節點,web2為主節點

[root@web1 ~]# cd /usr/lib64/heartbeat/
      ha_propagate \\此腳本是將heartbeat的配置文件復制給其他節點
      hb_standby  \\此腳本是將當前節點切換為備節點
      hb_takeover \\此腳本是將當前節點切換為主節點
      
[root@web1 heartbeat]# ./hb_standby  \\將web1切換為備節點
2015/06/27_12:47:03 Going standby [all].

 訪問測試

 3.png

擴展:使用NFS為兩臺高可用主機提供NFS文件系統

  1)在時間服務器開啟NFS功能

[root@time-or-nfs ~]# vim /etc/exports 
/web/html    172.16.2.0/24(rw,no_root_squash)  \\定義共享文件訪問權限以及不壓縮root用戶權限
[root@time-or-nfs ~]# mkdir -pv /web/html  \\創建此目錄
[root@time-or-nfs ~]# service nfs start   \\ 啟動nfs
[root@time-or-nfs ~]# exportfs  -arv   \\查看nfs
exporting 172.16.2.0/24:/web/html
[root@time-or-nfs ~]# echo "<h1>NFS Page</h1>"  > /web/html/index.html \\定義web主頁

 2)掛載測試

[root@web1 ~]# mount -t nfs 172.16.2.13:/web/html /mnt
[root@web1 ~]# mount
172.16.2.13:/web/html on /mnt type nfs (rw,vers=4,addr=172.16.2.13,clientaddr=172.16.2.12)
[root@web1 ~]# cd /mnt/
[root@web1 mnt]# ls
index.html
[root@web2 ~]# umount /mnt \\卸載

[root@web2 ~]# mount -t nfs 172.16.2.13:/web/html /mnt
[root@web2 ~]# mount
172.16.2.13:/web/html on /mnt type nfs (rw,vers=4,addr=172.16.2.13,clientaddr=172.16.2.14)
[root@web2 ~]# cd /mnt/
[root@web2 mnt]# ls
index.html
[root@web2 ~]# umount /mnt  \\卸載

 3)修改資源配置文件;

[root@web1 ~]# vim /etc/ha.d/haresources \\編輯heartbeat的資源配置文件
web1.linux.com  172.16.2.100/24/eth0/172.16.2.100 Filesystem::172.16.2.13:/web/html/::/var/www/html/::nfs httpd
[root@web1 ~]# scp /etc/ha.d/haresources web2:/etc/ha.d/ \\復制新的資源配置文件給web2一份

 4)重啟啟動heartbeat

[root@web1 ~]# service heartbeat restart;ssh web2 'service heartbeat restart'
Stopping High-Availability services: 
Done.

Waiting to allow resource takeover to complete:
Done.

Starting High-Availability services: 
2015/06/27_13:08:30 INFO:  Resource is stopped
Done.

Stopping High-Availability services: 
Done.

Waiting to allow resource takeover to complete:
Done.

Starting High-Availability services: 
2015/06/27_13:08:50 INFO:  Resource is stopped
Done.

 5)查看啟動狀態

[root@web1 heartbeat]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:0E:C4:29  
          inet addr:172.16.2.12  Bcast:172.16.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe0e:c429/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:176947 errors:0 dropped:0 overruns:0 frame:0
          TX packets:117322 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:212876014 (203.0 MiB)  TX bytes:18332012 (17.4 MiB)

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:0E:C4:29  
          inet addr:172.16.2.100  Bcast:172.16.2.100  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:1246 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1246 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:118420 (115.6 KiB)  TX bytes:118420 (115.6 KiB)
          
[root@web1 ~]# mount
172.16.2.13:/web/html/ on /var/www/html type nfs (rw,vers=4,addr=172.16.2.13,clientaddr=172.16.2.12)

 6)訪問web頁面測試

4.png

原創文章,作者:馬行空,如若轉載,請注明出處:http://www.www58058.com/5715

(0)
馬行空馬行空
上一篇 2015-07-08 09:31
下一篇 2015-07-08 09:36

相關推薦

  • ntp時間服務器

    前言   ntp(Network Time protocol)是網絡時間協議,是用來使網絡中的各個計算機時間同步的一種協議。 ntp配置  1)ntp是由ntp軟件提供,如果沒有可以使用yum進行安裝  2)ntp配置文件介紹:/etc/ntp.conf   利用restrict來管理權限控制;語法格式:restric…

    Linux干貨 2015-06-24
  • PHP安全模式詳解(PHP5.4安全模式將消失)

    1. 安全模式      一直沒有用過php的safe_mode安全模式,以此說明作為日后參考。      PHP 的安全模式是為了試圖解決共享服務器(shared-server)安全問題而設立的。在結構上,試圖在 PHP 層上解決這個問題是不合理的,但修改 web 服務器層和操作系統層顯得非常不現…

    Linux干貨 2015-06-02
  • 馬哥教育N22期第七周作業

    1、創建一個10G分區,并格式為ext4文件系統; [root@localhost xuc]# cat /proc/partitions  major minor  #blocks  name    8   &…

    Linux干貨 2016-10-24
  • 系統基礎之權限管理

    權限管理: 概論:  上節,為大家介紹了用戶,和組的知識.今天為大家介紹與用戶,組息息相關的知識,權限.linux是多用戶,多任務的操作系統,面對多人的操作,安全問題就很重要,權限機制就很好的對安全進行防護,避免他人操作自己的文件.下面給大家詳細介紹權限.   首先讓我們先直觀地看下權限,對權限有個最基本的認識.以/etc/issue文件…

    Linux干貨 2016-08-04
  • 配置使用基于mysql存儲rsyslog日志信息

    配置使用基于mysql存儲rsyslog日志信息   日志對于我們來說,肯定不會陌生。每個系統應用,只要有人訪問,每時每刻都會產生大量的日志,用來記錄服務器應用的運行信息。以便于我們在服務有異常時進行查看,或是從日志記錄中提取出應用系統的運行信息。某些電商Web網站甚至會利用日志記錄去對用戶的購買行為作分析,以便更好的服務于客戶。 &nb…

    Linux干貨 2016-09-05
  • centos6.9的安裝

    先準備一個安裝虛擬機的的軟件VMware Workstation Pro和光盤centos6.9和光盤 然后打開該軟件,點擊創建新的虛擬機   然后會出現一個新建虛擬機向導,選擇典型,點下一步 繼續點下一步   把虛擬機名稱改成所裝的版本 然后新建一個文件夾,點擊瀏覽,接著點擊這個新建的文件夾,這個文件夾就是虛擬機所裝入的位置,…

    2017-07-15
欧美性久久久久