架构设计
ip | 服务应用 |
---|---|
192.168.166.128 | nginx/tomcat/keepalived |
192.168.166.129 | nginx/tomcat/keepalived |
192.168.166.130 | 虚拟ip |
我这是测试环境,防火墙全部关闭了。
# 查看防火墙状态 systemctl status firewalld # 关闭防火墙 systemctl stop firewalld
安装tomcat服务
192.168.166.128
在tomcat服务webapps下创建一个目录:zkc
当前目录:
mdkir -p webapps/zkcecho "192.168.166.128" >> webapps/zkc/a.html#启动tomcat./bin/startup.sh
访问地址:http://192.168.166.128:8080/zkc/a.html
192.168.166.129
在tomcat服务webapps下创建一个目录:zkc
当前目录:
mdkir -p webapps/zkcecho "192.168.166.129" >> webapps/zkc/a.html#启动tomcat./bin/startup.sh
访问地址:http://192.168.166.129:8080/zkc/a.html
安装nginx服务
可以直接到官网下载或者通过yum下载
两台机器安装步骤一样
# 安装nginx yum -y install nginx # 进入配置文件目录 cd /etc/nginx/ # 修改配置文件 vim nginx.conf upstream app_server{ server 192.168.166.128:8080 weight=1 max_fails=1 fail_timeout=100; server 192.168.166.129:8080 weight=1 max_fails=1 fail_timeout=100; } server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html; # load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location /zkc { proxy_set_header host $host:$server_port; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_connect_timeout 10; proxy_read_timeout 600; proxy_send_timeout 600; proxy_pass http://app_server; access_log /var/log/nginx/access-app.log main; } # 启动 nginx
访问: curl http://192.168.166.128/zkc/a.html
访问:curl http://192.168.166.129/zkc/a.html
安装keepalived
安装keepalived两台机器一样
可以上网下载keepalived或者通过yum安装
# 安装 yum -y install keepalived cd /etc/keepalived/ # 创建检测文件 vim chk_nginx.sh # 赋予可执行权限 chmod +x chk_nginx.sh
chk_nginx.sh 内容
#!/bin/bash status=$(ps aux|grep nginx | grep -v grep | grep -v bash | wc -l) #echo $status >> check.log if [ "${status}" = "0" ]; then #echo "nginx已关闭,正在重启" >> check.log # 启动nginx /usr/sbin/nginx status=$(ps -c nginx --no-header|wc -l) #echo $status >> check.log # 再次判断 if [ $status -eq 0 ];then # echo "nginx已关闭,正在重启" >> check.log # 关闭keepalived systemctl stop keepalived; fi fi
创建keepalived配置文件
# 备份原有keepalived.conf内容 mv keepalived.conf keepalived.conf_bk vim keepalived.conf
global_defs { router_id lvs_ha2 } vrrp_script chk_mysql { script "/etc/keepalived/chk_nginx.sh" interval 2 } vrrp_instance ha_1 { state backup interface ens33 virtual_router_id 80 priority 80 advert_int 2 nopreempt authentication { auth_type pass auth_pass 1111 } virtual_ipaddress { 192.168.166.130 } track_script { chk_mysql } }
启动keepalived
systemctl start keepalived
#查看虚拟ip ip add
访问虚拟ip环境:curl http://192.168.166.130/zkc/a.html
说明环境已经搭建成功
验证
1、192.168.166.128 中nginx突发故障,但是keepalived使得nginx又重新启动
2、重启服务器模拟服务器故障死机
192.168.166.128重启
192.168.166.129,此时发现虚拟ip漂移到此服务器上
为什么没有128呢,是因为刚才服务重启,但是tomcat、nginx、keepalived服务没有做开机自启。
到此这篇关于nginx+tomcat+keepalived实现高可用的文章就介绍到这了,更多相关nginx+tomcat+keepalived高可用内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!