heartbeat+nginx

第二章  Heartbeat原理

 

 

高可用原理:兩臺機器AB,正常是A提供服務,B閒置,當A宕機或服務宕掉,切換到B繼續提供服務,使用心跳進行通信和選舉

 

heartbeat原理:心跳檢測和資源接管兩個部分。集羣中的主機互相發送報文,告訴對方自己當前的狀態,如果在一定時間內未收到對方報文,認定對發死亡

 

心跳線的作用(內網IP):互相發送報文AB兩臺虛擬機上都裝nginxheartbeatAB兩臺虛擬機都把heartbeat起來,但不啓動nginx

     

     這時主節點上的nginx會自動啓動,並獲得VIP

      

     正常情況下是A提供服務,VIPA上,如果A上的nginx出現故障停止工作,VIP會漂移到B上,B上的nginx也會自動啓動,以此達到高可用效果

 

          第三章  安裝前準備

1.業務設計

1.系統:倆臺centos 6.5【參考文檔一vmware安裝centos6.5

2.IP、主機名和心跳線IP設置

業務IP:192.168.1.244   主機名node1    內網心跳線IP192.168.8.244

業務IP:192.168.1.245   主機名node2    內網心跳線IP192.168.8.245

3.虛擬vip

Vip192.168.1.240     訪問IP

4.安裝軟件包

libnetfilter_conntrack-devel-0.0.100-2.el6.x86_64.rpm

libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm

libxml2-static-2.7.6-14.el6.x86_64.rpm

rpm -ivh epel-release-6-8.noarch.rpm

libnet-1.2-rc3.tar.gz

heartbeat-2.0.6.tar.gz

ngx_cache_purge-2.3.tar.gz

nginx-1.9.7.tar.gz

 

2.安裝前準備

1.關閉防火牆

# service iptables stop

#chkconfig iptables off

改完後確認

# service iptables status

Iptables: 未運行防火牆

 

2.關閉selinux

 

修改/etc/selinux/config 文件,將SELINUX=enforcing改爲SELINUX=disabled或SELINUX=permissive

# vi /etc/selinux/config

……

SELINUX=disabled

……

 

 

3.時間同步 

在兩臺主機中分別查看時間是否同步

[root@localhost ~]# date

如果同步,則無需修改,否則要進行同步,具體步驟如下:

若是聯網的狀態,執行以下命令:      

ntpdate time.nist.gov

若是內網的狀態,執行下列操作:

A、在ip192.168.1.244中執行命令

vi /etc/ntp.conf

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap  //允許192.168.1.0的網段同步

/etc/init.d/ntpd restart

 

B、在ip192.168.1.245中執行命令

vi /etc/ntp.conf

192.168.1.244

/etc/init.d/ntpd restart

 

C、同步主時間服務器時間,執行命令

ntpdate 192.168.1.244                               

 

4.互相添加對方主機名解析

主節點   IP:192.168.1.244   主機名node1    內網心跳線IP192.168.8.244

 

添加主機名node1

[root@localhost ~]# vi /etc/sysconfig/network

NETWORKING=yes

#HOSTNAME=localhost.localdomain

HOSTNAME=node1

:wq 保存退出

查看添加之後的結果

 

 

 

解析備節點主機名(注意備節點IP和主機名)

[root@localhost ~]# vi /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.245   node2

:wq 保存退出

查看添加結果

 

 

 

添加內網心跳線

 

[root@localhost ~]# cd /etc/sysconfig/network-scripts/

[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0

[root@localhost network-scripts]# vi ifcfg-eth0:0

DEVICE=eth0:0                               網卡eth0:0

#HWADDR=00:0c:29:4f:02:ee                   #

TYPE=Ethernet

#UUID=7995f1d7-8789-43f1-b17e-c0441dfea01b   #

NM_CONTROLLED=yes

BOOTPROTO=static                            static

IPV6INIT=no

USERCTL=no

IPADDR=192.168.8.244                      心跳線

NETMASK=255.255.255.0

:wq 保存退出

 

重啓網卡

[root@node1 network-scripts]# service network restart

[root@node1 network-scripts]# ifconfig

 

 

備節點    IP:192.168.1.245   主機名node2    內網心跳線IP192.168.8.245

 

添加主機名node2

[root@localhost ~]# vi /etc/sysconfig/network

NETWORKING=yes

#HOSTNAME=localhost.localdomain

HOSTNAME=node2

:wq 保存退出

查看添加之後的結果

 

 

 

 

解析主節點主機名(注意主節點IP和主機名)

[root@localhost ~]# vi /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.244   node1

:wq 保存退出

查看添加結果

 

 

 

添加內網心跳線

[root@localhost ~]# cd /etc/sysconfig/network-scripts/

[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0

[root@localhost network-scripts]# vi ifcfg-eth0:0

 

DEVICE=eth0:0                                 網卡eth0:0 

#HWADDR=00:0c:29:ca:2f:3f                      #

TYPE=Ethernet

#UUID=e0a7f7dd-2d1b-4ae0-8a6c-0e696f519baa      #

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

USERCTL=no

IPV6INIT=no

IPADDR=192.168.8.245                          心跳線

NETMASK=255.255.255.0

:wq 保存退出

 

 

重啓網卡

[root@localhost network-scripts]# service network restart

[root@localhost network-scripts]# ifconfig

 

 

5.主節點和備節點重啓系統

 

主節點   IP:192.168.1.244

 

 

備節點    IP:192.168.1.245

 

 

 

 

 

 

           第四章  通用環境

 

1.配置本地yum環境

cd /etc/yum.repos.d/

ls

mkdir repo.bak

cp CentOS-* repo.bak/

rm -rf CentOS-*

vi aa.repo

寫入以下內容

[aa]

name=aa

baseurl=file:///mnt

gpgcheck=1

:wq 保存退出

 

 

2.掛載

[root@node1 yum.repos.d]# mount /dev/dvd /mnt

[root@node1 yum.repos.d]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-*

先將安裝包通過ftp分別傳輸到兩臺主機的root文件夾下

 

3.安裝rpm

先將安裝包通過ftp分別傳輸到兩臺主機的root文件夾下

[root@node1 ~]#rpm -ivh libnetfilter_conntrack-devel-0.0.100-2.el6.x86_64.rpm libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm libxml2-static-2.7.6-14.el6.x86_64.rpm --force --nodeps

 

4.yum安裝

[root@node1 ~]#yum -y install glib2-devel libtool-ltdl-devl net-snmp-devel bzip2-devel ncurses-devel openssl-devel libtoollibxml2-devel gettext bison flex zlib-devel mailx which libxslt docbook-dtds docbook-style-xsl PyXML shadow-utils opensp autoconf automake gcc make gcc-c++ kgconfig glib2-devel  e2fsprogs libnet*  libxml2*  PyXML  gcc+  openssl  pcre-develzlib-devel 

 

 

5.epel

[root@node1 ~]# rpm -ivh epel-release-6-8.noarch.rpm

 

 

6.添加組

[root@node1 ~]# groupadd haclient

[root@node1 ~]# useradd hacluster -g haclient

7.Libnet安裝

[root@node1 ~]# tar zxvf libnet-1.2-rc3.tar.gz

[root@node1 ~]# cd libnet-1.2-rc3

[root@node1 libnet-1.2-rc3]# ./configure

[root@node1 libnet-1.2-rc3]# make

[root@node1 libnet-1.2-rc3]# make install

拷貝動態庫

cp  -r /usr/local/lib/* /usr/lib64/

 

8.Hrartbeat安裝

[root@node1 ~]# tar xf heartbeat-2.0.6.tar.gz

[root@node1 ~]# cd heartbeat-2.0.6

[root@node1 heartbeat-2.0.6]# ./ConfigureMe configure

[root@node1 heartbeat-2.0.6]#make

make 編譯報錯,去這三個路勁下刪除makefile中的所有-Werror

 

 vi /root/heartbeat-2.0.6/lib/hbclient/Makefile

 vi /root/heartbeat-2.0.6/lib/plugins/stonith/Makefile

 vi /root/heartbeat-2.0.6/telecom/recoverymgrd/Makefile

 

 

 

再次編譯

[root@node1 heartbeat-2.0.6]#make

 

[root@node1 heartbeat-2.0.6]#make install

 

 

9.加入開機啓動

[root@node1 ~]# chkconfig --add heartbeat

[root@node1 ~]# chkconfig heartbeat on 35

 

 

 

 

         第五章 nginx安裝及緩存配置

1.Nginx安裝

 

上傳軟件包到/root    

[root@node1~]#mkdir-p/usr/local/nginx

[root@node1~]#tar zxvf ngx_cache_purge-2.3.tar.gz

[root@node1~]#tar zxvf nginx-1.9.7.tar.gz

[root@node1~]#cd nginx-1.9.7

[root@node1nginx-1.9.7]#./configure--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module--with-http_sub_module--add-module=../ngx_cache_purge-2.3加入緩存模塊

[root@node1 nginx-1.9.7]#make

[root@node1 nginx-1.9.7]#make install

[root@node1 nginx-1.9.7]#cd/usr/local/nginx

啓動nginx

[root@node1 nginx]#./sbin/nginx

查看nginx啓動是否成功

[root@node1 nginx]#./sbin/nginx-t

 

重新載入

[root@node1 nginx]#./sbin/nginx-s reload

查看nginx進程

[root@node1 nginx]#ps-ef | grep nginx

 

2.Nginx配置文件

cd /usr/local/nginx/

vi conf/nginx.conf

user nobody;

 

worker_processes 1;

啓動進程,通常設置成和cpu的數量相等

 

#error_log logs/error.log;

#error_log logs/error.log notice;

 

error_log logs/error.log info;

打開定義錯誤日誌的路徑及其日誌級別

 

pid logs/nginx.pid;打開pid

events{

worker_connections 1024;

use epoll;   #Linux最常用支持大併發的事件觸發機制

 

}

 

#設定http服務器,利用它的反向代理功能提供負載均衡支持

http{

include mime.types;

default_type application/octet-stream;

 

 

#配置緩存目錄

client_body_buffer_size 32k;

proxy_connect_timeout 10;

proxy_read_timeout 180;

proxy_send_timeout 5;

proxy_buffer_size 16k;

proxy_buffers 4 64k;

proxy_temp_file_write_size 256k;

proxy_busy_buffers_size 128k;

proxy_temp_path/home/admin/workspace/nginx/temp;

proxy_cache_path/home/admin/workspace/nginx/cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=10g;

#日誌的格式

log_format access.log'$remote_addr-[$time_local]-"$request"-"$http_user_agent"';



#log_format main'$remote_addr-$remote_user[$time_local]"$request"'

#'$status$body_bytes_sent"$http_referer"'

#'"$http_user_agent""$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;

 

 

tcp_nopush on;

tcp_nodelay on;

 

 

#keepalive_timeout 0;

keepalive_timeout 65;

 

#gzip on;

 

配置負載均衡服務器列表

upstream heren-master{

server 192.168.1.235:8051weight=1 max_fails=2 fail_timeout=30s;

server 192.168.1.238:8051weight=1 max_fails=2 fail_timeout=30s;

ip_hash;

}

#weight設置的權重max_fails設置的是健康監測失敗次數,timeout設置超時時間

upstream heren-message{

server 192.168.1.235:9999weight=1 max_fails=2 fail_timeout=30s;

}

upstream heren-report{

server 192.168.1.235:8081;

}

upstream heren-schedule{

server 192.168.1.235:8061;

}

 

 

配置主機

server{

listen 80;

server_name localhost;

 

#charset koi8-r;

 

#access_log logs/host.access.log main;

 

 

 

配置業務訪問url


  

location/{

root html;

#index index.html index.htm;      增加#

}

 

 

 

 

 

 

 

 

配置緩存規則

location~.*\.(gif|jpg|jpeg|png|bmp|sqf|js|css)$

{

proxy_next_upstream http_502 http_504 error timeout invalid_header;

proxy_cache cache_one;

proxy_cache_valid 200 304 12h;

proxy_cache_valid any 1m;

proxy_set_header Host$host;

proxy_set_header X-Real-IP$remote_addr;

proxy_ignore_headers"Cache-Control""Expires";

proxy_pass http://heren-master;

expires 1h;

}

#設置js|css在瀏覽器中過期的時間

location~.*\.(js|css)

{

expires 1h;

}

#設置清除所有的cache

location~/purge(/.*)

{

allow 127.0.0.1;

allow 192.168.243.0/24;

deny all;

proxy_cache_purge cache_one$host$1$is_args$args;

}

}

}

 

 

4.上傳nginx腳本.

nginx啓動腳本通過ftp傳輸到到文件夾/etc/init.d//etc/ha.d/resource.d/

賦予nginx腳本執行權限

[root@node1 ha.d]# cd /etc/init.d

[root@node1 init.d]# ll

 

如若出現上面情況,執行下面命令

 

[root@node1 init.d]# cp  nginx?ű? nginx

[root@node1 init.d]# rm -rf  nginx?ű?

[root@node1 init.d]# chmod +x nginx

[root@node1 init.d]# ll

 

 

 

 

 

文件夾/etc/ha.d/resource.d/下的nginx權限修改與上述相同,此處不再贅述

 

 

 

 

         第六章  配置heartbeat

 

1.主節點

拷貝配置文件到/etc

 

[root@node1 heartbeat-2.0.6]# cp doc/ha.cf  /etc/ha.d/

[root@node1 heartbeat-2.0.6]# cp doc/haresources  /etc/ha.d/

[root@node1 heartbeat-2.0.6]# cp doc/authkeys  /etc/ha.d/

A、主配置文件

[root@node1 heartbeat-2.0.6]# cd /etc/ha.d

[root@node1 ha.d]# ls

[root@node1 ha.d]# vi ha.cf

 

【把下面代碼前面的#都刪掉之後保存退出
# 開啓HAdebug日誌,建議調試完後關閉此日誌
debugfile /var/log/ha-debug


# 開啓HA日誌
logfile    /var/log/ha-log


# 設置日誌打印級別
logfacility    local0


# 多長時間建材一次心跳
keepalive 2


# 連續多長時間檢測失敗示對方掛掉,單位秒
deadtime 30


# 連續多長時間檢測失敗開始警告提示,單位秒
warntime 10


# 爲服務重啓預留一段時間,在這段時間不進行心跳檢測
initdead 120


# 默認端口是UDP 694,如果在局域網還有人在玩Heartbeat,並且他用廣播,你最好改個端口
# 否則可能會導致認證失敗
udpport    694


# 使用單播通信,在HA2上修改爲ucast   eth1 192.168.10.1
ucast    eth0 192.168.8.245     填寫備節點心跳線IP


# 主節點恢復正常後是否再切換回來
auto_failback on


# 設置看門狗
# Watchdog在實現上可以是硬件電路也可以是軟件定時器,能夠在系統出現故障時自動重新啓動系統。
# Linux 內核下,
watchdog的基本工作原理是:當watchdog啓動後(/dev/watchdog
設備被打開後)


# 如果在某一設定的時間間隔內/dev/watchdog沒有被執行寫操作,
# 硬件watchdog電路或軟件定時器就會重新啓動系統。
watchdog /dev/watchdog


# 節點列表,主節點在前,不要寫反了

刪掉ken3kathy,修改如下
node    node1
node    node2

以上全部修改完之後保存退出

 

B、配置認證文件

[root@node1 ha.d]# vi authkeys

同樣把以下代碼前面的#”去掉

auth 1
1 crc

保存退出


修改authkeys權限爲600
[root@node1 ha.d]# chmod  600 authkeys

 

C配置資源文件

[root@node1 ha.d]# vi haresources

加入下列代碼

node1 IPaddr::192.168.1.240/24/eth0 nginx

修改後的結果

 

保存退出

 

 

2.備用節點  

192.168.1.245 主機名 node2

cd /etc/ha.d

vi ha.cf

ucast    eth0 192.168.8.245修改爲192.168.8.244  填寫主節點心跳線ip

authkeysharesource必須和主節點一模一樣

 

 

 

             第七章  測試

 

啓動heartbeat,注意nginx不需要啓動

1.主節點

[root@node1 ~]# service heartbeat start

[root@node1 ~]# ps -ef |grep nginx

 

查看heartbeat狀態

 

 

[root@node1 ~]# tailf /var/log/messages

 

[root@node1 ~]# ip a

 

 

2.備用節點

[root@node2 nginx]# service heartbeat start

[root@node2 nginx]# ps -ef |grep nginx

 

 

[root@node2 nginx]# service heartbeat status

      

[root@node2 nginx]# tailf /var/log/messages

 

[root@node2 nginx]# ip a

 

 

3.停掉主節點Heartbeat

[root@node1 ~]# service heartbeat stop

同時nginx 也自動停掉了,查看nginx進程,vip也飄移到node2上面了

[root@node1 ~]# ps -ef |grep nginx

 

[root@node1 ~]# ip a

 

 

查看備用節點,同時nginx 啓動,vip飄移到node2

 

[root@node2 nginx]# ip a

 

 

 

             

 

            第八章  報錯

報錯一:

Heartbeat安裝到 make 報錯不是 make install

cc1: warnings being treated as errors

pils.c:245: error: initialization fromincompatible pointer type

pils.c:246: error: initialization fromincompatible pointer type

gmake[2]: *** [pils.lo] 錯誤 1

gmake[2]: Leaving directory`/root/heartbeat-2.0.7/lib/pils'

gmake[1]: *** [all-recursive]錯誤 1

gmake[1]: Leaving directory`/root/heartbeat-2.0.7/lib'

make: *** [all-recursive] 錯誤 1

則將/root/heartbeat-2.0.7/lib/pils裏的makefile中的所有-Werror刪除

如報

cc1: warnings being treated as errors

client_lib.c:1850: error: 'display_orderQ'defined but not used

gmake[2]: *** [client_lib.lo]錯誤 1

gmake[2]: Leaving directory`/root/heartbeat-2.0.7/lib/hbclient'

gmake[1]: *** [all-recursive]錯誤 1

gmake[1]: Leaving directory`/root/heartbeat-2.0.7/lib'

make: *** [all-recursive] 錯誤 1

則將/root/heartbeat-2.0.7/lib/hbclient裏的makefile中的所有-Werror刪除

如報

cc1: warnings being treated as errors

stonith_signal.h:34: error:'stonith_signal_set_simple_handler' defined but not used

gmake[4]: *** [apcmaster.lo]錯誤 1

gmake[4]: Leaving directory`/root/heartbeat-2.0.7/lib/plugins/stonith'

gmake[3]: *** [all-recursive]錯誤 1

gmake[3]: Leaving directory`/root/heartbeat-2.0.7/lib/plugins/stonith'

gmake[2]: *** [all-recursive]錯誤 1

gmake[2]: Leaving directory`/root/heartbeat-2.0.7/lib/plugins'

gmake[1]: *** [all-recursive]錯誤 1

gmake[1]: Leaving directory`/root/heartbeat-2.0.7/lib'

make: *** [all-recursive] 錯誤 1

則將/root/heartbeat-2.0.7/lib/plugins/stonith裏的makefile中的所有-Werror刪除

如報

cc1: warnings being treated as errors

conf_lex.c:1195: 錯誤:‘input’定義後未使用

gmake[2]: *** [recoverymgrd-conf_lex.o]錯誤 1

gmake[2]: Leaving directory`/root/heartbeat-2.0.7/telecom/recoverymgrd'

gmake[1]: *** [all-recursive]錯誤 1

gmake[1]: Leaving directory `/root/heartbeat-2.0.7/telecom'

make: *** [all-recursive] 錯誤 1

則將/root/heartbeat-2.0.7/telecom/recoverymgrd裏的makefile中的所有-Werror刪除

如報

chown hacluster/var/lib/heartbeat/cores/hacluster

chown: 無效的用戶:"hacluster"

gmake[2]: [install-exec-local]錯誤 1 (忽略)

chmod 700/var/lib/heartbeat/cores/hacluster

gmake[2]: Nothing to be done for`install-data-am'.

gmake[2]: Leaving directory`/root/heartbeat-2.0.7'

gmake[1]: Leaving directory`/root/heartbeat-2.0.7'

則證明沒添加用戶及組,按照上面的添加用戶及組後再編譯安裝。

結束後出現

chown hacluster /var/lib/heartbeat/cores/hacluster

chmod 700/var/lib/heartbeat/cores/hacluster

gmake[2]: Nothing to be done for`install-data-am'.

gmake[2]: Leaving directory`/root/heartbeat-2.0.7'

gmake[1]: Leaving directory`/root/heartbeat-2.0.7'

 

 vi /root/heartbeat-2.0.6/lib/hbclient/Makefile

 vi /root/heartbeat-2.0.6/lib/plugins/stonith/Makefile

 vi /root/heartbeat-2.0.6/telecom/recoverymgrd/Makefile

 

報錯二:

日誌信息報錯

原因沒有找到動態庫

 node1 IPaddr[2451]: ERROR: Could not send gratuitous arps. rc=127

 

 

沒有發現動態庫

libnet.so.9 => not found  

 

 

 cp  -r /usr/local/lib/* /usr/lib64/

 

 

查看日誌

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