Linux架構之HA配置

簡介html

經過安裝該Linux-HA軟件,能夠實現Linux雙機系統的高可用性解決方案,實現雙機系統的熱備份,並可以作到雙機之間的無縫切換,從而對外node

提供穩定可靠的服務,最終實現系統高性能RAS(reliability, availability, and serviceability)。linux


 實驗環境nginx

wKioL1ht48KxNpH7AAHvvrDzldU315.png-wh_50

這裏使用heartbeat來作HA集羣,而且把nginx服務做爲HA對應的服務。shell


Chumjtest01 eth0 172.31.24.199  eth0:1   192.168.1.109 VIP: 172.31.24.202
Chumjtest02 eth0 172.31.24.200 eth0:1   192.168.1.110 VIP: 172.31.24.202

實驗準備工做vim

 1.編輯修改hosts文件,將兩臺服務器的IP添加上,
bash

 2. 關閉防火牆 iptables -F;  
   關閉selinux: setenforce 0
服務器

 3.安裝好eprl,安裝好heartbeat、libnet網絡


主配置curl

[root@chumjtest01 ~]# cd /usr/share/doc/heartbeat-3.0.4/
[root@chumjtest01 heartbeat-3.0.4]# cp  authkeys  ha.cf haresources   /etc/ha.d/
[root@chumjtest01 heartbeat-3.0.4]# cd /etc/ha.d/
[root@chumjtest01 ha.d]# ls
authkeys  ha.cf  harc  haresources  rc.d  README.config  resource.d  shellfuncs
[root@chumjtest01 ha.d]# vim authkeys     //authkeys是驗證主從的
auth 3           //選擇3 使用md5 驗證
#1 crc
#2 sha1 HI!
3 md5 Hello!      //去掉#號,表示打開md5來驗證 
[root@chumjtest01 ha.d]# chmod 600 authkeys  //賦予root讀寫權限。


配置VIP

[root@chumjtest01 ha.d]# cd /etc/sysconfig/network-scripts/
[root@chumjtest01 network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1  //新建一個IP配置文件,同一個網卡能夠配置多個IP
[root@chumjtest01 network-scripts]# vim ifcfg-eth0:1    //編輯VIP
DEVICE=eth0:1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=172.31.24.202    //將IP地址改爲VIP地址,去掉一些不要的內容。
PREFIX=24
#GATEWAY=172.31.24.1
DEFROUTE=yes
[root@chumjtest01 network-scripts]# service network restart    //重啓下網絡服務
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:  Determining if ip address 172.31.24.199 is already in use for device eth0...
Determining if ip address 172.31.24.198 is already in use for device eth0...
                                                           [  OK  ]
[root@chumjtest01 network-scripts]# ifconfig     //能夠看到VIP網卡已經顯示
eth0      Link encap:Ethernet  HWaddr 00:50:56:B3:79:E3  
          inet addr:172.31.24.199  Bcast:172.31.24.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:feb3:79e3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:22 errors:0 dropped:0 overruns:0 frame:0
          TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2130 (2.0 KiB)  TX bytes:3428 (3.3 KiB)
eth0:1    Link encap:Ethernet  HWaddr 00:50:56:B3:79:E3  
          inet addr:172.31.24.198  Bcast:172.31.24.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)


配置節點文件

[root@chumjtest01 /]# cd /etc/ha.d/
[root@chumjtest01 ha.d]# vim haresources
#node1  10.0.0.170 Filesystem::/dev/sda1::/data1::ext2
chumjtest01     172.31.24.198:/24/eth0:0 nginx      //添加主機名、VIP和須要集羣的服務


配置ha配置文件

[root@chumjtest01 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 172.31.24.200    //對方的IP地址
auto_failback on
node    chumjtest01    //node1地址
node    chumjtest02    //node2地址
ping 172.31.24.1        //仲裁地址,通常用路由器或者交換機
respawn hacluster /usr/lib64/heartbeat/ipfail



從機配置

將主機上的配置文件拷貝到從機上

[root@chumjtest01 ha.d]scp authkeys ha.cf haresources chumjtest02:/etc/ha.d/
只要修改
vi  /etc/ha.d/ha.cf 
 //只須要更改一個地方
ucast eth1 172.31.24.200 改成   ucast eth1 172.31.24.199



啓動heartbeat

先啓動主,在啓動從

主機啓動狀況:

[root@chumjtest01 ha.d]# service heartbeat start
Starting High-Availability services: INFO:  Resource is stopped
Done.

檢查下nginx服務有沒有啓動

[root@chumjtest01 ha.d]# ps aux |grep nginx
root      6989  0.0  0.0 109336  2036 ?        Ss   17:40   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     6990  0.0  0.0 109760  2704 ?        S    17:40   0:00 nginx: worker process                   
nginx     6991  0.0  0.0 109760  2756 ?        S    17:40   0:00 nginx: worker process                   
root      6995  0.0  0.0 103252   824 pts/1    S+   17:41   0:00 grep nginx


從機啓動狀況:

[root@chumjtest02 ha.d]# service heartbeat start
Starting High-Availability services: INFO:  Resource is stopped
Done.

測試

在主機的nginx下寫一個簡單的html文件,而且訪問下

[root@chumjtest01 /]# cd /usr/share/nginx/html
[root@chumjtest01 html]# vim 1.html
chumjtest01
[root@chumjtest01 ha.d]# curl http://172.31.24.199/1.html    //測試下顯示chumjtest01
chumjtest01

一樣在從機上寫下一個簡單的html文件

[root@chumjtest02 html]# vim 1.html
chumjtest02
[root@chumjtest02 html]# curl http://172.31.24.200/1.html   //網頁沒法顯示,說明Nginx沒啓動
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /1.html was not found on this server.</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at 172.31.24.200 Port 80</address>
</body></html>

在測試下VIP訪問,說明如今在nginx在主機上運行

[root@chumjtest01 ha.d]# curl http://172.31.24.202/1.html
chumjtest01

打開防火牆,將主機禁ping

[root@chumjtest01 html]# iptables -I INPUT -p icmp -j DROP

查看主機chumjtest01的HA日誌,看到「We are dead」,表示主機認爲本身已經死了,要切到從機上

Jan 05 13:55:29 chumjtest01 ipfail: [21826]: info: NS: We are dead. :<

查看從機chumjtest02的HA日誌,從機啓動ngix。

ResourceManager(default)[3176]: 2017/01/05_13:55:44 info: Running /etc/init.d/nginx  start

在訪問下VIP的地址,已經切換到從機上去了。

[root@chumjtest01 html]# curl http://172.31.24.202/1.html
chumjtest02

注:

heartbeat日誌在/var/log/ha-log,若是啓動有問題請儘可能查看日誌。


1.ERROR: Client child command [/usr/lib/heartbeat/ipfail] is not executable

說明64位系統,要在ha.cf下的/usr/lib64/heartbeat/ipfail


2.ERROR: Illegal directive [node ] in /etc/ha.d//ha.cf

ucast eth0 172.31.24.200 要注意在ha.cf下網絡端口是否匹配

相關文章
相關標籤/搜索