KeepLived 实现

image.png

虚拟VIP浮动

  1. 安装
yum install keepalived
  1. 设置ka的预先配置环境
1. 设置基于key的ssh验证
2. 添加ka server的dns解析至/etc/hosts文件
  1. 配置文件
    vim /etc/keepalived/keepalived.conf
## maskter
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalive@localhost
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id ka1
   vrrp_mcast_group4 224.100.100.100      #设置多播地址
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1            # 公告的时间间隔
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.80.100/24
    }
}

## backup
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalive@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1
   vrrp_mcast_group4 224.100.100.100
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.80.100/24
    }
}

# 测试
tcpdump -i eth0 -nn host 224.100.100.100

触发脚本施行

# 当时间发生时,触发/etc/keepalived/ka_script.sh 执行
vrrp_instance VI_1 {
    notify_master "/etc/keepalived/ka_script.sh master"
    notify_backup "/etc/keepalived/ka_script.sh backup"
    notify_fault "/etc/keepalived/ka_script.sh fault"
}

双主配置

  1. ka1
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalive@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1
   vrrp_mcast_group4 224.100.100.100
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 66
    priority 100
    advert_int 1
    notify_master "/etc/keepalived/ka_script.sh master"
    notify_backup "/etc/keepalived/ka_script.sh backup"
    notify_fault "/etc/keepalived/ka_script.sh fault"
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.80.100/24
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 88
    priority 80
    advert_int 1
    notify_master "/etc/keepalived/ka_script.sh master"
    notify_backup "/etc/keepalived/ka_script.sh backup"
    notify_fault "/etc/keepalived/ka_script.sh fault"
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.80.200/24 dev eth0 label eth0:2
    }
}
  1. ka2
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalive@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1
   vrrp_mcast_group4 224.100.100.100
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 66
    priority 80
    advert_int 1
     notify_master "/etc/keepalived/ka_script.sh master"
    notify_backup "/etc/keepalived/ka_script.sh backup"
    notify_fault "/etc/keepalived/ka_script.sh fault"
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.80.100/24
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 88
    priority 100
    advert_int 1
     notify_master "/etc/keepalived/ka_script.sh master"
    notify_backup "/etc/keepalived/ka_script.sh backup"
    notify_fault "/etc/keepalived/ka_script.sh fault"
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.80.200/24 dev eth0 label eth0:2
    }
}

单主KeepAlived + LVS

  1. ka1
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalive@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1
   vrrp_mcast_group4 224.100.100.100
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 66
    priority 100
    advert_int 1
    notify_master "/etc/keepalived/ka_script.sh master"
    notify_backup "/etc/keepalived/ka_script.sh backup"
    notify_fault "/etc/keepalived/ka_script.sh fault"
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.80.100/24
    }
}

virtual_server 192.168.80.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    sorry_server 127.0.0.1 80
    real_server 192.168.80.13 80 {
        weight 1
        HTTP_GET {
                url {
            path /
            status_code 200
        }
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3 
        } 
    }
    real_server 192.168.80.14 80 {
            weight 1
            HTTP_GET {
                url {
                    path /
                    status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3 
            }   
        }
}
  1. ka2
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalive@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1
   vrrp_mcast_group4 224.100.100.100
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 66
    priority 80
    advert_int 1
     notify_master "/etc/keepalived/ka_script.sh master"
    notify_backup "/etc/keepalived/ka_script.sh backup"
    notify_fault "/etc/keepalived/ka_script.sh fault"
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.80.100/24
    }
}

virtual_server 192.168.80.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    sorry_server 127.0.0.1 80
    real_server 192.168.80.13 80 {
        weight 1
        HTTP_GET {
                url {
            path /
            status_code 200
        }
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3 
        } 
    }
    real_server 192.168.80.14 80 {
            weight 1
            HTTP_GET {
                url {
                    path /
                    status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3 
            }   
        }
}

你可能感兴趣的:(KeepLived 实现)