Nginx+keepalived负载均衡高可用
结构图
环境:
主 服务器:192.168.239.10
备 服务器:192.168.239.20
Web 服务器1:192.168.239.40
Web 服务器2:192.168.239.30
vip:192.168.239.100
客户端:192.168.239.200
配置两台节点web服务器(192.168.239.30、192.168.239.40 )
配置第一台节点服务器(192.168.239.30)
这里我用的tomcat作为web服务器当然也可以用nginx与apache作为web服务器
#首先将tomcat所需软件包拉入opt目录并关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
#安装jdk
cd /opt
rpm -ivh jdk-8u201-linux-x64.rpm
#设置jdk的环境变量
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
source /etc/profile.d/java.sh
java -version
#安装tomcat
tar zxvf apache-tomcat-8.5.16.tar.gz
mkdir /usr/local/tomcat #创建目录作为tomcat配置配置路径
mv apache-tomcat-8.5.16 /usr/local/tomcat/
#配置tomcat环境变量
vim /etc/profile.d/tomcat.sh
export CATALINA_HOME1=/usr/local/tomcat/tomcat
export CATALINA_BASE1=/usr/local/tomcat/tomcat
export TOMCAT_HOME1=/usr/local/tomcat/tomcat
source /etc/profile.d/tomcat.sh
#修改 tomcat1 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量
vim /usr/local/tomcat/apache-tomcat-8.5.16/bin/startup.sh
-----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE
export CATALINA_HOME=$CATALINA_HOME
export TOMCAT_HOME=$TOMCAT_HOME
vim /usr/local/tomcat/apache-tomcat-8.5.16/bin/shutdown.sh
-----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE
export CATALINA_HOME=$CATALINA_HOME
export TOMCAT_HOME=$TOMCAT_HOME
#开启服务到此tomcat就安装好了
/usr/local/tomcat/apache-tomcat-8.5.16/bin/startup.sh
#添加站点目录文件
mkdir /usr/local/tomcat/apache-tomcat-8.5.16/webapps/test
vim /usr/local/tomcat/apache-tomcat-8.5.16/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("this is dynamic web1");%>
</body>
</html>
#由于主机名 name 配置都为 localhost,需要删除前面的 HOST 配置
vim /usr/local/tomcat/apache-tomcat-8.5.16/conf/server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/apache-tomcat-8.5.16/webapps/test/" path="" reloadable="true" />
#开启服务
/usr/local/tomcat/apache-tomcat-8.5.16/bin/shutdown.sh
/usr/local/tomcat/apache-tomcat-8.5.16/bin/startup.sh
配置第二台节点服务器(192.168.239.40)
配置与第一台tomcat一致唯一不同站点目录网页文档用于区分实现负载均衡
vim /usr/local/tomcat/apache-tomcat-8.5.16/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title>
</head>
<body>
<% out.println("this is dynamic web2");%>
</body>
</html>
访问测试
配置负载调度器(主:192.168.239.10)
#关闭防火墙
systemctl stop firewalld.service
setenforce 0
#安装相关依赖包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
#创建运行用户
useradd -M -s /sbin/nologin nginx
#安装nginx
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/
cd nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --grou=nginx --with-http_stub_status_module --with-stream && make && make install
#优化路径
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#将nginx加入服务并给执行权限
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
#开启服务
systemctl start nginx.service
systemctl enable nginx.service
#配置四层代理
vim /usr/local/nginx/conf/nginx.conf
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
access_log logs/stream_access.log main;
upstream backend_server { #添加节点服务器地址
server 192.168.239.30:8080;
server 192.168.239.40:8080;
}
server {
listen 8080; #监听端口
proxy_pass backend_server;
}
}
systemctl restart nginx
#安装keepalived
yum install -y keepalived
cd /etc/keepalived
#修改配置文件
vim keepalived.conf
#定义全局参数
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 172.0.0.1 #修改,邮件服务指向本地
smtp_connect_timeout 30
router_id NGINX_01 #指定服务器(路由器)的名称,主备服务器名称须不同,主为LVS_01,备为LVS_02
}
vrrp_instance VI_1 { #定义VRRP热备实例参数
state MASTER #指定热备状态,主为MASTER,备为BACKU
interface ens33 #修改,指定承载vip地址的物理接口
virtual_router_id 51 #修改,指定虚拟路由器的ID号,每个热备组保持一致
priority 100 #指定优先级数值越大优先级越高
advert_int 1 #通告间隔秒数(心跳频率)
authentication { #定义认证信息,每个热备组保持一致
auth_type PASS #认证类型
auth_pass 1111 #修改,指定验证密码,主备服务器保持一致
}
virtual_ipaddress { #指定群集vip地址
192.168.239.100
}
}
#编写服务控制简本当nginx服务挂掉是自动关闭keepalved服务并给脚本权限
vim check_nginx.sh
#!/bin/bash
count=$(ps -ef | grep nginx | egrep -cv "grep"|$$)
if [ $count -eq 0 ];then
systemctl stop keepalived
fi
chmod +x check_nginx.sh
#修改配置文件
vim keepalived.conf
#检测脚本
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh" #心跳执行的脚本,检测nginx是否启动
track_script { #调用检测脚本
check_nginx
}
systemctl start keepalived.service
ip addr
配置负载调度器(备:192.168.239.10)
#关闭防火墙
systemctl stop firewalld.service
setenforce 0
#yum安装nginx
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
yum install nginx -y
#配置四层代理
cd /etc/nginx/
vim nginx.conf
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status #upstream_bytes_sent';
access_log logs/stream_access.log main;
upstream backend_server {
server 192.168.239.30:8080; #添加节点服务器
server 192.168.239.40:8080;
}
server {
listen 8080; #监听地址
proxy_pass backend_server;
}
}
#创建日志文件目录
mkdir logs
#开启服务
systemctl restart nginx
#安装keepalived服务
yum install -y keepalived
#修改配置文件与主差不多只要在其基础修改几处即可
cd /etc/keepalived
vim keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 172.0.0.1
smtp_connect_timeout 30
router_id NGINX_02 #修改此处指定服务器(路由器)的名称,主备服务器名称须不同,
}
vrrp_instance VI_1 {
state BACKUP #指定热备状态,主为MASTER,备为BACKU
interface ens33
virtual_router_id 51
priority 90 #修改此行指定优先级数值越大优先级越高
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.239.100
}
}
#编写服务控制简本当nginx服务挂掉是自动关闭keepalved服务并给脚本权限
vim check_nginx.sh
#!/bin/bash
count=$(ps -ef | grep nginx | egrep -cv "grep"|$$)
if [ $count -eq 0 ];then
systemctl stop keepalived
fi
chmod +x check_nginx.sh
vim keepalived.conf
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh" #心跳执行的脚本,检测nginx是否启动
track_script {
check_nginx #调用检测脚本
}
systemctl start keepalived.service
客户机测试
关闭主负载调度器的keepalived服务再次测试
此时备负载调度器则会将自己转为Master设备
再次访问任然可以访问
3)]
[外链图片转存中…(img-ouUioDNj-1656229719343)]
客户机测试
[外链图片转存中…(img-s9eG2P9Z-1656229719343)]
[外链图片转存中…(img-w6ox2RQF-1656229719344)]
[外链图片转存中…(img-gyofa70G-1656229719344)]
关闭主负载调度器的keepalived服务再次测试
[外链图片转存中…(img-TCQFnmHu-1656229719344)]
此时备负载调度器则会将自己转为Master设备
[外链图片转存中…(img-nGnVPzC6-1656229719344)]
再次访问任然可以访问
[外链图片转存中…(img-3wg6T6P2-1656229719344)]