环境:
1、centos6.5
2、虚拟ip:192.168.1.140
3、192.168.1.146、192.168.1.148上分别部署Nginx、Keepalived、tomcat并进行配置
说明:
Nginx 是一个高性能的HTTP反向代理服务器
Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以利用其来避免服务的单点故障
Tomcat 是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器

Keepalived + Nginx + Tomcat 的高可用负载均衡架构
如下图:为典型的Tomcat服务多实例部署的架构图

Keepalived高可用_第1张图片

(1)用户通过域名请求到DNS,由DNS解析域名后返回对应的IP地址,该IP及为Keepalived映射服务器的虚拟IP
(2)通过该虚拟IP访问到对应的负载均衡器(Nginx),这里Nginx部署两个,然后通过Keepalived来保证NG的高可用,正常情况下由Keepalived-M将虚拟IP映射转发至Nginx-M,
如果Nginx-M出现故障,此时Keepalived会切换至Keepalived-S开始工作,从而保证了NG的单点故障问题。
(3)通过Nginx负载均衡器,将请求路由到对应的Tomcat服务

开始搭建Keepalived + Nginx + Tomcat 的高可用负载均衡架构

服务器准备两台:192.168.1.146、192.168.1.48

安装依赖包yum install -y gcc gcc-c++ ncurses-devel perl pcre pcre-devel openssl openssl-devel zlib make
安装nginx+tomcat

安装nginx
如果yum已安装pcre插件可以不用安装

[root@nginx ~]# useradd nginx -s /sbin/nologin
[root@nginx opt]# tar zxf pcre-8.34.tar.gz
[root@nginx opt]# cd pcre-8.34
[root@nginx pcre-8.34]# ./configure
[root@nginx pcre-8.34]# make && make install
[root@nginx opt]# tar zxf tengine-1.5.1.tar.gz
[root@nginx opt]# cd tengine-1.5.1
[root@nginx tengine-1.5.1]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@nginx tengine-1.5.1]# make && make install

(1)修改配置文件

[root@nginx ~]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@nginx ~]# vi /usr/local/nginx/conf/nginx.conf

添加以下配置

user nginx nginx;
worker_processes auto;
worker_rlimit_nofile 65535;
error_log /var/log/www/error.log;
#pid      logs/nginx.pid;
events {
    use epoll;
    worker_connections  51200;
}
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"';

    server_info  off;
    server_tag   off;
    server_name_in_redirect off;
    access_log  /var/log/www/access.log  main;
    client_max_body_size 20m;
    client_header_buffer_size 16k;
    large_client_header_buffers 4 16k;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    server_tokens on;
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_proxied   any;
    gzip_http_version 1.1;
    gzip_comp_level 3;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    upstream www {
       #ip_hash;
       server 192.168.1.146:8080;
       server 192.168.1.148:8080;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
      root   html;
      index  index.html index.htm;
      proxy_pass   http://www;
      proxy_redirect    off;
      proxy_set_header   Host $host;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      client_max_body_size   10m;
      client_body_buffer_size   128k;
      proxy_connect_timeout   90;
      proxy_send_timeout   90;
      proxy_read_timeout   90;
      proxy_buffer_size   4k;
      proxy_buffers   4 32k;
      proxy_busy_buffers_size   64k;
      proxy_temp_file_write_size  64k;
   }
  }
}

安装tomcat

安装jdk,然后解压tomcat到指定路径

[root@test1 date]# rpm -ivh jdk-7u79-linux-x64.rpm
[root@test1 date]# tar zxf apache-tomcat-7.0.85.tar.gz  
[root@test1 date]# mv apache-tomcat-7.0.85 /usr/local/tomcat

安装keepalived

[root@test1 date]#yum -y install keepalived
[root@test1 date]#mv /etc/keepalived/keepalived.conf  /etc/keepalived/keepalived.conf.bak
[root@test1 date]#vi /etc/keepalived/keepalived.conf

修改配置文件keepalived.conf

#全局配置
global_defs {
    router_id LVS_DEVEL
}
#执行脚本配置
vrrp_script check_nginx_status {
    script "/etc/keepalived/check_nginx_status.sh"
    interval 2
    weight 2
}
#keeplived实例配置
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 88
    priority 99 #优先级
    advert_int 1
    nopreempt

    authentication {
        auth_type PASS
        auth_pass Chefu88
    }

    track_script {
        check_nginx_status
    }
#虚拟ip
    virtual_ipaddress {
        192.168.1.140
    }

执行脚本配置check_nginx_status.sh
检测到nginx进程,wc -l统计后,如果为0,就说明ps查不到nginx进程,然后等待3秒,自动启动nginx,
如果重新启动后,已然找不到nginx进程才会关闭keep,然后当keep关闭后,vip就转移到另一台机器

#!/bin/bash
#keepalived check nginx status scripts

prog=nginx
prog_num=`ps -C $prog --no-header | wc -l`
if [ $prog_num -eq 0 ];then
   /usr/local/nginx/sbin/nginx
   sleep 3s
   prog_num=`ps -C $prog --no-header | wc -l`
   if [ $prog_num -eq 0 ];then
      killall keepalived
   fi
fi

查看keepalived切换的效果,如虚拟ip在192.168.1.146上,停止146的keeplived,然后再次查看虚拟ip是否切换到192.168.1.148上,访问虚拟ip:192.168.1.140,可以查看到访问的是主keepalived服务器的tomcat