keepalived单个服务绑定vip实验(服务级别高可用)

标题是个伪命题,该实验的结果如图所示:
keepalived单个服务绑定vip实验(服务级别高可用)_第1张图片
感觉keepalived可以支持服务(ip:port)级别的高可用。keepalived可以支持多个vip并可根据脚本来转换单个vip至不同服务器上,感觉有这个功能就能做到服务级别的高可用,单个服务绑定单个vip。之前一直认为keepalived是服务器级别的高可用,转移了一个vip就得全部转移。vrrp_script和vrrp_instance相互绑定,在vrrp_script中可编写服务相关脚本,weight是权重,负值代表减去权重。
服务器A:192.168.5.10
服务器B:192.168.5.20

服务器A配置文件如下:

# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
        notification_email {
            [email protected]
        }
        notification_email_from [email protected]
        smtp_server smtp.xxxxx.com.cn
        smtp_connect_timeout 30
        router_id hw001
}


vrrp_script check_apps_6000 {
    script "[[ -e /etc/keepalived/down ]] && exit 1 || exit 0"
    interval 1
    weight -2
}

vrrp_script check_apps_6001 {
    script "[[ -e /etc/keepalived/down6001 ]] && exit 1 || exit 0"
    interval 1
    weight -2
}

vrrp_instance hw_vi_6000 {
        state MASTER

        interface enp0s3

        virtual_router_id 60
        priority 100
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass hw123456
        }

        virtual_ipaddress {
                192.168.5.200
        }

        track_script {
            check_apps_6000
        }
}

vrrp_instance hw_vi_6001 {
        state MASTER

        interface enp0s3

        virtual_router_id 61
        priority 100
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass hw123456
        }

        virtual_ipaddress {
                192.168.5.201
        }

        track_script {
            check_apps_6001
        }
}

服务器B配置如下:

# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
        notification_email {
            [email protected]
        }
        notification_email_from [email protected]
        smtp_server smtp.xxxxx.com.cn
        smtp_connect_timeout 30
        router_id hw001
}

vrrp_script check_apps_6000 {
    script "[[ -e /etc/keepalived/down ]] && exit 1 || exit 0"
    interval 1
    weight -2
}

vrrp_script check_apps_6001 {
    script "[[ -e /etc/keepalived/down6001 ]] && exit 1 || exit 0"
    interval 1
    weight -2
}

vrrp_instance hw_vi_6000 {
        state BACKUP

        interface enp0s3

        virtual_router_id 60
        priority 99
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass hw123456
        }

        virtual_ipaddress {
                192.168.5.200
        }

        track_script {
            check_apps_6000
        }
}

vrrp_instance hw_vi_6001 {
        state BACHKUP

        interface enp0s3

        virtual_router_id 61
        priority 99
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass hw123456
        }

        virtual_ipaddress {
                192.168.5.201
        }

        track_script {
            check_apps_6001
        }
}

服务器A,B根据如上配置,启动keepalived后,配置文件中的两个vip会落在服务器A上,在服务器A上/etc/keepalived/目录下建立文件down,会发现192.168.5.200在服务器B上,两个vip在不同的服务器上。

配置文件部分参数解析:

vrrp_script:

script 可在此编写简单脚本,亦可外部倒入脚本(推荐);
interval 1 在此代表检测时间为1秒;
weight -2  正值代表权重相加,在此代表权重减2;

vrrp_instance:

interface 设置vip所在网卡,值可通过命令`ip addr`查看;
virtual_router_id 此值需各不相同,默认范围(1-255),和vmac相关;
priority 初始权重;
track_script 调用所需脚本(vrrp_script)。

:查看vip,使用命令ip addr show

你可能感兴趣的:(keepalived)