一、環境
Centos 6.7 nginx1.6.3
web01ip:172.16.1.7(內網)
web02ip:172.16.1.8(內網)
web搭建三個網站:blog、bbs、www
主的lb01ip:10.0.0.5/24(外網)
備的lb02ip:10.0.0.6/24(外網)
vip:10.0.0.4/24
二、負載均衡的配置
主配置文件:nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
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 /app/logs/access.log main;
error_log /app/logs/error.log;
upstream blog_server_pools {
ip_hash;
server 172.16.1.7:80;
server 172.16.1.8:80;
}
upstream bbs_server_pools {
ip_hash;
server 172.16.1.7:80;
server 172.16.1.8:80;
}
upstream www_server_pools {
ip_hash;
server 172.16.1.7:80;
server 172.16.1.8:80;
}
include extra/lb_blog.conf;
include extra/lb_bbs.conf;
include extra/lb_www.conf;
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
proxy_pass http://blog_server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
~
分支配置
[root@lb01 extra]# cat lb_blog.conf
server {
listen 80;
server_name blog.etiantian.org;
location / {
proxy_pass http://blog_server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
其他兩個分配置同上
三、keepalive安裝配置
1.yum -y insatll keepalive
2.配置keepalived.conf
[root@lb01 keepalived]# cat keepalived.conf
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24
}
}
[root@lb02 keepalived]# cat keepalived.conf
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24
}
}
3.主備兩臺服務開啓
/application/nginx/sbin/nginx
/etc/init.d/keepalive start
四、測試
首先在兩臺負載均衡器上查看ip
[root@lb01 ~]# ip addr|grep 10.0.0.4
inet 10.0.0.4/24 scope global secondary eth0
[root@lb02 ~]# ip addr|grep 10.0.0.4
然後把主負載均衡關閉
[root@lb01 ~]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]
最後查看備份負載均衡上的IP
[root@lb02 ~]# ip addr|grep 10.0.0.4
inet 10.0.0.4/24 scope global secondary eth0
發現虛擬IP已經跳轉過來。
五、知識拓展
弄個監控腳本來監測nginx的服務,一旦主機的nginx停止掉,會馬上停止主機的keepalived,然後切換到備機。
vim check_nginx.sh
###############監測nginx服務切換備機#############
#############write by chunchun at 20151203#########
#!/bin/bash
#
#!/bin/bashi
nginx=`ps -C nginx --no-header | wc -l`
if [ $nginx -eq 0 ]
then
/application/nginx/sbin/nginx >> /dev/null
echo "nginx 已經正常重啓工作中"
sleep 3
else
echo "nginx 正常運行中..."
keepalived=`ps -C nginx --no-header | wc -l`
if [ $keepalived -eq 0 ]
then
/etc/init.d/keepalived stop >> /dev/null
echo "已經正常停止keepalived服務"
else
echo "keepalived 仍處於運行狀態..."
fi
fi
讓腳本每隔多久監測一次
15 * * * * /server/scripts/check_nginx.sh &> /dev/null
實現Nginx+Keepalived中Nginx進程的高可用
[root@lb01 scripts]# crontab -e
############nginx+keepalive監控nginx服務高可用##########
*/15 * * * * sh /server/scripts/check_nginx.sh >/dev/null 2>&1