nginx+keepalived+tomcat实现主从高可用负载均衡
设备:
1、准备四台虚拟机,两台tomcat,两台nginx
2、两台tomcat配置相同,测试页不同
两台Tomcat配置完全相同、只有测试页面不同
安装jdk和tomcat
[root@localhost ~]# rz
导入jdk以及tomcat源码包
[root@localhost ~]# tar xf apache-tomcat-8.5.16.tar.gz
[root@localhost ~]# tar xf jdk-8u191-linux-x64.tar.gz
[root@localhost ~]# mv jdk1.8.0_191 /usr/local/java #移动jdk包到/usr/local/下并取名java
[root@localhost ~]# mv apache-tomcat-8.5.16 /usr/local/tomcat #移动tomcat包到/usr/local/下并取名tomcat
[root@localhost ~]# vi /etc/profile
在最后添加
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin
[root@localhost ~]# source /etc/profile #生效文件
[root@localhost ~]# /usr/local/tomcat/bin/startup.sh #开启服务
Tomcat1测试页:
[root@localhost ~]# echo “111111” > /usr/local/tomcat/webapps/ROOT/index.jsp
Tomcat2测试页:
[root@localhost ~]# echo “222222” > /usr/local/tomcat/webapps/ROOT/index.jsp
[root@localhost ~]# /usr/local/tomcat/bin/startup.sh #启动服务
[root@localhost ~]# netstat -anpt | grep :8080 #查看8080端口
tcp6 0 0 :::8080 :::* LISTEN 9968/java
关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# netstat -anpt | grep :8080
两台nginx配置完全相同
安装keepalived
[root@localhost ~]# yum install keepalived -y
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# ls
keepalived.conf
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak #备份keeplived配置文件
[root@localhost~]# vim /etc/keepalived/keepalived.conf #修改主配置文件
! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_http_port { #检测nginx服务是否在运行。有很多方式,比如进程,用脚本检测等等 script "/root/nginx.sh" #这里通过脚本监测 interval 2 #每两秒检测一次 weight -5 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5 fall 2 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间) rise 1 #检测1次成功就算成功。但不修改优先级 } vrrp_instance VI_1 { state BACKUP #主keepalived为master,备keeplived为backup interface ens33 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.200.254 } track_script { #执行监控的服务。 chk_http_port #引用VRRP脚本,即在 vrrp_script 部分指定的名字。 } }
编写nginx心跳脚本
[root@localhost ~]# vim nginx.sh
#!/bin/bash counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /usr/local/nginx/sbin/nginx sleep 2 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then systemctl stop keepalived fi fi
[root@localhost~]# chmod +x /root/nginx.sh
[root@localhost~]# systemctl restart keeplived #重启keepalived服务
安装Nginx
[root@localhost ~]# rz
导入Nginx源码包
[root@localhost ~]# tar xf xf nginx-1.15.9.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.15.9
[root@localhost~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre
[root@localhost~]# ln -sf /usr/local/nginx/sbin/nginx /usr/local/sbin
[root@localhost ~]# ll /usr/local/sbin/nginx
lrwxrwxrwx. 1 root root 27 10月 21 10:50 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@localhost~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; events { use epoll; worker_connections 1024; } 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; keepalive_timeout 65; gzip on; upstream tomcat_server { server 192.168.200.112:8080 weight=1; #此处IP为tomcat机IP server 192.168.200.113:8080 weight=1; } server { listen 192.168.200.254:80; #监听服务机192.168.200.254的80端口 server_name localhost; charset utf-8; access_log logs/host.access.log main; location / { root html; index index.html index.htm index.jsp; proxy_pass http://tomcat_server; proxy_set_header Host $http_host; } } }
[root@localhost~]# nginx -t #检查语法
[root@localhost~]#nginx #启动服务
[root@localhost~]#killall -HUP nginx #启动服务
[root@localhost ~]# netstat -anpt | grep :80 #检测80端口
[root@localhost ~]# ip a
inet 192.168.200.122/24 brd 192.168.200.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.200.254/32 scope global ens33 #可以看到有多了一个192.168.200.254的网卡
测试:开启多个页面依旧是111和222分配一致