菜单

keepalived的vip设置为公网IP

2018年9月20日 - linux, linux, linux, linux, linux
目前有两种方法可以实现,这两种方法都需要机器有两块网卡,例 eth0为内网,eth1为公网ip,两台机器的eth1上分别绑定公网ip都要能正常使用。
例:
A机器ip为: 192.168.10.10
B机器ip为: 192.168.10.11
公网ip为: 123.123.123.123 网关:123.123.123.1 掩码:255.255.255.240
第一种:
1,先设置A机器与B机器的eth1的网卡配置配置文件,不要设置公网ip信息
$vim /etc/sysconfig/network-scripts/ifcfg-eth1
[code]DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
HWADDR=52:54:00:97:d2:2b
NM_CONTROLLED=”no”
USERCTL=no[/code]

然后重启网卡
2,配置A机器keepalived.conf(B机器请修改MASTER为BACKUP,priority的值小于100,其他不变)
安装yum install -y keepalived
打开文件
vim /etc/keepalived/keepalived.conf
[code]global_defs {
router_id ID_1
}
vrrp_script checkscript
{
script “/etc/keepalived/check.sh”
interval 3
weight -20
}
vrrp_instance ID {
state MASTER #设置为主服务器,备服务器设置为BACKUP
interface eth0 #监测网络接口
nopreempt #设置非抢占模式
virtual_router_id 111 #主、备必须一样
priority 100 #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)
advert_int 1 #VRRP Multicast广播周期秒数
authentication {
auth_type PASS #VRRP认证方式,主备必须一致
auth_pass 1111 #(密码)
}
track_script
{
checkscript
}
virtual_ipaddress {
123.123.123.123/28 dev eth1 #设置 在 eth1网卡上绑定 123.123.123.123 掩码为 240的公网ip作为HA虚拟地址
}
virtual_routes {
default via 123.123.123.1 # 设置默认网关为 123.123.123.1
}
}[/code]
上图中需要解释的配置如下:

virtual_ipaddress {
123.123.123.123/28 dev eth1
} ##设置 在 eth1网卡上绑定 123.123.123.123 掩码为 240的公网ip

virtual_routes {
default via 123.123.123.1
} ## 设置默认网关为 123.123.123.1
检查脚本内容如下:
[code]#!/bin/bash
count = `ps aux | grep -v grep | grep haproxy | wc -l`
if [ $count > 0 ]; then
exit 0
else
exit 1
fi[/code]
3,两边分别启动keepalived,并查看A主机的eth1网卡是否绑定公网ip 123.123.123.123。停止A主机的keepalived,查看B主机是否正常绑定公网ip。

keepalived -D -f /etc/keepalived/keepalived.conf
查看log消息:
tail -f /var/log/messages
启动主节点A后的日志为:会广播ARP消息
[code][root@srv4 ~]# tail -f /var/log/messages
Sep 20 01:45:29 srv4 Keepalived_vrrp: Configuration is using : 34546 Bytes
Sep 20 01:45:29 srv4 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]
Sep 20 01:45:30 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100
Sep 20 01:45:31 srv4 Keepalived_vrrp: Netlink reflector reports IP 192.168.8.100 added
Sep 20 01:45:31 srv4 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.8.100 added
Sep 20 01:45:31 srv4 avahi-daemon[4029]: Registering new address record for 192.168.8.100 on eth0.
Sep 20 01:45:36 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100 [/code]
通过ip a 命令可以看到192.168.8.100/24绑定到了eth0上
[code][root@srv4 bin]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:50:2d:9d brd ff:ff:ff:ff:ff:ff
inet 192.168.8.4/24 brd 192.168.8.255 scope global eth0
inet 192.168.8.100/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe50:2d9d/64 scope link
valid_lft forever preferred_lft forever [/code]
启动备用节点B后的日志为:
[code]Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: Configuration is using : 34262 Bytes
Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]
Sep 20 01:47:31 hadoopsrv Keepalived: Starting VRRP child process, pid=20567[/code]

keepalived的vip设置为公网IP》有2个想法

上课不好好学习的小胖子

期待作者发布些有关zabbix的学习文档

回复
    小学课本的小明

    莫着急,后续会上的

    回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注

18 − 4 =