Nginx+Keeplived+Tomcat搭建高可用/负载均衡的web服务器集群
一、集群规划服务器角色主机名IP地址/VIP软件Nginx MasterNK120.0.20.101/20.0.20.100Nginx/KeepalivedNginx BackupNK220.0.20.102/20.0.20.100Nginx/KeepalivedWeb ServerNK320.0.20.103TomcatWeb ServerNK420.0.20.104Tomcat #关闭selinux和firewall
#软件版本为:apache-tomcat-7.0.85.tar.gzjdk-8u151-linux-x64.tar.gz
二、安装配置Tomcat
1.安装JDK并配置环境变量
2.部署Tomcat
# tar zxf apache-tomcat-7.0.85.tar.gz
# mv apache-tomcat-7.0.85 /usr/local/tomcat 3.配置server.xml
# cat /usr/local/tomcat/conf/server.xml
#NK4为tomcat2
4.在web.xml里增加
# vim /usr/local/tomcat/conf/web.xml
......
......
index.html
index.htm
index.jsp
5.建立测试页面
# mkdir /usr/local/tomcat/webapps/test
# cat /usr/local/tomcat/webapps/test/index.jsp
Session serviced by tomcat
Session ID
-----NK3
Created on
6.复制web.xml到测试文件夹
# mkdir /usr/local/tomcat/webapps/test/WEB-INF
# cp /usr/local/tomcat/conf/web.xml /usr/local/tomcat/webapps/test/WEB-INF/ 7.启动tomcat
# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started. #以上操作在NK4上同步执行,除了配置文件server.xml稍有区别,其他都一样
8.浏览测试页面
http://s1.运维网.com/images/20180404/1522809462280699.png
http://s1.运维网.com/images/20180404/1522809503382413.png
三、配置Nginx
1.获取Nginx yum源并安装
# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# yum install -y nginx 2.修改配置文件(NK1和NK2相同配置)
# cat /etc/nginx/nginx.conf
usernginx;
worker_processes1;
error_log/var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections1024;
}
http {
include /etc/nginx/mime.types;
default_typeapplication/octet-stream;
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log/var/log/nginx/access.logmain;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout65;
server_tokens off;
gzipon;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_send_timeout 60;
proxy_read_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
upstream test{
server 20.0.20.103:8080 weight=10;
server 20.0.20.104:8080 weight=10;
}
server {
listen 80;
server_name20.0.20.101;
charset utf-8;
location / {
proxy_passhttp://test;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
include /etc/nginx/conf.d/*.conf;
} 3.启动nginx
# systemctl start nginx
# systemctl enable nginx 4.测试
http://s1.运维网.com/images/20180404/1522813918948980.png
http://s1.运维网.com/images/20180404/1522814000480458.png
四、配置keepalived
1.安装keepalived
# yum -y install keepalived 2.编辑nginx检测脚本
# cat /usr/local/keepalived/chknginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl restart nginx
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl stop keepalived
fi
fi# chmod +x /usr/local/keepalived/chknginx.sh 3.修改配置文件
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_nginx {
script "/usr/local/keepalived/chknginx.sh" #nginx检测脚本
interval 3
weight -2
}
vrrp_instance VI_1 {
state MASTER #NK2为BACKUP
interface ens192
virtual_router_id 151
priority 100 #NK2为99
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
track_script {
check_nginx #检测脚本名
}
virtual_ipaddress {
20.0.20.100
}
} 4.在网卡上添加一个IP地址
http://s1.运维网.com/images/20180404/1522816470836995.png
#NK2和NK1配置类似
5.启动keepalived
# systemctl start keepalived
# systemctl enable keepalived 6.查看IP是否绑定
# ip add |grep "inet 20"
inet 20.0.20.101/16 brd 20.0.255.255 scope global ens192
inet 20.0.20.100/32 scope global ens192
inet 20.0.20.100/16 brd 20.0.255.255 scope global secondary ens192
# ip add |grep "inet 20"
inet 20.0.20.102/16 brd 20.0.255.255 scope global ens192
inet 20.0.20.100/16 brd 20.0.255.255 scope global secondary ens192 7.使用虚拟IP浏览测试页面
http://s1.运维网.com/images/20180404/1522816907831305.png
http://s1.运维网.com/images/20180404/1522816925551167.png
四、故障测试
1.keepalived测试
http://s1.运维网.com/images/20180404/1522818030190117.png
终止掉master后切换到了backup
http://s1.运维网.com/images/20180404/1522817968492010.png
2.nginx测试
在NK1上终止nginx后,会通过脚本自动启动nginx
http://s1.运维网.com/images/20180404/1522818450587551.png
http://s1.运维网.com/images/20180404/1522818254531589.png
http://s1.运维网.com/images/20180404/1522818473617474.png
3.tomcat测试
终止NK3上的tomcat
http://s1.运维网.com/images/20180404/1522818751522747.png
http://s1.运维网.com/images/20180404/1522818767967902.png
页:
[1]