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

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



1、环境nginx

5台虚拟机,分别是:apache

1台测试机(192.168.2.83);vim

2台nginx/keepalived(192.168.2.235/192.168.2.236);tomcat

2台Web Servers(192.168.2.237/192.168.2.238);安全

注:VIP设置为 192.168.2.229;bash


2、安装配置Web Server服务器

因为Web Server的安装与配置很是简单,根据本身喜爱,安装一个便可;好比:apache、nginx、tomcat等等。在此就再也不详述;app


3、安装配置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就安装及配置好了。


4、安装配置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了;


5、校验及测试

  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

相关文章
相关标签/搜索