keepalived nginx tomcat搭建高性能web服务器集群

发布时间:2023-09-05 点击:125
建站服务器
使用keepalived nginx tomcat搭建高性能web服务器集群,前端用nginx做反向代理实现负载均衡,同时结合keepalived对nginx实现高可用,后端使用tomcat做动态jsp解析,实现了动静分离。
(本文永久地址:http://woymk.blog.51cto.com/10000269/1928716)
搭建环境:centos 6.9
准备四台服务器
vip: 192.168.75.130
master: 192.168.75.131 (安装nginx做反向代理实现负载匀衡,结合keepalived实现高可用)
backup: 192.168.75.132 (同上)
web1: 192.168.75.151 (安装nginx和tomcat实现动静分离)
web2: 192.168.75.152 (同上)
这里为节省服务器,nginx和tomcat装在了一台服务上,也可以分开配置。
配置步骤如下:
1. 安装nginx、tomcat软件
四台服务器分别安装好nginx
安装方法参见:lnmp环境搭建
web1,web2上分别安装好tomcat
安装方法参见:tomcat8.5的安装配置
web1, web2上分别启动nginx和tomcat
service nginx start
service tomcat start
2. 在master和backup上配置nginx
vi /usr/local/nginx/conf/nginx.conf
加入或修改以下内容:
upstreamweb_nginx {
server 192.168.75.151;
server 192.168.75.152;
}
upstreamweb_tomcat {
#ip_hash;
server 192.168.75.151:8080;
server 192.168.75.152:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://web_nginx/;
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
}
location ~ \\\\.jsp$ {
proxy_pass http://web_tomcat/;
}
}
分别在master和backup上启动nginx服务
service nginx start
3. 配置keepalived
在master和backup上分别安装keepalived
yum install -y keepalived
1) 配置master
vi /etc/keepalived/keepalived.conf
清空原文,加入如下内容:
! configuration file for keepalived
global_defs {
notification_email {
#acassen@firewall.loc
#failover@firewall.loc
#sysadmin@firewall.loc
}
#notification_email_from alexandre.cassen@firewall.loc
#smtp_server 192.168.200.1
#smtp_connect_timeout 30
router_id lvs_devel
}
vrrp_script chk_http_port {
script </dev/tcp/127.0.0.1/80
interval 1
weight -2
}
vrrp_instance vi_1 {
state master
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type pass
auth_pass 1111
}
virtual_ipaddress {
192.168.75.130
}
track_script {
chk_http_port
}
}
2) 配置backup
scp 192.168.75.131:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
vi /etc/keepalived/keepalived.conf
backup的配置文件只需要修改
state master -> state backup
priority 100 -> priority 99
最后,分别在master和backup上启动keepalived服务,先主后备
/etc/init.d/keepalived start
4. 测试:
在master上停止nginx服务
[root@master ~]# /etc/init.d/nginx stop
停止 nginx: [确定]
在backup上查看日志
[root@backup ~]# tail -f /var/log/messages
may 23 17:21:33 backup keepalived_vrrp[1818]: vrrp_instance(vi_1) forcing a new master election
may 23 17:21:33 backup keepalived_vrrp[1818]: vrrp_instance(vi_1) forcing a new master election
may 23 17:21:34 backup keepalived_vrrp[1818]: vrrp_instance(vi_1) transition to master state
may 23 17:21:35 backup keepalived_vrrp[1818]: vrrp_instance(vi_1) entering master state
may 23 17:21:35 backup keepalived_vrrp[1818]: vrrp_instance(vi_1) setting protocol vips.
may 23 17:21:35 backup keepalived_vrrp[1818]: vrrp_instance(vi_1) sending gratuitous arps on eth0 for 192.168.75.130
may 23 17:21:35 backup keepalived_healthcheckers[1817]: netlink reflector reports ip 192.168.75.130 added
may 23 17:21:40 backup keepalived_vrrp[1818]: vrrp_instance(vi_1) sending gratuitous arps on eth0 for 192.168.75.130
日志显示已经成功切换到备用节点上。
[root@backup ~]# curl 192.168.75.130 -i
http/1.1 200 ok
server: nginx/1.10.3
date: tue, 23 may 2017 09:34:21 gmt
content-type: text/html
content-length: 616
connection: keep-alive
last-modified: mon, 22 may 2017 10:28:07 gmt
etag: 5922bd37-268
accept-ranges: bytes
master停掉后,用curl访问web服务,一切正常。

继推出国内首款NVMe存储Mach后 宏杉将NVMe全闪从产品到解决方案系列化
企业云服务器怎么看创建日期的时间限制
ssl证书显示不安全
用的好好的突然就不解析了
阿里云ecs云服务器购买须知
虚拟主机到期需要续费吗
学习php jQuery Ajax实现点赞效果的方法
怎么购买钉钉云服务器