10.9. keepalived

10.9.1. 架构

10.9.2. 配置容器的静态IP

vim /etc/network/interfaces

原始文件内容:

auto eth0
iface eth0 inet dhcp
hostname $(hostname)

需要修改成:

auto eth0
hostname $(hostname)
iface eth0 inet static
    address 10.196.126.12
    netmask 255.255.255.0
    gateway 10.196.126.1
    dns-nameservers 8.8.8.8 8.8.4.4
/etc/init.d/networking restart
# 或者
# 重启lxc容器。

10.9.3. nginx-1

IP: 10.196.126.12

apk add nginx
service nginx start
apk add keepalived
mkdir -p /etc/keepalived
vi /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass my_secret_password
    }
    virtual_ipaddress {
        10.196.126.14
    }
}

解释:

vrrp_instance VI_1 {
    state MASTER                    # 指定此节点为主节点
    interface eth0                  # 监控的网络接口
    virtual_router_id 51            # VRID(虚拟路由器ID),在所有节点上必须相同
    priority 100                    # 优先级,数字越大,优先级越高
    advert_int 1                    # 通告间隔时间(秒),各节点间同步信息的时间间隔
    authentication {
        auth_type PASS              # 认证类型
        auth_pass my_secret_password # 认证密码,确保所有节点一致
    }
    virtual_ipaddress {
        10.196.126.14               # 虚拟IP地址(VIP),要在节点间切换的IP
    }
}
service keepalived start

10.9.4. nginx-2

IP: 10.196.126.13

apk add nginx
service nginx start
apk add keepalived
mkdir -p /etc/keepalived
vi /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass my_secret_password
    }
    virtual_ipaddress {
        10.196.126.14
    }
}

解释:

vrrp_instance VI_1 {
    state BACKUP                    # 指定此节点为备节点
    interface eth0                  # 监控的网络接口
    virtual_router_id 51            # VRID(虚拟路由器ID),在所有节点上必须相同
    priority 90                     # 优先级,数字越大,优先级越高
    advert_int 1                    # 通告间隔时间(秒),各节点间同步信息的时间间隔
    authentication {
        auth_type PASS              # 认证类型
        auth_pass my_secret_password # 认证密码,确保所有节点一致
    }
    virtual_ipaddress {
        10.196.126.14               # 虚拟IP地址(VIP),要在节点间切换的IP
    }
}
service keepalived start