一提到HA你們都知道heartbeat,heartbeat是個很是不錯的HA開源軟件,今天咱們來介紹一下利用keepalived來作HA,keepalived的最大特色就是切換比較快,廢話少說
咱們來操做吧。
1、部署環境
兩臺服務器IP分別爲:
10.8.1.108 master
10.8.1.109 slaved
10.8.1.110 virtual_ip
2、將壓縮文件複製至Linux的opt目錄下:
keepalived-1.2.1.tar.gz
nginx64-1.2.4.tar.gz
將「解決opensel和PCRE問題方法」目錄內文件複製至opt目錄下
3、先執行如下更新包以解決安裝nginx和keepalived過程當中遇到問題:
解決安裝nginx出現問題「./configure: error: the HTTP rewrite module requires the PCRE library.」
rpm -ivh --nodeps pcre-devel-6.6-6.el5_6.1.x86_64.rpm
rpm -ivh --nodeps pcre-6.6-6.el5_6.1.x86_64.rpm
解決安裝keepalived出現問題「configure: error: !!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!! 」
rpm -ivh --nodeps e2fsprogs-devel-1.39-33.el5.x86_64.rpm
rpm -ivh --nodeps keyutils-libs-devel-1.2-1.el5.x86_64.rpm
rpm -ivh --nodeps libsepol-devel-1.15.2-3.el5.x86_64.rpm
rpm -ivh --nodeps libselinux-devel-1.33.4-5.7.el5.x86_64.rpm
rpm -ivh --nodeps krb5-devel-1.6.1-70.el5.x86_64.rpm
rpm -ivh --nodeps zlib-devel-1.2.3-4.el5.x86_64.rpm
rpm -ivh --nodeps openssl-devel-0.9.8e-22.el5.x86_64.rpm
四.安裝nginx
nginx的安裝,其餘的web軟件均可以例如apache,lighttpd等等
tar -zxvf nginx64-1.2.4.tar.gz
cd nginx64-1.2.4
執行如下命令:
./configure --with-http_stub_status_module
make && make install
5、keepalived的安裝
tar -zxvf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
分別執行如下命令:
./configure
make && make install
查看進程應該是三個:
ps -ef|grep keepalived
兩臺服務器的安裝步驟徹底同樣
六.配置文件的修改
1.nginx配置文件的修改,因爲咱們只是簡單的測試因此只要顯示的頁面能識別是哪臺服務器的便可
master服務器:
cd /usr/local/nginx/html
echo "nginx master" > index.html
slaved服務器:
cd /usr/local/nginx/html
echo "nginx slaved" > index.html
2.keepalived配置文件的修改:
master服務器;
cd /usr/local/etc/keepalived
vi keepalived.conf
內容以下:
! Configuration File for keepalived
global_defs {
notification_email {
jlsfwq@163.com
}
notification_email_from keepalived@staff.sina.com.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER ############ 輔機爲 BAUCK
interface eth0
virtual_router_id 51
mcast_src_ip 10.8.1.108
priority 102 ########### 權值要比 bauck 高
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.8.1.110 ############ 虛IP地址
}
}
slaved服務器:
cd /usr/local/etc/keepalived
vi keepalived.conf
內容以下:
! Configuration File for keepalived
global_defs {
notification_email {
jlsfwq@163.com
}
notification_email_from keepalived@staff.sina.com.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 10.8.1.109
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.8.1.110
}
}
3.nginx和keepalived的啓動(啓動順序先master後slaved)
nginx的啓動:
/usr/local/nginx/sbin/nginx -t 測試配置文件是否正確
/usr/local/nginx/sbin/nginx 啓動nginx
修改nginx.conf 中監聽端口80->8088:
Conf代碼
listen 8088;
server_name localhost;
查看nginx是否啓動命令:
lsof -i:8088
強制中止Nginx:
pkill -9 nginx
查詢nginx主進程號
ps -ef | grep nginx
keepalived的啓動:
108 /usr/local/sbin/keepalived -D -f /usr/local/etc/keepalived/keepalived.conf
109 cd /usr/local/keepalive/sbin/
./keepalived -D -f /usr/local/keepalive/etc/keepalived/keepalived.conf
日誌查看:tail -200f /var/log/messages
四. 測試
1.先測試一下master和slaved的nginx的服務是否正常
http://10.8.1.108
會顯示:nginx master
http://10.8.1.109
會顯示:nginx slaved
證實兩臺服務器的nginx服務都正常
2.測試虛擬IP的連通性和查看虛擬ip在master仍是在slaved上
測試虛擬IP的連通性:
在本地ping 10.8.1.109能ping通證實虛擬IP已經啓動
查看虛擬ip在的主機:
分別在master和slave上執行ip a命令,例如:
[root@glusterfs_client keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:a9:71:5e brd ff:ff:ff:ff:ff:ff
inet 192.168.4.188/24 brd 192.168.4.255 scope global eth0
inet 192.168.4.189/32 scope global eth0
inet6 fe80::20c:29ff:fea9:715e/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
哪臺主機能看到這行"inet 192.168.4.189/32 scope global eth0" 證實虛擬主機就在哪臺機器上
3. 測試虛擬IP的應用:
http://10.8.1.110
能正常顯示:nginx master
4.測試keepalived的切換功能
在本地一直ping 192.168.4.189,到192.168.4.188-master上切斷網絡,例如:service network stop
本地ping 192.168.4.189何時通證實切換成功,不通的話證實切換失敗
驗證切換到slaved上:
http://192.168.4.189 顯示:nginx slaved 表示OK!!!
能夠到192.168.4.190-slave上執行:ip a 產看虛擬IP:
好了到此咱們已經成功的實現了keepalived的HA功能,將192.168.4.188-master的網絡恢復虛擬IP又自動切換到master的上來
http://jlsfwq.blog.51cto.com/818700/172220
另外參考文獻地址:http://blog.csdn.net/chengfei112233/article/details/7649938
html