• keepalived配置模板


    环境

    作用 IP 角色
    lb01 10.0.0.5 Master
    lb02 10.0.0.6 Backup
    VIP 10.0.0.3(随时被抢占)

    安装keepalived

    [root@lb01 ~]# yum install -y keepalived
    [root@lb02 ~]# yum install -y keepalived
    
    [root@lb01 ~]# systemctl start keepalived.service
    [root@lb02 ~]# systemctl start keepalived.service
    
    [root@lb01 ~]# systemctl enable keepalived.service 
    [root@lb02 ~]# systemctl enable keepalived.service
    
    #keepalived不能使用restart管理(???)
    

    关闭iptables和selinux

    sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
    systemctl enable firewalld
    
    #实际上开着防火墙也可以,不过要开启某些端口(80 443)
    

    配置keepalived抢占式

    master

    [root@lb01 ~]# vim /etc/keepalived/keepalived.conf
    global_defs {
        router_id lb01
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 50
        priority 150
        advert_int 2
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3
        }
    }
    [root@lb01 ~]# systemctl restart keepalived.service 
    

    backup

    [root@lb02 ~]# vim /etc/keepalived/keepalived.conf
    global_defs {
        router_id lb02
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id 50
        priority 100
        advert_int 2
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3
        }
    }
    [root@lb01 ~]# systemctl restart keepalived.service 
    

    [root@lb01 ~]# ip a

    [root@lb02 ~]# ip a

    QQ截图20200607094616.png

    [root@lb01 ~]# systemctl stop keepalived.service

    [root@lb01 ~]# ip a

    [root@lb02 ~]# ip a

    QQ截图20200607094702.png

    配置keepalived非抢占式

    master

    [root@lb01 ~]# vim /etc/keepalived/keepalived.conf
    global_defs {
        router_id lb01
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id 50
        priority 150
        advert_int 1
        nopreempt
    
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3
        }
    }
    
    [root@lb01 ~]# systemctl restart keepalived.service
    

    backup

    [root@lb02 ~]# vim /etc/keepalived/keepalived.conf
    global_defs {
        router_id lb02
    }
    
    vrrp_instance VI_1 {
        state BACKUP        
        interface eth0
        virtual_router_id 50
        priority 100
        advert_int 1
        nopreempt
        
        authentication {    
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3
        }
    }    
    
    [root@lb02 ~]# systemctl restart keepalived.service
    

    tg0soV.md.png
    tg0rd0.md.png
    tg06iT.md.png

    通过windows的arp去验证,是否会切换MAC地址

    tgBCY8.md.png

    通过脚本绑定nginx和keepalived的状态

    [root@lb01 ~]# vim /tmp/check.sh
    #!/bin/bash
    nginx=$(ps -C nginx --no-header|wc -l)
    
    #1.判断Nginx是否存活,如果不存活则尝试启动Nginx
    if [ $nginx -eq 0 ];then
        systemctl start nginx
        sleep 3
        #2.等待3秒后再次获取一次Nginx状态
        nginx=$(ps -C nginx --no-header|wc -l) 
        #3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本  
        if [ $nginx -eq 0 ];then
            systemctl stop keepalived
            echo nginx停止服务
       fi
    fi
    #给脚本增加执行权限(一定要做)
    [root@lb01 ~]# chmod +x /tmp/check.sh
    

    非抢占式nginx和keepalived关联模板

    lb01和lb02配置完全相同

    lb01

    1.配置绑定脚本
    [root@lb01 ~]# vim /tmp/check.sh
    
    2.把绑定脚本加入到keepalived配置文件
    [root@lb01 ~]# vim /etc/keepalived/keepalived.conf
    global_defs {
        router_id lb01
    }
    
    #脚本执行内容不能超过5秒,否则会中断再次重新执行脚本
    vrrp_script check {
        script "/tmp/check.sh"
        interval 5
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id 50
        priority 150
        advert_int 1
        nopreempt
    
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3
        }
    
        #调用并运行脚本
        track_script {
        check
        }
    
    }
    
    [root@lb01 ~]# systemctl restart keepalived.service
    

    lb02

    1.配置绑定脚本
    [root@lb01 ~]# vim /tmp/check.sh
    
    2.把绑定脚本加入到keepalived配置文件
    [root@lb01 ~]# vim /etc/keepalived/keepalived.conf
    global_defs {
        router_id lb01
    }
    
    #脚本执行内容不能超过5秒,否则会中断再次重新执行脚本
    vrrp_script check {
        script "/tmp/check.sh"
        interval 5
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id 50
        priority 150
        advert_int 1
        nopreempt
    
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3
        }
    
        #调用并运行脚本
        track_script {
        check
        }
    
    }
    
    [root@lb02 ~]# systemctl restart keepalived.service
    

    抢占式nginx和keepalived关联模板

    lb01

    1.配置绑定脚本
    [root@lb01 ~]# vim /tmp/check.sh
    
    2.把绑定脚本加入到keepalived配置文件
    [root@lb01 ~]# vim /etc/keepalived/keepalived.conf
    global_defs {
        router_id lb01
    }
    
    #脚本执行内容不能超过5秒,否则会中断再次重新执行脚本
    vrrp_script check {
        script "/tmp/check.sh"
        interval 5
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 50
        priority 150
        advert_int 1
    
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3
        }
    
        #调用并运行脚本
        track_script {
        check
        }
    
    } 
    
    [root@lb01 ~]# systemctl restart keepalived.service
    

    lb02

    [root@lb01 ~]# vim /etc/keepalived/keepalived.conf
    global_defs {
        router_id lb01
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id 50
        priority 150
        advert_int 1
    
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3
        }
    }
    
    [root@lb02 ~]# systemctl restart keepalived.service
    

    t2Q8De.md.png
    t2QGHH.md.png
    t2QYEd.md.png

    高可用keepalived故障闹裂 解决脚本

    闹裂现象:同一路由id的不同节点的服务器在某种状态下无法检测到不同节点的别的服务器的状态,而发生vip的抢占的现象。无论是抢占式还是非抢占式都可能发生闹裂现象

    抢占式脚本要部署在backup,非抢占式脚本部署在'backup'(优先级低的)

    #lb02部署脚本如下
    [root@lb02 ~]# vim /tmp/check.sh
    #!/bin/sh
    vip=10.0.0.3
    lb_ip=10.0.0.5
    while true;do
        ping -c 2 $lb01_ip &>/dev/null
        if [ $? -eq 0 -a `ip add|grep "$vip"|wc -l` -eq 1 ];then
            pkill keepalived
            echo "存在脑裂现象,并且已经将该服务器keepalived杀死"
        else
            echo "没有脑裂现象"
        fi
    sleep 5
    done
    

    实时监测网站状态

    [root@db01 ~]# vim a.sh 
    #!/bin/bash
    
    while true ;do
            code_status=$(curl -I -m 10 -o /dev/null -s -w %{http_code} http://cs.wp.com)
            if [ $code_status -eq 200 -o 301 -o 302 ];then
                    echo $(date +%F-%T)_网站正常 >> /tmp/check
            else
                    echo $(date +%F-%T)_网站挂了 >> /tmp/check
            fi
            sleep 1
    done &
    

    企业实况

    企业中同一路由id,不同节点的服务器会有多个,‘主从配置’略有不同
    具体情况具体分析

  • 相关阅读:
    《CLR via C#》读书笔记1 之 CLR的执行模型
    C#中的事件和委托
    优分享VR开源啦,优分享VR是基于Google VR开发的一款手机VR视频资源的聚合软件
    安卓端开源移动浏览器开源项目
    Android客户端发布博客
    博客园的IOS客户端“我的博客园”已发布到AppStore
    博客园的IOS客户端图片展示
    ios在tableview里面加subview后在ip4和ip5上显示不一样的问题
    APP开发手记01(app与web的困惑)
    ios开发3.5和4.0寸屏幕自适应中的一点问题
  • 原文地址:https://www.cnblogs.com/syy1757528181/p/13060633.html
Copyright © 2020-2023  润新知