keepalived + nginx + tomcat+memcached+高可用主从热备+负载均衡
keepalived + nginx + tomcat来实现HA的负载均衡(HA=高可用)
注:
此文未考虑session共享 ---> 负载均衡tomcat时利用memcached实现session共享 请参考另一篇博客
设备:
两台keepalived + nginx keepalived和nginx同一台服务器都要安装 IP为 192.168.159.31和192.168.159.34
两台 tomcat服务器 IP为 192.168.159.32和192.168.159.33
第一步:nginx实现负载均衡
1:配置nginx.conf (我主要贴出来重要的代码)
upstream you { # 这个是负载均衡哪几台服务器
server 192.168.159.32:8080;
server 192.168.159.33:8080;
}
server {
......
location / {
root html;
index index.html index.htm;
proxy_pass http://you; # 引用定义的you
}
......
}
2:两台nginx都需要配置上述代码
3:把两台的tomcat都启动;可以启动一台nginx(192.168.159.31);
然后浏览器中输入192.168.159.31 即可在页面上看到访问了不同的tomcat(可以修改index.jsp来区分tomcat)
第二步:利用keepalived 来实现高可用
1:上述已经表明两台keepalived的IP为 192.168.159.31和192.168.159.34
2:此处规定 192.168.159.31为MASTER(主) 192.168.159.34为BACKUP(备) 虚拟IP为 192.168.159.200
3:配置/etc/keepalived/keepalived.conf (一般就是这个路径)
代码如下:
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from hello@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_nginx {
script "/home/check_nginx.sh" #在/home下创建check_nginx.sh
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER #在另一台上为BACKUP
interface eth0
virtual_router_id 51
priority 199 #在另一台上修改小点,最好相差50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.159.200/24 dev eth0 label eth0:1 #绑定虚拟IP(VIP) 此IP为浏览器访问的IP
}
track_script {
check_nginx
}
}
4:vi /home/check_nginx
#!/bin/bash
#
URL="http://192.168.159.31" #此处对应nginx的IP地址
code=`curl -I -m 5 -o /dev/null -s -w %{http_code} $URL` #检测nginx是否还活着
if [ $code -ne 200 ];then
sleep 2
code=`curl -I -m 5 -o /dev/null -s -w %{http_code} $URL`
if [ $code -ne 200 ];then
`service keepalived stop`
fi
fi
5:上述 3和4部 两台keepalived+nginx上都要执行 并做相应的修改
第三步:
1:启动tomcat,nginx,keepalived
2:查看主keepalived上 ifcofig 可以看到虚拟IP;备keepalived上 并没有虚拟IP
3:页面输入 192.168.159.200(VIP) 即可访问
4:把主keepalived(192.168.159.31)上 停止nginx 此时在查看ifconfig 发现没有虚拟IP了
在备keepaived上 可以看到有虚拟IP
5:页面输入 192.168.159.200(VIP) 还是可以正常的访问
结束:
这样就完成了高可用,IP漂移
推荐阅读
-
Nginx配置upstream实现负载均衡及keepalived实现nginx高可用
-
JAVAEE——宜立方商城03:Nginx负载均衡高可用、Keepalived+Nginx实现主备
-
LVS+Nginx负载均衡、LVS高可用环境搭建、安装Keepalived
-
Keepalived+Nginx实现负载均衡高可用
-
KeepAlived+Redis+Haproxy实现主从热备、负载均衡
-
Keepalived实现Nginx负载均衡高可用的示例代码
-
荐 搭建Keepalived+Lvs+Nginx高可用集群负载均衡
-
关于使用Keepalived实现Nginx的自动重启及双主热备高可用问题
-
keepalived+nginx双机热备+负载均衡
-
keepalived + nginx + tomcat+memcached+高可用主从热备+负载均衡