雙主模式使用兩個VIP,前段有2臺服務器,互爲主從,兩臺服務器同時工做,不存在資源浪費狀況。同時在前端的DNS服務器對網站作多條A記錄,實現了Nginx的負載均衡,當一臺服務器故障時候,資源會轉移到另外一臺服務器,繼續提供服務,在大型的網站中多數都使用此種架構。在此使用主主模式配置Nginx+keepalived的高可用性。php
兩臺Nginx,兩臺Web,前端DNS由運營商提供。css
IP地址html
Nginx1:10.10.10.11前端
Nginx2:10.10.10.12nginx
VIP1:10.10.10.21c++
VIP2:10.10.10.22web
Real1:10.10.10.13vim
Real2:10.10.10.14後端
1,在2臺Web主機上部署環境,安裝Nginx+PHP+MySQL,參考我前面的文章bash
2,分別在二臺Nginx負載均衡器上安裝Nginx,配置
安裝GCC編譯器等工具:
yum install -y gcc gcc-c++ autoconf automake libtool make openssl openssl-devel
安裝Nginx:
wget http://exim.mirror.fr/pcre/pcre-8.38.tar.gz
tar -zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure
make && make install
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install
wget http://nginx.org/download/nginx-1.6.3.tar.gz
tar -zxvf nginx-1.6.3.tar.gz
cd nginx-1.6.3/
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/logs/nginx.pid --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module
make && make install
Nginx.conf配置文件,二個nginx負載均衡器的文件同樣
user nobody;
worker_processes 1;
error_log /usr/local/nginx/logs/error.log notice;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 51200;
}
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;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
server_tokens off;
keepalive_timeout 60;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
upstream backend
{
server 10.10.10.13;
server 10.10.10.14;
}
server {
listen 80;
server_name 10.10.10.21; #Nginx2改成10.10.10.22
location / {
root html;
index index.php index.html index.htm;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /nginx_status {
stub_status on;
auth_basic "NginxStatus";
auth_basic_user_file /usr/local/nginx/htpasswd;
#allow 127.0.0.1;
#deny all;
}
location ~* \.(ini|docx|txt|doc|pdf)$ {
#禁止訪問文檔性文件
root /usr/share/nginx/html;
deny all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|html|htm|css)$ {
root /home/image;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /home/image;
if ( !-e $request_filename) {
proxy_pass http://backend;
}
}
}
}
配置完成啓動服務
[root@hd1 sbin]# ./nginx
./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
cd /lib
[root@hd1 lib]# ls
[root@hd1 lib]# ls *pcre*
libpcre.so.0 libpcre.so.0.0.1
[root@hd1 lib]# ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1
[root@hd1 lib]#
[root@hd1 sbin]# ./nginx
在二臺Nginx上安裝及配置keepalived:
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
tar -zxvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.i686
Keepalived configuration
------------------------
Keepalived version : 1.2.15
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lssl -lcrypto -lcrypt
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : No
fwmark socket support : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
SNMP support : No
SHA1 support : No
Use Debug flags : No
make && make install
ln -s /usr/local/sbin/keepalived /sbin/
#這一步很重要,不執行ln -s會報錯「Starting keepalived: /bin/bash: keepalived: command not found」
service keepalived start
二臺Nginx上keepalived.conf配置以下,配置完成後分別service keepalived start啓動,檢測keepalived配置是否成功
Nginx1:
global_defs {
notification_email {
test@163.com
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_VIP1
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(檢測腳本執行的間隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(調用檢測腳本)
}
virtual_ipaddress {
10.10.10.21/24 broadcast 10.10.10.255 dev eth1 label eth1:1
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth1
virtual_router_id 52
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(調用檢測腳本)
}
virtual_ipaddress {
10.10.10.22/24 broadcast 10.10.10.255 dev eth1 label eth1:2
}
}
Nginx2:
global_defs {
notification_email {
test@163.com
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_VIP2
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(檢測腳本執行的間隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(調用檢測腳本)
}
virtual_ipaddress {
10.10.10.21/24 broadcast 10.10.10.255 dev eth1 label eth1:1
}
}
vrrp_instance VI_2 {
state MASTER
interface eth1
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(調用檢測腳本)
}
virtual_ipaddress {
10.10.10.22/24 broadcast 10.10.10.255 dev eth1 label eth1:2
}
}
如下是針對nginx狀態進行檢測的腳本
vim /usr/local/src/check_nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
腳本加上可執行權限
chmod +x /usr/local/keepalived/sbin/check_nginx.sh
服務開啓後網卡狀態
[root@hd2 keepalived-1.2.15]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:50:56:22:04:b1 brd ff:ff:ff:ff:ff:ff
inet 10.10.10.12/24 brd 10.10.10.255 scope global eth1
inet 10.10.10.22/24 brd 10.10.10.255 scope global secondary eth1:2
inet6 fe80::250:56ff:fe22:4b1/64 scope link
valid_lft forever preferred_lft forever
3: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
link/ether be:88:be:d6:81:a6 brd ff:ff:ff:ff:ff:ff
[root@hd2 keepalived-1.2.15]#
====================================測試============================
測試web主節點服務down掉以後,備用節點服務是否能正常運行,kill -9 xxxxx,web仍然可以訪問
模擬keepalived節點出現故障,nginx服務器是否能自動轉移
[root@hd1 sbin]# service keepalived stop
Stopping keepalived: [ OK ]
結果訪問vip 21沒法訪問,可是22仍然可以正常提供服務