Keepalived+Nginx+Tomcat 实现高可用Web集群
以下是一个基于Keepalived和Nginx实现高可用Web集群的简化示例:
- 安装Keepalived:
sudo apt-install keepalived
- 配置Keepalived(
/etc/keepalived/keepalived.conf
):
global_defs {
router_id LVS_DEVEL
}
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 {
2.168.1.100
}
}
- 安装Nginx:
sudo apt-get install nginx
- 配置Nginx(
/etc/nginx/sites-available/default
):
upstream webapp {
server 192.168.1.10:8080;
server 192.168.1.20:8080;
}
server {
listen 80;
location / {
proxy_pass http://webapp;
}
}
- 安装Tomcat:
sudo apt-get install tomcat9
- 配置Tomcat实例(在两台服务器上),确保
JAVA_OPTS
环境变量包含-Dcatalina.base
指向正确的Tomcat目录。 - 启动Keepalived和Nginx服务:
sudo systemctl start keepalived
sudo systemctl start nginx
以上是一个高可用Web集群的简化示例。这个配置假设两个Tomcat实例运行在不同的服务器上,并且它们的IP地址分别是192.168.1.10和192.168.1.20。Nginx负责负载均衡请求到两个Tomcat实例,而Keepalived确保虚拟IP(在这个例子中是192.168.1.100)始终指向主服务器,从而实现高可用性。如果主服务器失效,Keepalived会自动故障转移到备用服务器。
评论已关闭