Keepalived+Nginx+tomcat实现主备+负载
部署系统:
Red Hat Enterprise Linux Server release 7.0
软件版本:apache-tomcat-7.0.92.tar.gz
keepalived-2.0.11.tar.gz
nginx-1.15.7.tar.gz (openssl-1.1.1.tar.gz pcre-8.40.tar.gz zlib-1.2.11.tar.gz )
架构图
vip:192.168.56.80-----> A:192.168.56.129 nginx(8001) tomcat (8080)
B:192.168.56.130 nginx(8001) tomcat (8080)
开始部署
1、准备两台虚拟机,网络我选择的是nat,如下图. 这种网络有点麻烦后面需要做一下映射。
在vmware 编辑下虚拟网络编辑器,选择NAT设置
这样通过本机IP访问这此端口了.
服务器上网络准备如下:
[root@mrice_02 sbin]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=eno16777736
ONBOOT=yes
IPADDR0=192.168.56.130
PREFIX0=32
GATEWAY0=192.168.56.2
HWADDR=00:0c:29:b9:46:b5
PEERDNS=yes
2、服务器准备完毕后,开始安装软件.
tomcat 不用说了,解压后,编写一个默认页面.
nginx 安装 请参考https://www.cnblogs.com/mrice/p/9882781.html
nginx.conf 配置文件如下 ,nginx配置可简单也可复杂,可根据生产中进行配置.
#user nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} 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"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ; #gzip on; upstream mrice{
server 192.168.56.129: weight=;
server 192.168.56.130: weight=;
} server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} location /mrice {
proxy_pass http://mrice/;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }
验证负载是否正常
keepalived 安装
本次使用的是源码安装
tar -xzvf keepalived-2.0.11.tar.gz
cd keepalived-2.0.11
./configure --prefix=/usr/local/keepalived 安装目录根据需要修改
make && make install
cp keepalived-2.0.11/keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp keepalived-2.0.11/keepalived/etc/sysconfig/keepalived
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
配置keepalived.conf
! Configuration File for keepalived global_defs {
notification_email {
mrice_02@mrice.com
}
notification_email_from mrice@mrice.com
smtp_server 192.168.56.8
smtp_connect_timeout
router_id mrice_backup
}
vrrp_script chk_http_port {
script "/opt/nginx/check_nginx.sh"
interval
weight
} vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.56.80
}
}
其中配置文件中/opt/nginx/check_nginx.sh检查nginx进程
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq ];then
/usr/nginx/sbin/nginx #重启nginx
if [ `ps -C nginx --no-header |wc -l` -eq ];then #nginx重启失败
exit
else
exit
fi
else
exit
fi
通过VIP访问是否正常。
可查看VIP地址在哪台服务器上
[root@mrice_02 sbin]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:b9:46:b5 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.130/32 brd 192.168.56.130 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.56.80/32 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feb9:46b5/64 scope link
valid_lft forever preferred_lft forever
截止目前主备+负载配置完成,过程很简单,很多细节需要动手操作才会发现.
最新文章
- Android开发学习之路-LeakCanary使用
- ARC下OC对象和CF对象之间的桥接(bridge)
- char和byte的区别
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
- chrome进入控制台时自动进入断点模式的解决方法
- libev代码
- thinkphp 关联模型配置代码
- Json数据
- List对象排序的通用方法
- java float、double精度研究(转)
- LSJ_NHibernate第三章 IDAL,DAL,BLL
- 十四、C# 支持标准查询运算符的集合接口
- Linux下查看进程(程序)启动时的环境变量
- maven,本地仓库和私服nexus的配置,以及eclipse载入maven
- 总结JavaScript输出内容(document.write)
- Linux 命令--查看物理CPU个数、核数、逻辑CPU个数
- Springmvc的工作流程
- 多个Tomcat 配置多个JDK
- C#连接oracle数据库提示ORA-12154: TNS: 无法解析指定的连接标识符
- C#删除区域实现透明