温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何实现keeplied +nginx +tomcat 高可用部署

发布时间:2021-11-09 10:11:09 来源:亿速云 阅读:168 作者:柒染 栏目:建站服务器

这篇文章将为大家详细讲解有关如何实现keeplied +nginx +tomcat 高可用部署,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

 如何实现keeplied +nginx +tomcat 高可用部署

Tomcat 部署

本次实验是在两台虚拟机上部署tomcat中间件,每台虚拟机部署三个服务,分别使用7001、7002、7003三个端口,进行部署程序。

tomcat虚拟机地址:

192.168.56.57

192.168.56.58

 如何实现keeplied +nginx +tomcat 高可用部署

Tomcat7001配置:

进入到tomcat配置文件,进行修改端口参数

 如何实现keeplied +nginx +tomcat 高可用部署

 如何实现keeplied +nginx +tomcat 高可用部署

将业务端口更改成7001端口,对外部浏览器访问使用

如何实现keeplied +nginx +tomcat 高可用部署 

将程序关闭的端口更改成8006 端口,该端口对内部使用。

 如何实现keeplied +nginx +tomcat 高可用部署

其他几个服务相应的去更改tomcat端口。

Nginx配置

 

本次反向代理使用的是软负载nginx进行代理,分别使用了两台虚拟进行主备切换,保证业务正常使用。

 

Nginx 两台服务器地址为:

192.168.56.55

192.168.56.56

 

Nginx 配置:

本次nginx 安装在/usr/local/nginx目录下面

 如何实现keeplied +nginx +tomcat 高可用部署

Nginx配置文件更改:

[root@edsir1p8 conf]# cat nginx.conf

worker_processes  4;

 

error_log  logs/error.log;

 

pid       logs/nginx.pid;

 

 

events {

    use epoll;

    worker_connections  65000;

}

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

 

 log_format json '{"@timestamp":"$time_iso8601",'

             '"@version":"1",'

             '"client":"$remote_addr",'

             '"url":"$uri",'

             '"status":"$status",'

             '"domian":"$host",'

             '"host":"$server_addr",'

             '"size":"$body_bytes_sent",'

             '"responsetime":"$request_time",'

             '"referer":"$http_referer",'

             '"ua":"$http_user_agent"'

          '}';

 

 

    sendfile        on;

include   weixin.vhost.conf;

}

 

 

[root@edsir1p8 conf]# cat weixin.vhost.conf

upstream weixin {

#     ip_hash;

       server 192.168.56.57:7001 weight=1;

       server 192.168.56.57:7002 weight=1;

       server 192.168.56.57:7003 weight=1;

       server 192.168.56.58:7001 weight=1;

       server 192.168.56.58:7002 weight=1;

       server 192.168.56.58:7003 weight=1;

}

server {

      listen 80;

      server_name localhost;

 

       location / {

#     include proxy_common.conf;

       proxy_pass http://weixin;

       }

      access_log logs/weixin_access.log;

}

 

通过浏览器验证

 如何实现keeplied +nginx +tomcat 高可用部署

如何实现keeplied +nginx +tomcat 高可用部署

如何实现keeplied +nginx +tomcat 高可用部署

至此反向代理配置成功。

 

 

Keepalived 配置

 

安装Keepalived略

      本次实验将keepalived 和nginx 安装在一台服务器上。

192.168.56.55

192.168.56.56

完成上述步骤之后keepalived已经可以实现虚拟IP转移了,但是实际应用当中我们需要的是自动进行虚拟IP的转移,所以我们还需要配置keepalived的脚本,使其能够在某一个nginx无法提供服务的时候自动将虚拟IP转移到备用服务器,以下脚本来自于上边提供的链接,原理是通过curl访问某一个链接,如果连续两次三秒没有响应则降低服务器的优先级,我们在/etc/keepalived目录下创建一个名为check_status.sh的文件,然后键入以下内容

#!/bin/bash

count=0

for (( k=0; k<2; k++ )) do check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" http://localhost/index.jsp -o /dev/null )

if [ "$check_code" != "200" ]; then

count=$(expr $count + 1)

sleep 3

continue

else

count=0

break

fi

done

if [ "$count" != "0" ]; then

exit 1

else

exit

0

Fi

chmod +x check_status.sh

后我们在keepalived.conf中配置脚本,配置内容如下:

vrrp_script check_status

{

script "/etc/keepalived/check_status.sh"

interval 5

weight -5

}

vrrp_instance VI_1

{ state MASTER  #备keepalived 填写BACKUP

interface eth0

 virtual_router_id 51

priority 100 #备keepalived 填写90

advert_int 1

authentication {

auth_type PASS

auth_pass 1111 }

virtual_ipaddress {

192.168.56.100

}

track_script {

check_status

}

}

如何实现keeplied +nginx +tomcat 高可用部署

如何实现keeplied +nginx +tomcat 高可用部署 

配置完成后重启keepavlied即可,此时如果关闭本机的nginx的话可以看到在5秒后虚拟IP会漂移到备用服务器上去,这里因为演示的话图片太多了,就不上图片了,nginx退出可以执行nginx -s stop命令,如果是使用yum安装的nginx可以通过systemctl来停止nginx服务

 

模拟主nginx宕机

[root@edsir1p8 conf]# killall nginx

 

继续访问虚拟地址:

如何实现keeplied +nginx +tomcat 高可用部署

关于如何实现keeplied +nginx +tomcat 高可用部署就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI