1、場景需求前端
2、Keepalived 簡要介紹
Keepalived 是一種高性能的服務器高可用或熱備解決方案,Keepalived 能夠用來防止服務器單點故障的發生,經過配合 Nginx 能夠實現 web 前端服務的高可用。
Keepalived 以 VRRP 協議爲實現基礎,用 VRRP 協議來實現高可用性(HA)。VRRP(VirtualRouter
Redundancy Protocol)協議是用於實現路由器冗餘的協議,VRRP 協議將兩臺或多臺路由器設備虛擬成一個設備,對外提供虛擬路由器 IP(一個或多個),而在路由器組內部,若是實際擁有這個對外 IP 的路由器若是工做正常的話就是 MASTER,或者是經過算法選舉產生,MASTER 實現針對虛擬路由器 IP 的各類網絡功能,
如 ARP 請求,ICMP,以及數據的轉發等;其餘設備不擁有該虛擬 IP,狀態是 BACKUP,除了接收 MASTER 的VRRP 狀態通告信息外,不執行對外的網絡功能。當主機失效時,BACKUP 將接管原先 MASTER 的網絡功能。VRRP 協議使用多播數據來傳輸 VRRP 數據,VRRP 數據使用特殊的虛擬源 MAC 地址發送數據而不是自身網卡的 MAC 地址,VRRP 運行時只有 MASTER 路由器定時發送 VRRP 通告信息,表示 MASTER 工做正常以及虛擬路由器 IP(組),BACKUP 只接收 VRRP 數據,不發送數據,若是必定時間內沒有接收到 MASTER 的通告信息,各 BACKUP 將宣告本身成爲 MASTER,發送通告信息,從新進行 MASTER 選舉狀態。linux
3、方案規劃nginx
4、安裝 Nginxc++
一、安裝編譯 Nginx 所需的依賴包web
Connecting to 192.168.1.121:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Sat Apr 16 17:55:20 2016 from 192.168.1.61
[root@edu-proxy--01 ~]# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel算法
二、上傳 Nginx(nginx-1.6.2.tar.gz)到 /usr/local/src 目錄shell
三、編譯安裝 Nginxbash
[root@edu-proxy--01 ~]# cd /usr/local/src/
[root@edu-proxy--01 src]# ls
keepalived-1.2.18.tar.gz nginx-1.6.2.tar.gz
[root@edu-proxy--01 src]# tar -zxvf nginx-1.6.2.tar.gz 服務器
[root@edu-proxy--01 src]# cd nginx-1.6.2
[root@edu-proxy--01 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx
[root@edu-proxy--01 nginx-1.6.2]# make && make install
[root@edu-proxy--01 nginx-1.6.2]# vi /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 88;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
修改 Nginx 歡迎首頁內容(用於後面測試,用於區分兩個節點的 Nginx):
vi /usr/local/nginx/html/index.html
192.168.1.121 中的標題加 1
<h1>Welcome to nginx! 1</h1>
192.168.1.122 中的標題加 2
<h1>Welcome to nginx! 2</h1>
五、系統防火牆打開對應的端口 88
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 88 -j ACCEPT
service iptables restart
六、測試 Nginx 是否安裝成功
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
七、啓動 Nginx
/usr/local/nginx/sbin/nginx
重啓
/usr/local/nginx/sbin/nginx -s reload
八、設置 Nginx 開機啓動
vi /etc/rc.local
加入
/usr/local/nginx/sbin/nginx
分別訪問兩個nginx
nginx1
2.nginx2
5、安裝 Keepalived ( http://www.keepalived.org/download.html )
一、上傳或下載 keepalived(keepalived-1.2.18.tar.gz)到 /usr/local/src 目錄
二、解壓安裝
cd /usr/local/src
tar -zxvf keepalived-1.2.18.tar.gz
cd keepalived-1.2.18
./configure --prefix=/usr/local/keepalived
make && make install
三、將 keepalived 安裝成 Linux 系統服務:
由於沒有使用 keepalived 的默認路徑安裝(默認是/usr/local),安裝完成以後,須要作一些工做
複製默認配置文件到默認路徑
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
複製 keepalived 服務腳本到默認的地址
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
設置 keepalived 服務開機啓動
chkconfig keepalived on
四、修改 Keepalived 配置文件
注意,若是linux用的網卡是eth0,interface就要寫成eth1,用的是eth1就要寫成eth1
(1) MASTER 節點配置文件(192.168.1.121)
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id edu-proxy-01
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.121
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.120
}
BACKUP節點配置 (192.168.1.122)
! Configuration File for keepalived
global_defs {
router_id edu-proxy-02
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.122
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.120
}
}
5.編寫Nginx狀態檢測腳本/etc/keepalived/nginx_check.sh(keepalived.conf中配置)
腳本要求:若是nginx中止運行,嘗試啓動,若是沒法啓動則殺死本機的keepalived進程,keepaied將虛擬ip綁定到BACKUP機器上,內容以下
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
保存後給腳本授鄧權限
chmod -x /etc/keepalived/nginx_check.sh
6.啓動keepalived
serivce keepalived start
咱們在cmd中查看arp
在edu-proxy-01
[root@edu-proxy--01 keepalived]# ip add
這裏咱們訪問192.168.1.120:88
7.Keepalived+nginx的高可用測試
(1)關閉192.168.1.121的Nginx,Keepalived會將它從新啓動
[root@edu-proxy--01 keepalived]# /usr/local/nginx/sbin/nginx -s stop
(2)關閉192.168.1.121中的Keepalived,VIP會切換到192.168.1.122中
[root@edu-proxy--01 keepalived]# service keepalived stop
root@edu-proxy-02 keepalived]# ip add
查看虛擬ip
說明此時VIP已經漂移到主機192.168.1.122上了
再能過VIP來訪問Nginx集羣,訪問到的也是192.168.1.122
(3)從新啓動192.168.1.121中的Keepalived,VIP又會切回到192.168.1.121中來
[root@edu-proxy--01 keepalived]# service keepalived start
查看虛擬機IP
ip add
Keepalived啓動後,網絡接口上又會建立出VIP192.168.1.120
Keepalived服務管理命令:
中止: service keepalived stop
啓動: service keepalived start
重啓: service keepalived restart
查看狀態: service keepalived status
其餘參考資料:
keepalived之vrrp_script總結:http://my.oschina.net/hncscwc/blog/158746
keepalived雙機熱備實現故障時發送郵件通知:http://www.2cto.com/os/201407/317795.html
基於keepalived實現VIP轉移,lvs,nginx的高可用;:http://www.tuicool.com/articles/eu26Vz