环境:ubuntu9.04、nginx-0.8.39、keepalived(在线安装)
目标:
ip1:192.168.0.116
ip2:192.168.0.117
vip:192.168.0.200
安装nginx
$tar -zxvf nginx-0.8.39
$cd nginx-0.8.39
$sudo ./configure
$make
$sudo make install
安装keepalived
$sudo apt-get install keepalived
//以下略过
配置nginx
$sudo vim nginx.conf //打开修改如
#user nobody;
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 65;
#gzip on;
upstream 192.168.0.200 { #vip
server 192.168.237.1:8088;
server 192.168.237.128:8088;
server 192.168.237.129:8088;
}
server {
listen 80;
server_name 192.168.0.200;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://192.168.0.200;
}
#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;
}
location /nginx/status {
stub_status on;
access_log on;
auth_basic "NginxStatus";
#auth_basic_user_file conf/htpasswd;
}
}
}
PS:双机上配置相同。
配置keepalived
$sudo vim /etc/keepalived/keepalived.conf //内容如下
MASTER机
vrrp_script chk_nginx {
script "/tmp/check_http.sh"
interval 5
weight 2
}
global_defs {
notification_email {
}
notification_email_from
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_Nginx
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
mcast_src_ip 192.168.0.116
priority 100
advert_int 3
smtp_alert
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.0.200
}
}
BACKUP机
vrrp_script chk_nginx {
script "/tmp/check_http.sh"
interval 5
weight 2
}
global_defs {
notification_email {
}
notification_email_from
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_Nginx
}
vrrp_instance VI_1 {
state BACKUP
interface eth1 #实际情况而定
virtual_router_id 51
mcast_src_ip 192.168.0.117
priority 50
advert_int 3
smtp_alert
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.0.200
}
}
CHECK_HTTP SHELL
$sudo vim /tmp/check_http.sh
#!/bin/bash
NGINX_PROCESS='ps -C nginx --no-header | ws -l'
if[$NGINX_PROCESS -eq 0]
then
/etc/init.d/nginx start
sleep 3
if[$NGINX_PROCESS -eq 0]
then
/etc/init.d/keepalived stop
fi
fi
PS:check_http.sh在双机上相同
最后,启动全部服务进行测试,如停止MASTER机上的nginx,让BACKUP机来接管,测试略过。