一、拓扑结构
配置主机:
Nginx 1 Keepalived (Master):192.168.37.101
Nginx 2 keepalived (Backup):192.168.37.201
Tomcat1:192.168.37.133
Tomcat2: 192.168.37.134
Tomcat3: 192.168.37.135
Nfs: 192.168.37.136
VIp: 192.168.37.100
二、NFS配置
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
#安装nfs与rpc
yum -y install rpcbind nfs-utils
#创建三个共享文件夹
mkdir /web1
mkdir /web2
mkdir /web3
echo "<h1> this is Nginx1. </h1>" > /web1/index.html
echo "<h1> this is Nginx2. </h1>" > /web2/index.html
echo "<h1> this is Nginx3. </h1>" > /web3/index.html
#创建共享
vim /etc/exports
systemctl restart nfs
systemctl restart rpcbind
showmount -e
vim /etc/exports
/web1 192.168.37.133
/web2 192.168.37.134
/web3 192.168.37.135
三、Tomcat 1
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
tar zxvf nginx-1.12.2.tar.gz
useradd -M -s /sbin/nologin nginx #创建不带宿主,不可登录nginx用户
cd nginx-1.12.2
./configure
--prefix=/usr/local/nginx
--user=nginx
--group=nginx
make && make install #编译安装nginx
mount 192.168.37.136:/web1 /usr/local/nginx/html
df -Th
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin #命令链接,方便使用
nginx #开启
netstat -anpt | grep nginx #查看端口是否开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 57499/nginx: master
curl http://localhost
<h1> this is Nginx1. </h1>
四、Tomcat 2
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
tar zxvf nginx-1.12.2.tar.gz
useradd -M -s /sbin/nologin nginx #创建不带宿主,不可登录nginx用户
cd nginx-1.12.2
./configure
--prefix=/usr/local/nginx
--user=nginx
--group=nginx
make && make install #编译安装nginx
mount 192.168.37.136:/web2 /usr/local/nginx/html
df -Th
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin #命令链接,方便使用
nginx #开启
netstat -anpt | grep nginx #查看端口是否开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 57499/nginx: master
curl http://localhost
<h1> this is Nginx2. </h1>
五、Tomcat 3
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
tar zxvf nginx-1.12.2.tar.gz
useradd -M -s /sbin/nologin nginx #创建不带宿主,不可登录nginx用户
cd nginx-1.12.2
./configure
--prefix=/usr/local/nginx
--user=nginx
--group=nginx
make && make install #编译安装nginx
mount 192.168.37.136:/web2 /usr/local/nginx/html
df -Th
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin #命令链接,方便使用
nginx #开启
netstat -anpt | grep nginx #查看端口是否开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 57499/nginx: master
curl http://localhost
<h1> this is Nginx3. </h1>
六、负载均衡器配置
主从在这一部分的配置时一样的
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
tar zxvf nginx-1.12.2.tar.gz
#创建不带宿主,不可登录nginx用户
useradd -M -s /sbin/nologin nginx
cd nginx-1.12.2
./configure
--prefix=/usr/local/nginx
--user=nginx --group=nginx
#编译安装nginx
make && make install
ln -s /usr/local/nginx/conf/nginx.conf /etc/
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
vim /usr/local/nginx/conf/nginx.conf
…
#gzip on;
upstream nginx_server {
server 192.168.37.133:80 weight=1;
server 192.168.37.134:80 weight=1;
server 192.168.37.135:80 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
# index index.html index.htm;
proxy_pass http://nginx_server;
}
…
nginx -t #查看Nginx的配置文件位置
systemctl start nginx
netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6393/nginx: master
curl http://localhost
<h1> this is Nginx1. </h1>
curl http://localhost
<h1> this is Nginx2. </h1>
curl http://localhost
<h1> this is Nginx3. </h1>
curl http://localhost
<h1> this is Nginx1. </h1>
七、主keepalived配置(192.168.37.101)
yum install keepalived.x86_64 -y
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
vim keepalived.conf
删除所有
! Configuration File for keepalived
global_defs {
router_id master #本服务器的名称
}
vrrp_script chk_http_port { #vrrp脚本
script "/usr/local/src/nginx.sh" #脚本路径 最后手动执行此脚本,以确保脚本能
够正常运行
interval 2 #检测间隔时间 单位秒
weight 2
}
vrrp_instance vi_1 { #实例名称
state MASTER #指定keepalived的角色,MASTER为主,BACKUP为备
interface ens33 #当前进行vrrp通讯的网络接口卡
virtual_router_id 51 #虚拟路由编号,主从要一致
priority 110 #优先级,数值越大,获取地址优先级越大
adver_int 1 #检查间隔,默认为1s (vrrp组播周期秒数)
authentication { #认证方式
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #调用检测脚本
}
virtual_ipaddress {
192.168.37.100 #定义虚拟ip(VIP)
}
}
#将主调度器的配置文件,拷贝给调度器2
scp /usr/local/src/nginx.sh <root@192.168.37.201:/usr/local/src>
vim /usr/local/src/nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header | wc -l` 查询进程数
if [ $A -eq 0 ]
then /usr/local/nginx/sbin/nginx
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]
then exit 1
else exit 0
fi
else
exit 0
fi
#将脚本发给备服务器
scp /usr/local/src/nginx.sh root@192.168.37.201:/usr/local/src
chmod +x /usr/local/src/nginx.sh
systemctl start keepalived.service
systemctl status keepalived.service
八、备keepalived 配置
yum install keepalived.x86_64 -y
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
vim keepalived.conf
修改:
router_id backup 服务器名称
state BACKUP 指定keepalived的角色,MASTER为主,BACKUP为备
priority 100 优先级
chmod +x /usr/local/src/nginx.sh
systemctl start keepalived.service
systemctl status keepalived.service
九、浏览器访问
访问VIP:192.168.37.100
出现之前是在的网页,点击刷新,出现第二个网页,再刷新出现第三个,就是测试成功
模拟master挂掉
[root@master ~]# systemctl stop keepalived.service
#主服务器停止keepalived服务
然后再次测试访问