Skip to content

Latest commit

 

History

History
154 lines (115 loc) · 2.98 KB

5.71-使用keepalived搭建高可用集群.md

File metadata and controls

154 lines (115 loc) · 2.98 KB

概览

涉及理论知识点:VRRP协议 https://blog.csdn.net/zhyvodka/article/details/41800399

准备工作

三台机器恢复快照,抹掉LVS的实验痕迹

规划:

aming01 web1
aming02 Nginx负载均衡1
aming03 Nginx负载均衡2,同时也是web2

搭建Nginx负载均衡

在aming02和aming03上都要做

安装Nginx

配置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

安装配置keepalived

在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

编写监控Nginx的脚本

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

启动keepalived

检查防火墙, 要保证VRRP协议放行,SElinux要关闭

iptables -A INPUT -p vrrp -j ACCEPT
setenforce 0

启动

systemctl start keepalived

测试

测试点:

手动停止aming01上的nginx服务
手动停止aming01上的keepalived服务
设置防火墙禁止VRRP协议