环境介绍:
server:10.10.36.126,10.10.36.128
主要软件:
keepalived-1.3.2(126为主,128为从),nginx-1.11.8(126和128),apache-tomcat-8.0.39(126和128),redis-3.2.6(128)。
1、安装redis
tar -zxvf redis-3.2.6.tar.gz
mkdir redis
cd redis-3.2.6
make prefix=/home/wangzi/soft/redis install
cd ..
mv redis-3.2.6 ./redis
cd redis
cp ./redis-3.2.6/redis.conf ./redis.conf
vi redis.conf
bind 10.10.36.128
daemonize yes
dir /home/wangzi/soft/redis
启动:
./bin/redis-server ./redis.conf
防火墙打开6379端口。
2、安装Tomcat
tar -zxvf apache-tomcat-8.0.39.tar.gz
cd apache-tomcat-8.0.39/
vi ./conf/server.xml将端口号改为8081
将8081端口在防火墙中打开
cd webapps/ROOT/
cp index.jsp test.jsp
将test.jsp的内容改为:
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
128
<%= request.getSession().getId() %>
启动Tomcat:
./bin/startup.sh
128上的tomcat安装方法与此类似,端口为8082。
访问http://10.10.36.126:8081/test.jsp和http://10.10.36.128:8082/test.jsp可以看到对应的页面。
3、安装Nginx
yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel #root
tar -zxvf nginx-1.11.8.tar.gz
cd nginx-1.11.8
./configure --prefix=/home/wangzi/soft/nginx
make && make install
cd ../nginx
vi ./conf/nginx.conf
listen 8000;
vi ./html/index.html
Welcome to nginx! 8128
防火墙打开8000端口。
检查nginx安装是否成功:
./sbin/nginx -t
启动nginx:
./sbin/nginx
重启nginx
./sbin/nginx -s reload
126机器安装方法与此类似,端口指定为8000.
访问http://10.10.36.126:8000/和http://10.10.36.128:8000/可以看到对应的index页面。
4、配置Nginx
cd nginx
vi ./conf/nginx.conf
http下加入:
upstream tomcat{
server 10.10.36.126:8081;
server 10.10.36.128:8082;
}
http中的server下加入:
location /test {
proxy_pass http://tomcat/; #必须,其余为可选
proxy_set_header Host $http_host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 100m;
}
重启nginx:
./sbin/nginx -s reload
此配置在126和128机器上是一样的,不用修改。
此时,访问http://10.10.36.126:8000/test/test.jsp或http://10.10.36.128:8000/test/test.jsp,
可以看到:页面随机出现126或128的页面,但session不一致。
5、加入session共享
将commons-logging-1.2.jar,commons-pool2-2.4.2.jar,jedis-2.9.0.jar,TomcatClusterEnabledRedisSessionManager-1.0.jar放入tomcat的lib目录中;
将RedisDataCache.properties放入Tomcat的conf目录中,内容:
redis.hosts=10.10.36.128:6379
在conf下的context.xml中加入:
重启Tomcat,再次访问http://10.10.36.126:8000/test/test.jsp或http://10.10.36.128:8000/test/test.jsp,
会发现页面随机出现126或128,但session是一样的。
6、安装keepalived
su #root
yum install ipvsadm
yum install keepalived
(1)主从模式
vi /etc/keepalived/keepalived.conf
内容:
126:
global_defs {
router_id keep-126
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 3
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
mcast_src_ip 10.10.36.126
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
10.10.36.130
}
}
128:
global_defs {
router_id keep-128
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 3
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
mcast_src_ip 10.10.36.128
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
10.10.36.130
}
}
编写 Nginx 状态检测脚本 /etc/keepalived/nginx_check.sh,如果 nginx 停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程,keepalied将虚拟 ip 绑定到 BACKUP 机器上。内容如下:
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/home/wangzi/soft/nginx/sbin/nginx
sleep 5
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
启动服务:
systemctl start keepalived.service
查看状态:
systemctl status keepalived.service
使用ip add命令查看VIP信息:
126:
128:
此时访问http://10.10.36.130:8000/可以看到126上的nginx主页面。
若关闭126的keeplived,再次访问http://10.10.36.130:8000/可以看到128上的nginx主页面,可知keeplived已切换至128机器。
再次启动126的keeplived,页面又回到126。
访问http://10.10.36.130:8000/test/test.jsp将随机出现126或128的test页面(session一致,下同)。
此时关闭126或128上的任意一个keeplived,再次访问http://10.10.36.130:8000/test/test.jsp,还是随机出现126或128的test页面。
(2)双主模式
vi /etc/keepalived/keepalived.conf
内容:
126:
global_defs {
router_id keep-126
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 3
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
mcast_src_ip 10.10.36.126
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
10.10.36.130
}
}
vrrp_instance VI_2 {
state BACKUP
interface eno16777736
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.36.131
}
}
128:
global_defs {
router_id keep-128
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 3
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
mcast_src_ip 10.10.36.128
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.36.130
}
}
vrrp_instance VI_2 {
state MASTER
interface eno16777736
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
10.10.36.131
}
}
126:
128:
此时关闭126或128上的任意一个keeplived,http://10.10.36.130:8000/和http://10.10.36.131:8000/均可正常访问。
所需jar包下载:点击打开链接