keepalived+nginx+tomcat+redis集群环境部署

1.所需软件、jar包。配置文件下载:http://pan.baidu.com/s/1dFgntst

2.环境说明:

centos6.5  64位

主节点:192.168.40.121

副节点:192.168.40.122

公用的虚拟IP(VIP):192.168.40.123

安装keepalived和nginx前要先安装:

yum -y install gcc pcre-devel zlib-devel openssl-devel 这几个软件

3.keepalived安装部署:

参考帖子:

--> http://blog.csdn.net/xyang81/article/details/52554398

--> http://www.cnblogs.com/kevingrace/p/6138185.html

keepalived主要是用来做负载均衡和高可用的,这里是用来做高可用的,负载均衡用nginx

1)解压安装到/usr/local/src/后,进到keepalived目录下

./configure

make && make install

cp /usr/local/src/keepalived-1.3.2/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/sbin/keepalived /usr/sbin/

2)设置keepalived开机启动

echo "/etc/init.d/keepalived start" >> /etc/rc.local

3)设置keepalived的虚拟主机(vip)

配置 /etc/keepalived/keepalived.conf

主节点(192.168.40.121):

! Configuration File for keepalived
vrrp_script chk_http_port {// 检测nginx服务是否在运行。有很多方式,比如进程,用脚本检测等等,这是使用脚本
    script "/opt/chk_nginx.sh"
    interval 2 间隔多少时间测试nginx
    weight -5 
    fall 2 //连续2次失败才算失败
    rise 1 //1次成功就算成功
}
vrrp_instance VI_1 {//vrrp实例定义块
    state MASTER //主节点
    interface eth0 //网卡
    mcast_src_ip 192.168.40.121 //发送多播数据包时的源IP地址,这里注意了,这里实际上就是在哪个地址上发送VRRP通告,这个非常重要,一定要选择稳定的网卡端口来发送,
                    //这里相当于heartbeat的心跳端口,如果没有设置那么就用默认的绑定的网卡的IP,也就是interface指定的IP地址 virtual_router_id 51 //虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。即同一vrrp_instance下,MASTER和BACKUP必须是一致的 priority 100 //最大的为主节点 advert_int 1 //设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒 authentication { //设置验证类型和密码。主从必须一样 auth_type PASS //设置vrrp验证类型,主要有PASS和AH两种 auth_pass 1111 //设置vrrp验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信 } virtual_ipaddress { //虚拟主机设置 192.168.40.123 } track_script { //执行监控的服务。注意这个设置不能紧挨着写在vrrp_script配置块的后面(实验中碰过的坑),否则nginx监控失效!! chk_http_port } }

副节点(192.168.40.122):

! Configuration File for keepalived
vrrp_script chk_http_port {// 检测nginx服务是否在运行。有很多方式,比如进程,用脚本检测等等,这是使用脚本
    script "/opt/chk_nginx.sh"
    interval 2 间隔多少时间测试nginx
    weight -5 
    fall 2 //连续2次失败才算失败
    rise 1 //1次成功就算成功
}
vrrp_instance VI_1 {//vrrp实例定义块
    state BACKUP //副节点
    interface eth0 //网卡
    mcast_src_ip 192.168.40.122 //发送多播数据包时的源IP地址,这里注意了,这里实际上就是在哪个地址上发送VRRP通告,这个非常重要,一定要选择稳定的网卡端口来发送,
                    //这里相当于heartbeat的心跳端口,如果没有设置那么就用默认的绑定的网卡的IP,也就是interface指定的IP地址
    virtual_router_id 51 //虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。即同一vrrp_instance下,MASTER和BACKUP必须是一致的
    priority 90 //最大的为主节点
    advert_int 1 //设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
    authentication { //设置验证类型和密码。主从必须一样
        auth_type PASS //设置vrrp验证类型,主要有PASS和AH两种
        auth_pass 1111 //设置vrrp验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
    }
    virtual_ipaddress { //虚拟主机设置
        192.168.40.123
    }
    track_script { //执行监控的服务。注意这个设置不能紧挨着写在vrrp_script配置块的后面(实验中碰过的坑),否则nginx监控失效!!
        chk_http_port
    }
}

4)编写监控脚本

vim /opt/chk_nginx.sh

#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /usr/local/nginx/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        /etc/init.d/keepalived stop
    fi
fi

chmod 755 /opt/chk_nginx.sh

sh /opt/chk_nginx.sh(或 /bin/sh /opt/chk_nginx.sh)

5)分别启动keepalived

 /etc/init.d/keepalived start(stop)

4.nginx安装部署

参考帖子:

--> http://www.cnblogs.com/hunttown/p/5759959.html

nginx是一个反向代理服务器,主要用来做负载均衡,静态服务器(静态分离,将静态文件存放在nginx服务器)

1)解压安装nginx后,进入nginx目录:

2)./configure --prefix=/usr/local/nginx 

3)make && make install

 4)设置nginx开机启动

echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

5)配置nginx.conf(主从节点配置一样)

#user  nobody;
worker_processes  1; //跟cpu核数一样

#error_log  logs/error.log; //错误日记

#pid        logs/nginx.pid; //进程pid文件
events { worker_connections 1024; //进程连接数 } //http服务器 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 0; keepalive_timeout 65;// 长连接请求时间 #gzip on; //负载均衡配置 upstream core { server 192.168.40.122:8081 weight=1; server 192.168.40.121:8080 weight=1; }

//负载均衡有4种配置方式
//1.轮询 主要是根据请求时间的前后进行请求分配
//2.比重 设置weight通过比重分配
//3.ip_hash 根据请求ip的hash值分配,同一个ip请求同一个服务器
//4.fair(第三方) 根据服务器请求时间进行分配
//虚拟主机配置 server { listen 8088; server_name 192.168.40.121; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://core; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_buffering off; #root html; #index index.html index.htm; } #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; } } }

   6) /usr/local/nginx/sbin/nginx  启动nginx(nginx -s reload/stop)

5.redis安装部署

1)下载redis需要的三个jar包,放到tomcat的lib包下面

2)修改tomcat的conf包下面的context.xml文件

<Context>  
  
    <!-- Default set of monitored resources -->  
    <WatchedResource>WEB-INF/web.xml</WatchedResource>  

  
  <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
  <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
   host="10.10.49.20"  // redis安装所在节点的ip地址
   port="6379"  
   database="0"  
   maxInactiveInterval="60" />  
</Context>  

3)修改redis.conf,将redis改为后台启动

daemonize yes

4)启动redis

/src/reids-server ./redis.conf

/src/redis-cli

5) 退出redis

/src/redis-cli shutdown

原文地址:https://www.cnblogs.com/3chi/p/6905278.html