Nginx+Keepalived实现负载均衡高可用

Nginx+Keepalived实现负载均衡高可用



一、环境

5台虚拟机,分别是:

1台测试机(192.168.2.83);

2台nginx/keepalived(192.168.2.235/192.168.2.236);

2台Web Servers(192.168.2.237/192.168.2.238);

注:VIP设置为 192.168.2.229;


二、安装配置Web Server

由于Web Server的安装与配置非常简单,根据自己喜好,安装一个即可;比如:apache、nginx、tomcat等等。在此就不再详述;


三、安装配置Nginx

yum -y install gcc vim lrzsz pcre-devel kernel-devel openssl-devel wget

wget http://nginx.org/download/nginx-1.9.0.tar.gz

tar xzvf nginx-1.9.0.tar.gz

cd nginx-1.9.0

./configure --prefix=/usr/local/nginx --with-debug --with-http_stub_status_module --with-stream --with-http_ssl_module

make;make install

ln -s /usr/local/nginx/sbin/nginx  /sbin/

cd /usr/local/nginx/conf

cp nginx.conf nginx.conf.bak

编辑nginx主配置文件nginx.conf,内容大致如下:

worker_processes  10;
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"';
    sendfile        on;
    keepalive_timeout  65;
    upstream html_pool {

          ip_hash;

        server 192.168.2.237:80;
        server 192.168.2.238:80;

          #server 192.168.2.239:80 backup;

          #server 192.168.2.240:80 down;

        }
    server {
        listen       80;
        server_name  localhost;
        location / {
         proxy_pass     http://html_pool;
         proxy_set_header   Host             $host;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}


注:以上的操作,两台都需要操作;到此,我们的Nginx就安装及配置好了。


四、安装配置Keepalived

wget  http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
tar xzvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.23.4.el6.x86_64/

#内核参数,根据不同的操作系统,会不一样,注意更改为您服务器上的内核路径即可;

make
make install
ln -s /usr/local/sbin/keepalived /sbin/
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak

编辑keepalive.conf主配置文件,内容大致如下:

! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
        script "/etc/keepalived/check_nginx.sh"
        interval 2
        weight 2
        }

vrrp_instance VI_1 {
    state MASTER/BACKUP        #前主后备
    interface eth0
    virtual_router_id 60
    priority 100/80        #前主后备
    advert_int 3
    authentication {
        auth_type PASS
        auth_pass 33333
    }
    virtual_ipaddress {
        192.168.2.229
    }
        track_script {
                chk_nginx
        }
}


因为主配置文件里有用到一个检测nginx状态的脚本,所以另外需要创建脚本,内容如下:

# cat check_ngix.sh

#!/bin/bash
nginx_start=`ps -C nginx --no-header |wc -l`
if [ $nginx_start = 0 ]; then
#/etc/init.d/nginx start
/usr/local/nginx/sbin/nginx
sleep 3
nginx_status=`ps -C nginx --no-header |wc -l`
if [ $nginx_status = 0 ]; then
/etc/init.d/keepalived stop
fi
fi


chmod +x /etc/keepalived/check_nginx.sh

注:同样,以上的所有操作,需要两台服务器上一样操作,注意更改主备及优先级即可;到此,keepalive的相关安装与配置即OK了;


五、校验及测试

  1. 启动keepalived(两台):/etc/init.d/keepalived start

  2. 在主服务器上随意把nginx或者keepalived服务关闭,观察服务的可用性;

  3. 如果飘到备服务器上了,同样,在备服务器上随意把nginx或者keepalived服务关闭,再观察;

  4. 最后需要注意的是,如果服务器上有启用安全软件,比如ipatalbes,一定要让服务器间相互访问,不仅仅是ping通;如:iptables -I INPUT -s 192.168.2.0/24 -j ACCEPT



你可能感兴趣的:(nginx,keepalived,负载均衡,HA)