涉及理论知识点:VRRP协议 https://blog.csdn.net/zhyvodka/article/details/41800399
三台机器恢复快照,抹掉LVS的实验痕迹
aming01 web1
aming02 Nginx负载均衡1
aming03 Nginx负载均衡2,同时也是web2
在aming02和aming03上都要做
配置yum源
vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
安装
yum install -y nginx
vim /etc/nginx/conf.d/bbs_proxy.conf
upstream bbs
{
ip_hash;
server 192.168.222.128:80;
server 192.168.222.130:8080;
}
server
{
listen 80;
server_name bbs.aminglinux.cc;
location /
{
proxy_pass http://bbs;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
补充:关于upstream配置 https://blog.csdn.net/zwhfyy/article/details/70856035
在aming02和aming03上都要做
yum install -y keepalived
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected] //定义接收邮件人
}
notification_email_from //定义发邮件地址(实际没有)
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 001
}
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh" //此脚本为监控nginx服务的
interval 3
}
vrrp_instance aminglinux {
state MASTER
interface ens33 //网卡
virtual_router_id 001
priority 100 //权重100,此数值要大于backup
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux //定义密码
}
virtual_ipaddress {
192.168.222.100 //定义VIP
}
track_script {
chk_nginx //定义监控脚本,这里和上面vrr_script后面的字符串保持一致
}
}
补充: keepalived中如何配置第三方邮件告警 https://blog.csdn.net/HzSunshine/article/details/62052398
vim /usr/local/sbin/check_ng.sh
#!/bin/bash#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq 0 ]
then
systemctl start nginx #启动命令
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
修改权限
chmod 755 /usr/local/sbin/check_ng.sh
检查防火墙, 要保证VRRP协议放行,SElinux要关闭
iptables -A INPUT -p vrrp -j ACCEPT
setenforce 0
启动
systemctl start keepalived
测试点:
手动停止aming01上的nginx服务
手动停止aming01上的keepalived服务
设置防火墙禁止VRRP协议