keepalived 2.0.12

官方:http://www.keepalived.org/

一 简介

Keepalived is a routing software written in C. The main goal of this project is to provide simple and robust facilities for loadbalancing and high-availability to Linux system and Linux based infrastructures. Loadbalancing framework relies on well-known and widely used Linux Virtual Server (IPVS) kernel module providing Layer4 loadbalancing. Keepalived implements a set of checkers to dynamically and adaptively maintain and manage loadbalanced server pool according their health. On the other hand high-availability is achieved by VRRP protocol. VRRP is a fundamental brick for router failover. In addition, Keepalived implements a set of hooks to the VRRP finite state machine providing low-level and high-speed protocol interactions. In order to offer fastest network failure detection, Keepalived implements BFD protocol. VRRP state transition can take into account BFD hint to drive fast state transition. Keepalived frameworks can be used independently or all together to provide resilient infrastructures.

keepalived是用c写的路由软件,使用vrrp协议(Virtual Router Redundancy Protocol)和arp协议 (Address Resolution Protocol)实现简单和健壮的负载均衡和高可用;

VRRP 将局域网的一组路由器(包括一个Master 即活动路由器和若干个Backup 即备份路由器)组织成一个虚拟路由器,称之为一个备份组。这个虚拟的路由器拥有自己的IP 地址10.100.10.1(这个IP 地址可以和备份组内的某个路由器的接口地址相同,相同的则称为ip拥有者),备份组内的路由器也有自己的IP 地址(如Master的IP 地址为10.100.10.2,Backup 的IP 地址为10.100.10.3)。局域网内的主机仅仅知道这个虚拟路由器的IP 地址10.100.10.1,而并不知道具体的Master 路由器的IP 地址10.100.10.2 以及Backup 路由器的IP 地址10.100.10.3。它们将自己的缺省路由下一跳地址设置为该虚拟路由器的IP 地址10.100.10.1。

原理

主从节点之间通过广播或组播的方式发送vrrp包,然后根据priority来选举出master

14:20:21.521870 IP 192.168.0.1 > 192.168.0.2: VRRPv2, Advertisement, vrid 99, prio 100, authtype simple, intvl 1s, length 20

一旦master一定时间内没有及时发出vrrp包出来,则其他standby会发vrrp包再根据priority选举出master;

master会发送arp包,

Jan 28 19:04:26 cdp-test-server-05 Keepalived_vrrp[27675]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.0.3
Jan 28 19:04:26 cdp-test-server-05 Keepalived_vrrp[27675]: Sending gratuitous ARP on eth0 for 192.168.0.3

注意这里是虚拟ip(vip)的gratuitous ARP,

先看ARP (Address Resolution Protocol, 地址解析协议),将IP地址转换为MAC地址

ARP的过程:在Host A上发送ARP请求,内容为who has [IP_B], tell [IP_A], 包里携带了主机B的IP地址,以及主机A的IP和MAC。收到广播包的所有主机会检查请求的IP 地址是否是自己的,如果是,就会发送一个ARP应答(单播,从B到A),内容为 [IP_B] is at [MAC_B],包里携带了主机A和B的MAC及IP地址。

# arping 192.168.0.1

刚才的场景中如果Host A发请求的时候,内容为who has [IP_A], tell [IP_A],则这是一个gratuitous ARP,为什么会请求自己的IP,因为:正常的ARP是向其他主机请求信息,而免费ARP是主动向其他主机广播自己的信息,所以免费ARP不期待响应;

发送gratuitous ARP后收到广播包的所有主机或者交换机都可以通过命令查看vip和mac(master mac)映射:

# arp -a

这样其他主机就可以通过vip访问到master,也可以通过arp手工绑定

# arp -s 192.168.0.3 00-02-b3-3c-16-95

另外可以通过设置vrrp_garp_master_refresh来让master定期发送gratuitous ARP包;

如果是在云主机环境,就不用考虑搭keepalived了,因为云上通常会禁止vrrp协议的组播以及arp,可以考虑直接用云平台的虚拟ip服务;

二 安装

# yum install keepalived

主节点配置

# vi /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {

state MASTER

interface eth0

unicast_src_ip 192.168.0.1

unicast_peer {

192.168.0.2

}

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.0.3

}

}

virtual_server 192.168.0.3 81 {

delay_loop 6

lb_algo rr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

#sorry_server 127.0.0.1 80

real_server 192.168.0.1 80 {

weight 1

}

real_server 192.168.0.2 80 {

weight 1

}

}

这里使用的是单播(unicast_src_ip、unicast_peer )的方式 ,因为很多环境下组播不能用,如果想用组播,把单播参数去掉即可;

如果real_server和keealived部署在一台机器上,不需要配置virtual_server;

注释掉

#vrrp_strict

否则会在iptables里生成一条drop规则;

从节点修改配置

state BACKUP
unicast_src_ip 对调
unicast_peer 对调
priority 50

如果开启iptables需要增加规则

# iptables -I INPUT -d 224.0.0.0/8 -j ACCEPT
# iptables -A INPUT -p vrrp -j ACCEPT

启动

# service keepalived start

查看vip

# ip a

查看tcp包

# tcpdump -p vrrp -n

参考:https://docs.oracle.com/cd/E37670_01/E41138/html/section_ksr_psb_nr.html

日志位于/var/log/messages,如果报错:

Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: Registering Kernel netlink reflector
Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: Registering Kernel netlink command channel
Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: Registering gratuitous ARP shared channel
Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: Opening file '/etc/keepalived/keepalived.conf'.
Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: (VI_1): Cannot start in MASTER state if not address owner
Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: Unable to load ipset library - libipset.so.11: cannot open shared object file: No such file or directory
Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: VRRP_Instance(VI_1) removing protocol VIPs.
Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: VRRP_Instance(VI_1) removing protocol iptable drop rule
Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: Using LinkWatch kernel netlink reflector...
Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jan 27 17:05:05 cdp-test-server-05 Keepalived_vrrp[26508]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]

需要安装ipset

# yum install ipset

然后正常

Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: Registering Kernel netlink reflector
Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: Registering Kernel netlink command channel
Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: Registering gratuitous ARP shared channel
Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: Opening file '/etc/keepalived/keepalived.conf'.
Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: (VI_1): Cannot start in MASTER state if not address owner
Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: iptc_commit returned 0: No chain/target/match by that name
Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: VRRP_Instance(VI_1) removing protocol VIPs.
Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: VRRP_Instance(VI_1) removing protocol iptable drop rule
Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: Using LinkWatch kernel netlink reflector...
Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jan 27 21:40:35 cdp-test-server-05 Keepalived_vrrp[7001]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Jan 27 21:40:38 cdp-test-server-05 Keepalived_vrrp[7001]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jan 27 21:40:39 cdp-test-server-05 Keepalived_vrrp[7001]: VRRP_Instance(VI_1) Entering MASTER STATE
Jan 27 21:40:39 cdp-test-server-05 Keepalived_vrrp[7001]: VRRP_Instance(VI_1) setting protocol iptable drop rule
Jan 27 21:40:39 cdp-test-server-05 Keepalived_vrrp[7001]: VRRP_Instance(VI_1) setting protocol VIPs.
Jan 27 21:40:39 cdp-test-server-05 Keepalived_vrrp[7001]: Sending gratuitous ARP on eth0 for 192.168.0.3
Jan 27 21:40:39 cdp-test-server-05 Keepalived_vrrp[7001]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.0.3
Jan 27 21:40:39 cdp-test-server-05 Keepalived_vrrp[7001]: Sending gratuitous ARP on eth0 for 192.168.0.3
Jan 27 21:40:39 cdp-test-server-05 Keepalived_vrrp[7001]: Sending gratuitous ARP on eth0 for 192.168.0.3
Jan 27 21:40:39 cdp-test-server-05 Keepalived_vrrp[7001]: Sending gratuitous ARP on eth0 for 192.168.0.3
Jan 27 21:40:39 cdp-test-server-05 Keepalived_vrrp[7001]: Sending gratuitous ARP on eth0 for 192.168.0.3
Jan 27 21:40:44 cdp-test-server-05 Keepalived_vrrp[7001]: Sending gratuitous ARP on eth0 for 192.168.0.3
Jan 27 21:40:44 cdp-test-server-05 Keepalived_vrrp[7001]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.0.3
Jan 27 21:40:44 cdp-test-server-05 Keepalived_vrrp[7001]: Sending gratuitous ARP on eth0 for 192.168.0.3
Jan 27 21:40:44 cdp-test-server-05 Keepalived_vrrp[7001]: Sending gratuitous ARP on eth0 for 192.168.0.3
Jan 27 21:40:44 cdp-test-server-05 Keepalived_vrrp[7001]: Sending gratuitous ARP on eth0 for 192.168.0.3
Jan 27 21:40:44 cdp-test-server-05 Keepalived_vrrp[7001]: Sending gratuitous ARP on eth0 for 192.168.0.3

为什么连续发送5个arp,因为vrrp_garp_master_repeat默认为5;

参考:http://gcharriere.com/blog/?p=339

【原创】运维基础之keepalived的更多相关文章

  1. Linux运维基础

    一.服务器硬件 二.Linux的发展史 三.Linux的系统安装和配置 四.Xshell的安装和优化 五.远程连接排错 六.Linux命令初识 七.Linux系统初识与优化 八.Linux目录结构 九 ...

  2. 第一阶段·Linux运维基础-第1章·Linux基础及入门介绍

    01-课程介绍-学习流程 02-服务器硬件-详解 03-服务器核心硬件-服务器型号-电源-CPU 01-课程介绍-学习流程 1.1. 光看不练,等于白干: 1.2 不看光练,思想怠慢: 1.3 即看又 ...

  3. linux运维基础知识

    linux运维基础知识大全 一,序言 每一个微不足道的知识,也是未来的铺垫.每一份工作的薪资职位,也是曾经努力的结果. 二,服务器 1,运维人员工作职责: 1)保证数据不丢失:2)保证服务器24小时运 ...

  4. Linux系统运维基础测试题

    1    Linux运维基础测试题(第一关) 通过这段时间学习Linux基础命令,为了检测自己对Linux基础命令掌握的情况,从网上整理13到测试题,并将其整理出来供大家参考学习. 1.1    习题 ...

  5. HBase运维基础--元数据逆向修复原理

    背景 鉴于上次一篇文章——“云HBase小组成功抢救某公司自建HBase集群,挽救30+T数据”的读者反馈,对HBase的逆向工程比较感兴趣,并咨询如何使用相应工具进行运维等等.总的来说,就是想更深层 ...

  6. Linux运维基础采集项

    1. Linux运维基础采集项 做运维,不怕出问题,怕的是出了问题,抓不到现场,两眼摸黑.所以,依靠强大的监控系统,收集尽可能多的指标,意义重大.但哪些指标才是有意义的呢,本着从实践中来的思想,各位工 ...

  7. linux运维基础__争取十月前研究的差不多

    转来的一编,考虑在十月前研究的差不多 linux运维人员基础 1.很多地方经常会用到的rsync工具 实施几台服务器的同步效果 我们公司就是使用这个工具完成服务器的游戏的服务端和客户端同步,有几个文章 ...

  8. 网络配置——Linux运维基础

    今天把Linux的网络配置总结了一下,尽管并不难可是是个比較重要的基础.然后我也不知到自己以后是否会做运维,可是我知道自己比較喜欢刨根问底.还有就是我很珍惜我以前掌握过的这些运维的技能.今天突然间问自 ...

  9. [转帖] Linux运维基础知识学习内容

    原作者地址:https://www.cnblogs.com/chenshoubiao/p/4793487.html 最近在学习 linux  对简单的命令有所掌握 但是 复杂的脚本 shell pyt ...

随机推荐

  1. 【Android】pidcat 不显示日志输出

    问题: 直接安装了 pidcat :  brew install pidcat ,装完以后执行 pidcat <package name> ,发现没有日志输出,adb devices 也能 ...

  2. [转帖]Oracle 12cR2使用经验

    大规模升级来临,谈谈Oracle 12cR2使用经验 随着2019年2月13日,Oracle 19c (Oracle 12.2.0.3) for Exadata 版本发布,Oracle 12cR2体系 ...

  3. DAY13、迭代器,生成器,枚举

    一.迭代器 1.通过迭代器取值的优缺点 优点:不依赖索引取值,完成取值 缺点:不能计算长度,不能指定位取值(只能从前往后逐一取值) 2.可迭代对象 可迭代对象是有—iter—()方法的对象,调用该方法 ...

  4. Codechef April Challenge 2019 Division 2

    Maximum Remaining 题意:给n个数,取出两个数$a_{i}$,$a_{j}$,求$a_{i}\% a_{j}$取模的最大值 直接排个序,第二大(严格的第二大)模第一大就是答案了. #i ...

  5. [ML]机器学习书单

    https://anvaka.github.io/greview/hands-on-ml/1/

  6. centos7下使用LVM给系统硬盘扩容超详细

    简单介绍: LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的 ...

  7. NLTK基础

    Python上著名的⾃然语⾔处理库 ⾃带语料库,词性分类库 ⾃带分类,分词,等等功能 强⼤的社区⽀持 还有N多的简单版wrapper 安装语料库 # 方式一 import nltk nltk.down ...

  8. Redis protocol (redis通信协议)

    以前只会用redis -cli客户端, 或者在java程序里使用jedis来作为介质与redis服务器通信.这两天正好在实习中接触了一点这方面的内容. 五一放假就稍微翻了翻redis相关资料. 本篇博 ...

  9. PYTHON使用入门

    一 写在开头1.1 本文内容PYTHON语言的基础知识. 二 Q & A2.1 为什么选择PYTHON?软件质量:在很大程度上,PYTHON更注重可读性.一致性和软件质量,从而与脚本语言世界中 ...

  10. Windows Server 在IIS上创建安全网站

    第一步.打开服务器管理器,创建用户,并设置密码,自己记录下来,注意勾选密码永不过期. 第二步.设置用户隶属组:IIS_IUSRS. 第三步.设置建立的网站文件夹权限,添加刚才建立的用户,并添加修改.读 ...