实验准备
环境准备
lvs1:192.168.1.10
lvs2:192.168.1.11
tomcat1:192.168.1.12
tomcat2:192.168.1.13
DNS:192.168.1.14
NFS:192.168.1.15
虚拟服务器VIP1:192.168.1.20
虚拟服务器VIP2:192.168.1.30
实验流程:先解决keepalived双主模式并搭建nginx,建立nginx监控脚本,然后配置DNS,最后配置NFS
实验流程
搭建双主模式
下载keepalived和ipvsadm命令工具
yum -y install keepalived
yum -y install ipvsadm
两台lvs服务器同时搭建nginx,检测脚本
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS1
}
vrrp_script check_ngx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.20
}
track_script {
check_ngx
}
}
vrrp_instance VI_2 {
state SLAVE
interface ens33
virtual_router_id 52
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.1.30
}
track_script {
check_nginx
}
}
配置详解:
脚本定义模块
这个模块的意思是定义一个函数check_nginx,如果指定的脚本错误,就执行下面的参数
vrrp_script check_ngx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
fall 2
rise 1
}
参数:
script "/etc/keepalived/nginx_check.sh" :指定脚本所在的路径
interval :指定脚本执行的间隔。单位是秒。默认为1s。
weight :调整优先级。
rise 执行成功多少次才认为是成功
fall :执行失败多少次才认为失败。
脚本调用模块
配合vrrp_script使用,表示调用check_nginx函数
track_script {
check_nginx
}
双主模式:
定义两个标签VI_1和VI_2,虚拟IP192.168.1.20的主lvs为lvs1,虚拟IP192.168.1.30的主lvs为lvs2,并配置master和slave(backup)参数和权重
添加nginx检测脚本,放在/etc/keepalived目录下
脚本内容及详解vim /etc/keepalived/nginx_check.sh
#!/bin/bash
NGINX_PID=/usr/local/nginx/logs/nginx.pid
NGINX_CMD=/usr/local/nginx/sbin/nginx
if [ ! -f $NGINX_PID ];then
$NGINX_CMD
sleep 2
if [ ! -f $NGINX_PID ];then
/usr/bin/systemctl stop keepalived
fi
fi
查看一下nginx的pid是否存在,如果不存在,睡眠2秒,再次检测,如果还不存在,关闭keepalived服务,睡眠的作用是防止网络波动导致的检测不到nginx使keepalived立即关闭,给keepalived留2秒机会
开启keepalived(注意:如果修改配置文件,且keepalived开启状态,一定要先stop服务,再start,不能restart)systemctl start keepalived
测试:
ip a
lvs2配置文件内容
与lvs1相同,配置完先修改配置文件即可
只需要修改router_id LVS2
VI_1中
state SLAVE
priority 99
VI_2中
state MASTER
priority 100
! Configuration File for keepalived
global_defs {
router_id LVS2
}
vrrp_script check_nginx {
script "/etc/keepalived/nginx_check.sh"
weight -10
interval 5
fall 1
rise 1
}
vrrp_instance VI_1 {
state SLAVE
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.20
}
track_script {
check_nginx
}
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.1.30
}
track_script {
check_nginx
}
}
测试
lvs1,lvs2初始状态
lvs1
lvs2
关闭lvs1的keepalived,VIP1(192.168.1.20)飘到了lvs2上
重启lvs1的keepalived,VIP1飘回来
关闭lvs2的keepalived,VIP2(192.168.1.30)飘到了lvs1上
重启lvs2的keepalived,VIP2飘回来
两台lvs服务器同时搭建nginx,检测nginx脚本
lvs1服务器:
下载nginx
yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl-devel
useradd -M -s /sbin/nologin nginx
下载nginx1.6包
tar xf nginx-1.6.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.6.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_mp4_module --with-http_ssl_module && make && make install
ln /usr/local/nginx/sbin/nginx /usr/local/sbin
nginx
检测
修改nginx配置文件,upstream指向两台tomcat服务器进行负载均衡
vim /usr/local/nginx/conf/nginx.conf
重新加载nginx -s reload
lvs2和lvs1搭建流程一致
搭建tomcat服务器
tomcat1(192.168.1.12)
下载包 apache-tomcat-8.5.16.tar.gz(版本自选)
yum -y install java-1.8.0-openjdk.x86_64
tar xf apache-tomcat-8.5.16.tar.gz
mv apache-tomcat-8.5.16 /usr/local/tomcat85
cd /usr/local/tomcat85;ls
cd bin
为了区分,修改网站页面
vim /usr/local/tomcat85/webapps/ROOT/index.jsp
开启tomcat
./startup.sh
浏览器测试
tomcat2(192.168.1.13)
仿照搭建tomcat1
测试keepalived实现tomcat轮询
输入两个VIP(192.168.1.20 192.168.1.30)都可以进行轮询
关闭一个lvs服务器的keepalived服务,输入两个vip依然可以轮询实验完成,这里不做演示了
配置DNS服务器(192.168.1.14)
下载bind包
yum -y install bind
修改主配置文件(这里DNS CNAME解析设置成yun211.com,自己随意设置即可)
vim /etc/named.conf
检测主配置文件
named-checkconf /etc/named.conf
修改区域配置文件
cd /var/named/
cp -a /var/named/named.localhost yun211.localhost
vim yun211.localhost
启动named服务
systemctl start named
测试
浏览器输入www.yun211.com
搭建NFS(192.168.1.15)
nfs服务器端
yum -y install nfs-utils rpcbind
mkdir /opt/www
vim /etc/exports
/opt/www 192.168.1.0/24(ro,sync,no_root_squash)
echo "<h1>tomcat-nfs works</h1>" >/opt/www/index.jsp
开启rpcbind和nfs
systemctl start rpcbind nfs
检测:
exportfs
tomcat1
yum -y install nfs-utils
rm -rf /usr/local/tomcat85/webapps/ROOT/*
mount 192.168.1.15:/opt/www/ /usr/local/tomcat85/webapps/ROOT
systemctl start rpcbind nfs
df -Th
tomcat2 仿照tomcat1即可,配置过程相同
测试
tip:如果重启后记得开keepalived、nginx、tomcat、named和nfs,rpcbind服务
发布者:LJH,转发请注明出处:https://www.ljh.cool/5252.html