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

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


将业务端口更改成7001端口,对外部浏览器访问使用
将程序关闭的端口更改成8006 端口,该端口对内部使用。

其他几个服务相应的去更改tomcat端口。
Nginx配置
本次反向代理使用的是软负载nginx进行代理,分别使用了两台虚拟进行主备切换,保证业务正常使用。
Nginx 两台服务器地址为:
192.168.56.55
192.168.56.56
Nginx 配置:
本次nginx 安装在/usr/local/nginx目录下面

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;
}
通过浏览器验证



至此反向代理配置成功。
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
}
}

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30345407/viewspace-2680427/ ,如需转载,请注明出处,否则将追究法律责任。