还是忍不住感叹:学习知识的方法可能相同,但是每个人的领悟到的东西真的时千差万别。
网上的东西很多,但照搬只能是事倍功半。
首先,准备三台虚拟机
one:192.168.7.137###===作为客户端
two:192.168.7.136###===作为web-master
three:192.168.7.135###===///作为web-slave
虚拟ip:192.168.221.10
查看三台的操作系统都是centos7.4的
# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
我的这个系统里没有nginx的yum源,我没有用tar包,下的是rpm包
#wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
#yum install -y nginx ====nginx安装
#yum install -y keepalived ###====keepalived 安装。
将master与slave的nginx和keepalived以相同的方式安装好。
配置nginx
(master与slave的配置相同)
#vim /etc/nginx/conf.d /default.conf
将user_name改为www.test.com
user_name www.test.com
将root访问路径改为:/
root /root/test/;
还有一个地方的重点:配置文件user nginx ##===若你不想创建nginx用户要改成root
#echo [master/slave] > /root/test/index.html ##=====/master上echo master,slave上echo slave
#systemctl start nginx
反正确保:curl www.test.com和curl 192.168.8.136/135能够访问成功,分别显示master或者slave
配置master/slave keepalived
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {
notification_email { -------#指定发送通知邮件的邮箱
heyajing@vtstar.net -------#可以回车写多行
}
notification_email_from keepalived@localhost smtp_server 127.0.0.1 ====///#指定发送email的smtp服务器
smtp_connect_timeout 30 ====///#设置连接smtp server的超时时间
router_id 192.168.221.136/135 ====///slave上135,故障发生时,发邮件时显示在邮件主题中的信息
}
vrrp_script chk_nginx { ====///#检测nginx的服务
script "/etc/keepalived/nginx_check.sh" ===//#这里通过脚本监测
interval 2 =====//#脚本执行间隔,每2s检测一次
weight -5 ====///#检测失败(脚本返回非0)则优先级 -5
fall 2 ====//#检测连续2次失败才算确定是真的失败。
rise 1 ====//#检测1次成功就算成功。但不修改优先级
}
vrrp_instance VI_1 {
state MASTER =====///指定keepalived的角色
interface eth0 ===///实例绑定的网卡
virtual_router_id 51 ====///#虚拟路由标识,即同一vrrp_instance下,MASTER和BACKUP必须是一致。
mcast_src_ip 192.168.221.136/135 priority 100 =====///#定义优先级,数字越大,优先级越高
advert_int 1 ======///#设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
authentication {
auth_type PASS ====///#设置vrrp验证类型,主要有PASS和AH两种
auth_pass 1111 =====///#设置vrrp验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
}
track_script {
chk_nginx =====///#执行监控的服务
}
virtual_ipaddress {
192.168.221.10 =====///虚拟地址 如果有多个VIP,继续换行填写
}
}切到/etc/keepalived/的目录下 master与slave都操作
[root@www keepalived]# vim nginx_check.sh
#!/bin/bash
while :
do
N=`ps -C nginx --no-header | wc -l`
K=`ps -C keepalived --no-header | wc -l`
if [ $A -eq 0 ];then
if [ $K -ne o ];then
killall -TERM keepalived
echo "nginx is stop so keepalived to stop"
else
echo "nginx is stop and keepalived is stop"
fi
else
if [ $K -eq 0 ];then
/etc/init.d/keepalived start
echo "nginx is start so to start keepalived"
else
echo "nginx and keepalived is running"
fi
sleep 10
done
启动keepalived
#systemctl start keepalived
当nginx和keepalived都启动后
#ps -ef | grep nginx 启动成功了吗
#ps -ef | grep keepalived 启动成功否?
都启动成功后
[root@www keepalived]# ip a
..........
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:81:2b:99 brd ff:ff:ff:ff:ff:ff
inet 192.168.7.136/24 brd 192.168.221.255 ……………………
inet 192.168.221.10/32 scope global eth0 ====/绑定了eth0
valid_lft forever preferred_lft forever
……………………………………………………
inet 192.168.221.10/32 scope global eth0 ====/绑定了eth0
valid_lft forever preferred_lft forever
……………………………………………………
再验证
[root@www keepalived]# curl 192.168.221.10
master
客户端验证:在one那台主机上
#echo 192.168.221.10 www.test.com >> /etc/hosts
#curl www.test.com
master
停掉master的nginx和keepalived
#curl www.test.com
slave
参考:https://www.cnblogs.com/kevingrace/p/6138185.html
https://www.cnblogs.com/liulangzhizi/p/7050260.html