实验要求 ==> 实现Keepalived高可用对之间的故障切换过程,配置的vrrp实例的VIP为192.168.10.66,可以将其配置在网卡别名上。

实验环境 ==> CentOS 7.x

实验目的 ==> 实现Keepalived高可用对之间的故障切换功能

实验主机 ==> 192.168.10.6(主机名:node1)、192.168.10.7(主机名:node2)

实验前提 ==> 高可用对之间时间同步(可通过周期性任务来实现)

实验操作如下。

1、编辑keepalived配置文件

注意:使用vim打开/etc/keepalived/keepalived.conf后,先注释掉virtual_server相关的配置,可在末行模式下使用命令":.,$s/^/#/g"注释。

2、启动keepalived服务

(1) 在node1(192.168.10.6)上

[root@node1 ~]# systemctl start keepalived.service		# 启动keepalived服务
[root@node1 ~]# systemctl status keepalived.service # 查看keepalived服务状态
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2017-07-08 05:51:16 CST; 5s ago
# 已经在运行中
Process: 16520 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 16521 (keepalived)
CGroup: /system.slice/keepalived.service
├─16521 /usr/sbin/keepalived -D
├─16522 /usr/sbin/keepalived -D
└─16523 /usr/sbin/keepalived -D Jul 08 05:51:18 node1 Keepalived_healthcheckers[16522]: Netlink reflector reports IP 192.168.10.6 added
Jul 08 05:51:18 node1 Keepalived_healthcheckers[16522]: Registering Kernel netlink reflector
Jul 08 05:51:18 node1 Keepalived_healthcheckers[16522]: Registering Kernel netlink command channel
Jul 08 05:51:18 node1 Keepalived_healthcheckers[16522]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 08 05:51:18 node1 Keepalived_healthcheckers[16522]: Configuration is using : 7429 Bytes
Jul 08 05:51:18 node1 Keepalived_healthcheckers[16522]: Using LinkWatch kernel netlink reflector...
Jul 08 05:51:18 node1 Keepalived_vrrp[16523]: VRRP_Instance(VI_1) Entering MASTER STATE
# node1启动后状态为MASTER路由器
Jul 08 05:51:18 node1 Keepalived_vrrp[16523]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 08 05:51:18 node1 Keepalived_vrrp[16523]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.10.66
# node1发送免费ARP通告虚拟路由所在的MAC地址(ens33)
Jul 08 05:51:18 node1 Keepalived_healthcheckers[16522]: Netlink reflector reports IP 192.168.10.66 added
# 已经添加虚拟路由IP(192.168.10.66)
 

同样的信息可在/var/log/message中获得。

[root@node1 ~]# tail -5 /var/log/messages
Jul 8 05:51:18 localhost Keepalived_vrrp[16523]: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 8 05:51:18 localhost Keepalived_vrrp[16523]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 8 05:51:18 localhost Keepalived_vrrp[16523]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.10.66
Jul 8 05:51:18 localhost Keepalived_healthcheckers[16522]: Netlink reflector reports IP 192.168.10.66 added
Jul 8 05:51:23 localhost Keepalived_vrrp[16523]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.10.66
 

查看虚拟路由IP。

[root@node1 ~]# ip addr l
(...省略...)
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1280 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:13:d1:d8 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.6/24 brd 192.168.10.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.10.66/32 scope global ens33# 已经配置了虚拟路由IP
valid_lft forever preferred_lft forever
 

(2) 在node2(192.168.10.7)上

[root@node2 ~]# systemctl start keepalived.service
[root@node2 ~]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2017-07-08 06:48:36 CST; 30s ago
Process: 14507 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 14508 (keepalived)
CGroup: /system.slice/keepalived.service
├─14508 /usr/sbin/keepalived -D
├─14509 /usr/sbin/keepalived -D
└─14510 /usr/sbin/keepalived -D Jul 08 06:48:36 node2 Keepalived_vrrp[14510]: Configuration is using : 62884 Bytes
Jul 08 06:48:36 node2 Keepalived_vrrp[14510]: Using LinkWatch kernel netlink reflector...
Jul 08 06:48:36 node2 Keepalived_vrrp[14510]: VRRP_Instance(VI_1) Entering BACKUP STATE
# 启动后作为BACKUP路由器
Jul 08 06:48:36 node2 Keepalived_vrrp[14510]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Jul 08 06:48:36 node2 Keepalived_healthcheckers[14509]: Netlink reflector reports IP 192.168.10.7 added
Jul 08 06:48:36 node2 Keepalived_healthcheckers[14509]: Registering Kernel netlink reflector
Jul 08 06:48:36 node2 Keepalived_healthcheckers[14509]: Registering Kernel netlink command channel
Jul 08 06:48:36 node2 Keepalived_healthcheckers[14509]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 08 06:48:36 node2 Keepalived_healthcheckers[14509]: Configuration is using : 7427 Bytes
Jul 08 06:48:36 node2 Keepalived_healthcheckers[14509]: Using LinkWatch kernel netlink reflector...
 

此时VIP应该只配置在node1的ens33接口上,而不是配置在本主机(node2)的ens34接口上。

[root@node2 ~]# ip a
ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1280 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:68:80:eb brd ff:ff:ff:ff:ff:ff
inet 192.168.10.7/24 brd 192.168.10.255 scope global ens34
valid_lft forever preferred_lft forever
# BACKUP状态下没有配置VIP
 

3、暂停node1上的keepalived服务,测试node2节点是否会自动转变为MASTER状态

(1) 在node1(192.168.10.6)上

[root@node1 ~]# systemctl stop keepalived.service
[root@node1 ~]# tail /var/log/messages
Jul 8 06:58:20 localhost Keepalived[16723]: Stopping Keepalived v1.2.13 (11/05,2016)
# 关闭keepalived服务
Jul 8 06:58:20 localhost systemd: Stopping LVS and VRRP High Availability Monitor...
Jul 8 06:58:20 localhost Keepalived_vrrp[16725]: VRRP_Instance(VI_1) sending 0 priority
# 将自身优先级将为0,以让其它节点接任Master
Jul 8 06:58:20 localhost Keepalived_vrrp[16725]: VRRP_Instance(VI_1) removing protocol VIPs.
# 移除VIP
Jul 8 06:58:20 localhost Keepalived_healthcheckers[16724]: Netlink reflector reports IP 192.168.10.66 removed
# VIP已经从node1上移除
Jul 8 06:58:20 localhost systemd: Stopped LVS and VRRP High Availability Monitor.
 

查看ip地址。

[root@node1 ~]# ip a
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1280 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:13:d1:d8 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.6/24 brd 192.168.10.255 scope global ens33
valid_lft forever preferred_lft forever
# node1已经移除了VIP
 

(2) 在node2(192.168.10.7)上

[root@node2 ~]# tail /var/log/messages
Jul 8 06:58:21 localhost Keepalived_vrrp[14510]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 8 06:58:22 localhost Keepalived_vrrp[14510]: VRRP_Instance(VI_1) Entering MASTER STATE
# node2转变为Master状态
Jul 8 06:58:22 localhost Keepalived_vrrp[14510]: VRRP_Instance(VI_1) setting protocol VIPs.
# node2设置VIP
Jul 8 06:58:22 localhost Keepalived_vrrp[14510]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens34 for 192.168.10.66
# node2发送免费ARP,通告现在VIP对应的MAC地址
Jul 8 06:58:22 localhost Keepalived_healthcheckers[14509]: Netlink reflector reports IP 192.168.10.66 added
Jul 8 06:58:27 localhost Keepalived_vrrp[14510]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens34 for 192.168.10.66
 

此时在node2的ens34接口上应该配置有VIP。

[root@node2 ~]# ip a
ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1280 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:68:80:eb brd ff:ff:ff:ff:ff:ff
inet 192.168.10.7/24 brd 192.168.10.255 scope global ens34
valid_lft forever preferred_lft forever
inet 192.168.10.66/32 scope global ens34# VIP
valid_lft forever preferred_lft forever
 

4、再次启动node1节点的keepalived服务,测试node1是否能够转变为Master状态,并夺回VIP

(1) 在node1(192.168.10.6)上

[root@node1 ~]# systemctl start keepalived.service
[root@node1 ~]# tail /var/log/messages
Jul 8 07:14:38 localhost Keepalived_vrrp[16778]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 8 07:14:38 localhost Keepalived_vrrp[16778]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Jul 8 07:14:39 localhost Keepalived_vrrp[16778]: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 8 07:14:39 localhost Keepalived_vrrp[16778]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 8 07:14:39 localhost Keepalived_vrrp[16778]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.10.66
Jul 8 07:14:39 localhost Keepalived_healthcheckers[16777]: Netlink reflector reports IP 192.168.10.66 added
Jul 8 07:14:44 localhost Keepalived_vrrp[16778]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens33 for 192.168.10.66
# node1再次发送免费ARP
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]# ip a
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1280 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:13:d1:d8 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.6/24 brd 192.168.10.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.10.66/32 scope global ens33 # VIP已经夺回
valid_lft forever preferred_lft forever
 

(2) 在node2(192.168.10.7)上

[root@node2 ~]# tail /var/log/messages
Jul 8 07:14:39 localhost Keepalived_vrrp[14510]: VRRP_Instance(VI_1) Received higher prio advert
# node2收到node1发送的vrrp报文,发现node1的优先级比自己的高
Jul 8 07:14:39 localhost Keepalived_vrrp[14510]: VRRP_Instance(VI_1) Entering BACKUP STATE
# node2重新返回BACKUP状态
Jul 8 07:14:39 localhost Keepalived_vrrp[14510]: VRRP_Instance(VI_1) removing protocol VIPs.
Jul 8 07:14:39 localhost Keepalived_healthcheckers[14509]: Netlink reflector reports IP 192.168.10.66 removed
# node2将VIP移除
 

此时node2上应该没有配置VIP。

[root@node2 ~]# ip a
ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1280 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:68:80:eb brd ff:ff:ff:ff:ff:ff
inet 192.168.10.7/24 brd 192.168.10.255 scope global ens34
valid_lft forever preferred_lft forever
 

测试演示完毕。

补充:

此外,也可将VIP配置在网卡别名上,例如:

# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
...
virtual_ipaddress {
192.168.10.66 dev ens33 label ens33:0
}
}
 

这样配置的VIP则可以通过ifconfig命令来查看。

原文链接:https://blog.51cto.com/xuweitao/1953656

配置Keepalived单实例实现单IP自动漂移接管的更多相关文章

  1. Oracle 11g 单实例到单实例OGG同步实施文档-OGG initial load

    Oracle 11g 单实例到单实例OGG同步实施文档-OGG initial load 2018-06-07 00:514730原创GoldenGate 作者: leo 本文链接:https://w ...

  2. Oracle 11g 单实例到单实例OGG同步实施文档-RMAN 初始化

    Oracle 11g 单实例到单实例OGG同步实施文档-RMAN 初始化 2018-06-07 13:455170原创GoldenGate 作者: leo 本文链接:https://www.cndba ...

  3. Oracle 11g 单实例到单实例OGG同步实施文档-EXPDP初始化

    Oracle 11g 单实例到单实例OGG同步实施文档-EXPDP初始化 2018-06-07 00:446470原创GoldenGate 作者: leo 本文链接:https://www.cndba ...

  4. ORACLE11g R2【单实例 FS→单实例FS】

    ORACLE11g R2[单实例 FS→单实例FS] 本演示案例所用环境:   primary standby OS Hostname pry std OS Version RHEL6.5 RHEL6 ...

  5. ORACLE10g R2【单实例 FS→单实例FS】

    ORACLE10g R2[单实例FS→单实例FS] 本演示案例所用环境:   primary standby OS Hostname pry std OS Version RHEL5.8 RHEL5. ...

  6. DataGuard 单实例到RAC搭建

    背景简介: 本文为针对一次windows平台RAC数据库迁移至Linux平台RAC的笔记,基本步骤为: 1.搭建windows RAC到Linux 单实例数据库的DataGuard 2.做switch ...

  7. Oracle - 给rac创建单实例dg,并做主从切换

    一.概述 本文将介绍如何给rac搭建单节点的dg,以及如何对其进行角色转换.预先具备的知识(rac搭建,单实例-单实例dg搭建) 二.实验环境介绍 主库rac(已安装rac,并已有数据库orcl)ra ...

  8. 【释疑】tp99、单实例qps

    tp99 tp99的定义 tp99 (top percentile 99),指一组数据从小到大排列,处于99%位置的数据的值.例如等差数列range(1,101),tp99=99 tp99优于平均值的 ...

  9. debian下配置keepalived ha

    抄袭自http://blog.51yip.com/server/1417.html,做了一些修改 可以参考http://blog.linuxphp.org/archives/1615/ 备注:NAT模 ...

随机推荐

  1. nginx安装教程(详细)

    所见即所得编辑器, editorhtml{cursor:text;*cursor:auto} img,input,textarea{cursor:default}.cke_editable{curso ...

  2. 【Java】 java判断字符串是否为空的方法总结

    以下是java 判断字符串是否为空的四种方法: 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equals(s));方法二: ...

  3. docker--image的获取

    image有几种获取方式: 1.Docker官方提供了一种文件格式:Dockerfile,通过这种格式的文件,我们可以定义一个image,然后通过Dockerfile我们可以构建(build)一个im ...

  4. Windows Nginx 教程

    1. 下载Nginx: Nginx下载 (官方网站) 作者使用版本  Stable version  nginx/Windows-1.12.0

  5. less&sass

    定义: less是一种动态样式语言,对css赋予了动态语言的特性,比如变量.继承.运算.函数,既可以运行在客户端,也可以运行在服务器端,依赖JavaScript   sass是一种动态语言,属于缩排语 ...

  6. 用户界面样式(cursor,resize,vertical-align,outline,文字超出显示省略号)

    1. 鼠标样式 cursor default: 小白(箭头)默认 pointer:小手 move:移动 text:文本 not-allowed:禁止 2. 轮廓线(表单外发光)outline 给表单添 ...

  7. 使用python+ffmpeg批量转换格式

    需求:  给定一个文件夹路径,遍历该文件夹内的所有文件以及子文件夹内的文件,当所有后缀名为wav格式的文件转换为ogg格式的文件. import os # 获取目录下的所有文件列表 import fn ...

  8. 通信矩阵转DBC

    DBC的制作对于一些人来时比较陌生,熟悉的人做他感觉浪费时间(像我这样的),于是自己用PYTHON写了一个脚本,还挺好用的,只需要填写表格就好了,省出来大部分的时间. 分享下思路, 来看下DBC的文本 ...

  9. fiddler增加ip以及响应时间列

    最近打算看一下移动端app的响应等请求,这里打算用fillder来查看appium的模拟出发请求的操作来查看结果, 所以我们需要在左侧的面板增加我们所需要的ip,响应时间等数据以方便我们查看 fidd ...

  10. php文件锁阻塞模式和非阻塞模式

    1.阻塞模式(如果其它进程已经加锁文件,当前进程会等其它进程解锁后继续执行) <?php $handle = fopen('lock.txt', 'r'); //锁定 if(flock($han ...