ubuntu下配置nginx+keepalived做HA

环境: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机来接管,测试略过。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值