11、安装部署keepalived

1、条件部署

配置两台lb主机(要求nginx配置相同)

2、安装、启动、开机自启动keepalived服务

vim /etc/keepalived/keepalived.conf
yum install -y keepalived
systemctl start keepalived
systemctl enable keepalived

3、keepalived,VIP配置

global_defs {
    router_id lb01
}
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
    }
}

global_defs {
    router_id lb02
}
vrrp_instance VI_1 {
    state BACKUP
        interface eth0
        virtual_router_id 50
        priority 100
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass 1111
        }
    virtual_ipaddress {
        10.0.0.3
    }
}

4、给keepalived添加脚本

[root@lb01 scripts]# mkdir -p /server/scripts
[root@lb01 scripts]# vim /server/scripts/master.sh
[root@lb01 scripts]# chmod +x /server/scripts/master.sh 

MASTER检查nginx服务是否正常,如果停止服务会自动开启,如开启失败则自动关闭keepalived服务由BACKUP接管业务

#!/bin/bash
nginxpid=$(netstat -tlnp|grep [n]ginx|wc -l)

if [ $nginxpid -eq 0 ];then
        systemctl start nginx
        sleep 3
        nginxpid=$(netstat -tlnp|grep [n]ginx|wc -l)
if [ $nginxpid -eq 0 ];then
        systemctl stop keepalived
        fi
fi
[root@lb01 scripts]# vim /etc/keepalived/keepalived.conf

global_defs {
    router_id lb01
}
vrrp_script master {
    script "ght 50/server/scripts/master.sh"
    interval 5
    weight 50
}
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 {
                master

    }
}

如果BACKUP获得VIP,检查MASTER是否还活着(脑裂),如果发现两个VIP则关闭BACKUP的keepalived

[root@lb02 ~]# vim /server/scripts/backup.sh
#!/bin/bash/
server=`curl -I -s 10.0.0.5|grep 200|wc -l`
client=`ip a|grep 10.0.0.3|wc -l`

if [ ${server} -eq 1 -a ${client} -eq 1 ];then
      systemctl stop keepalived
fi
[root@lb02 scripts]# vim /etc/keepalived/keepalived.conf
global_defs {
    router_id lb02
}
vrrp_script client {
    script "ght 50/server/scripts/client.sh"
    interval 5
    weight 50
}

vrrp_instance VI_1 {
    state BACKUP
        interface eth0
        virtual_router_id 50
        priority 100
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass 1111
        }
    virtual_ipaddress {
        10.0.0.3
    }

        ck_script {
          client

    }

}

5、重启keepalived服务.

systemctl restart keepalived
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。