keepalive高可用

1. 安装 Keepalived

  • 在主机 C 和 D 上安装 Keepalived:
    1
    2
    sudo yum install keepalived -y  # CentOS/RHEL
    sudo apt-get install keepalived -y # Debian/Ubuntu

2. 配置 Keepalived

  • 配置 Keepalived 以管理 VIP 并在故障转移时运行自定义脚本。

  • 在主机 C 上创建/编辑 /etc/keepalived/keepalived.conf:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    ! Configuration File for keepalived

    global_defs {
    #keepalived机器标识,无特殊作用,一般为机器名
    router_id LVS_DEVEL
    }


    vrrp_instance VI_1 {
    state MASTER
    interface bond0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass your_password
    }
    virtual_ipaddress {
    192.168.1.100/24 dev bond0
    }
    track_script {
    chk_service
    }
    notify_master "/etc/keepalived/master.sh"
    notify_backup "/etc/keepalived/backup.sh"
    notify_fault "/etc/keepalived/fault.sh"
    nopreempt # 添加这个参数以防止主机故障恢复后自动切回-防止业务的断层
    }

    vrrp_script chk_service {
    script "/etc/keepalived/check_service.sh"
    interval 2
    weight -20
    }
  • 在主机 D 上创建/编辑 /etc/keepalived/keepalived.conf:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    ! Configuration File for keepalived

    global_defs {
    #keepalived机器标识,无特殊作用,一般为机器名
    router_id LVS_DEVEL
    }


    vrrp_instance VI_1 {
    state BACKUP
    interface bond0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass your_password
    }
    virtual_ipaddress {
    192.168.1.100/24 dev bond0
    }
    track_script {
    chk_service
    }
    notify_master "/etc/keepalived/master.sh"
    notify_backup "/etc/keepalived/backup.sh"
    notify_fault "/etc/keepalived/fault.sh"
    }

    vrrp_script chk_service {
    script "/etc/keepalived/check_service.sh"
    interval 2
    weight -20
    }
  • 192.168.1.100/24 dev bond0 #ip是虚拟ip 这里bond0 是虚拟网卡名称(通过bond技术来让双网卡做冗余-详情看linux双网卡冗余篇)

  • 自行编写以下三个脚本(不是必须):

    notify_master “/etc/keepalived/master.sh” #选举为master时执行脚本
    notify_backup “/etc/keepalived/backup.sh” #选举为backup时执行脚本
    notify_fault “/etc/keepalived/fault.sh” #故障时执行脚本

3. 编写自定义脚本

  • /etc/keepalived/check_service.sh:用于检查业务服务是否运行
    1
    2
    3
    4
    5
    6
    7
    8
    #!/bin/bash
    # 检查业务服务是否运行的脚本
    # 假设业务服务名为 "my_service"
    if systemctl status my_service > /dev/null; then
    exit 0
    else
    exit 1
    fi

4. 设置脚本权限

1
sudo chmod +x /etc/keepalived/*.sh

5. 启动 Keepalived

1
2
sudo systemctl start keepalived
sudo systemctl enable keepalived

nginx + keepalived 高可用配置参考

扩展

配置多个Virtual Router

在主机A和主机B上分别配置Keepalived
以下是主机A上的配置示例,你需要在主机B上进行类似的配置,但将state字段设置为BACKUP,并调整priority字段。

主机A的Keepalived配置(/etc/keepalived/keepalived.conf)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
! Configuration File for keepalived

# 全局配置
global_defs {
router_id LVS_DEVEL
}

# 配置虚拟路由器1(服务1)
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.1.140
}
}

# 配置虚拟路由器2(服务2)
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1

authentication {
auth_type PASS
auth_pass 2222
}

virtual_ipaddress {
192.168.1.141
}
}

主机B的Keepalived配置(/etc/keepalived/keepalived.conf)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
! Configuration File for keepalived

global_defs {
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1

authentication {
auth_type PASS
auth_pass 1111
}

virtual_ipaddress {
192.168.1.140
}
}

vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 90
advert_int 1

authentication {
auth_type PASS
auth_pass 2222
}

virtual_ipaddress {
192.168.1.141
}
}