文章目录
a, 使用单个nginx反向代理两个tomcat
主机 | ip | 端口说明 |
---|---|---|
主机1 | 192.168.56.66 | 8080: tomcat8, 80:nginx反向代理 |
主机2 | 192.168.56.77 | 8080: tomcat8 |
a.1,配置单个nginx反向代理
[root@c6 haproxy]# cat /etc/nginx/conf.d/proxy.conf
upstream tomcats {
#ip_hash;
server 192.168.56.66:8080;
server 192.168.56.77:8080;
}
server {
listen 80;
server_name c6;
location / {
proxy_pass http://tomcats;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
a.2,测试单个nginx反向代理
[root@c7 ~]# cat /opt/tomcat8/webapps/ROOT/a.jsp
<body>
<h1>c7</h1>
<%
System.out.println(session.getCreationTime());
out.println("<br> SESSION ID:" + session.getId() + "<br>");
out.println("Session created time is :" + session.getCreationTime()
+ "<br>");
%>
</body>
b, 使用keepalived维护虚拟ip: 在主备节点漂移
b1, 两个节点配置keepalived
[root@c6 haproxy]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
[email protected]
}
notification_email_from [email protected]
smtp_server localhost
smtp_connect_timeout 30
router_id LVS_DEVEL_c6 ##主备不同
vrrp_macst_group4 224.0.67.67
}
##测试keepalive的backup状态: 存在这个文件则权重减30,角色变为backup
#vrrp_script chk_keep {
# script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
# interval 1
# weight -30
#}
vrrp_instance VI_1 {
state MASTER ##主备不同
interface eth1
virtual_router_id 51
priority 100 ##主备不同
advert_int 1
authentication {
auth_type PASS
auth_pass testkeepalive
}
virtual_ipaddress {
192.168.56.230 ##浮动ip
}
# #调用keepalived状态测试脚本
# track_script {
# chk_keep
# }
# #发送通知: master状态时,backup状态时..
# notify_master "/etc/keepalived/notify.sh master"
# notify_backup "/etc/keepalived/notify.sh backup"
# notify_fault "/etc/keepalived/notify.sh fault"
}
##############################################
[root@c7 haproxy]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
[email protected]
}
notification_email_from [email protected]
smtp_server localhost
smtp_connect_timeout 30
router_id LVS_DEVEL_c7 ##diff
}
##keepalived state change test script
#vrrp_script chk_keep {
# script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
# interval 1
# weight -30
#}
vrrp_instance VI_1 {
state BACKUP ##diff
interface eth1
virtual_router_id 51
priority 80 ##diff
advert_int 1
authentication {
auth_type PASS
auth_pass testkeepalive
}
virtual_ipaddress {
192.168.56.230 ##float ip
}
# #invoke script
# track_script {
# chk_keep
# }
# #notify state change
# notify_master "/etc/keepalived/notify.sh master"
# notify_backup "/etc/keepalived/notify.sh backup"
# notify_fault "/etc/keepalived/notify.sh fault"
}
b2, keepalived的notify状态通知脚本
每个keepalived主机都配置相同脚本: 当keepalived是master状态时,启动haproxy; 否则就停止haproxy
具体<linux如何发送126邮件>配置见https://blog.csdn.net/eyeofeagle/article/details/104295051#c_action_39
[root@c7 vagrant]# cat /etc/keepalived/notify.sh
#!/bin/bash
#keepalived-master-->启动haproxy
#keepalived-backup-->停止haproxy
role=$1
send_mail(){
subject="keepalived master changes"
context="$(date +'%F %T'): $(hostname) changes to $role "
echo $context |mailx -s $subject [email protected] root@localhost
}
ctl_haproxy(){
case $role in
master)
service haproxy start;;
backup|faul)
service haproxy stop;;
esac
}
send_mail
ctl_haproxy
b3, 测试keepalived
[root@c6 conf.d]# service keepalived start
Starting keepalived: [ OK ]
[root@c6 conf.d]# ip a |grep 230
inet 192.168.56.230/32 scope global eth1
[root@c7 conf.d]# systemctl start keepalived
[root@c7 conf.d]# ip a |grep 230
##ip转移
[root@c6 conf.d]# service keepalived stop
Stopping keepalived: [ OK ]
[root@c6 conf.d]# ip a |grep 230
[root@c6 conf.d]#
[root@c7 conf.d]# ip a |grep 230
inet 192.168.56.230/32 scope global eth1
c, 使用两个nginx反向代理两个tomcat
两台主机nginx配置一样,如上面配置
d, 使用两个haproxy反向代理两个tomcat
主机 | ip | 端口说明 |
---|---|---|
主机1 | 192.168.56.66 | 8080: tomcat8, 80:happroxy |
主机2 | 192.168.56.77 | 8080: tomcat8, 80:happroxy |
a.1, 配置haproxy反向代理
[root@c6 haproxy]#
....
frontend main *:80
#acl url_static path_beg -i /static /images /javascript /stylesheets
#acl url_static path_end -i .jpg .gif .png .css .js
#use_backend static if url_static
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static1 192.168.56.66:8080 check
server static2 192.168.56.77:8080 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 192.168.56.66:8080 check
server app2 192.168.56.77:8080 check
##############################################
[root@c7 haproxy]#
......
frontend main *:80
#acl url_static path_beg -i /static /images /javascript /stylesheets
#acl url_static path_end -i .jpg .gif .png .css .js
#use_backend static if url_static
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static1 192.168.56.66:8080 check
server static2 192.168.56.77:8080 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 192.168.56.66:8080 check
server app2 192.168.56.77:8080 check