HA Cluster和KeepAlived
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HA Cluster和KeepAlived相关的知识,希望对你有一定的参考价值。
HA Cluster和KeepAlived
===========================================================================
概述:
===========================================================================
HA Cluster:
1.介绍
★Linux Cluster类型:
LB:Load Balancing,负载均衡集群;
存在SPoF(单点故障)
HA:High Availiablity,高可用集群;
HP:High Performance,高性能集群;
★什么是高可用集群(HA)?
高可用集群就是当某一个节点或服务器发生故障时,另一个节点能够自动且立即向外提供服务,即将有故障节点上的资源转移到另一个节点上去,这样另一个节点有了资源既可以向外提供服务。高可用集群是用于单个节点发生故障时,能够自动将资源、服务进行切换,这样可以保证服务一直在线。在这个过程中,对于客户端来说是透明的。
★高可用集群的衡量标准
高可用集群一般是通过系统的可靠性(reliability)和系统的可维护性(maintainability)来衡量的。
通常用平均无故障时间(MTTF)来衡量系统的可靠性,
用平均维护时间(MTTR)来衡量系统的可维护性。
因此,一个高可用集群服务可以这样来定义:HA=MTTF/(MTTF+MTTR)*100%。
★一般高可用集群的标准有如下几种:
99%:表示 一年宕机时间不超过4天;
99.9% :表示一年宕机时间不超过10小时;
99.99%: 表示一年宕机时间不超过1小时;
99.999% :表示一年宕机时间不超过6分钟
注意:
99%: %1---> 99.9%, 0.1%(提升了10倍)
2.提高系统可用性方式,降低MTTR:冗余(redundant)
★降低MTTR:冗余(redundant)
☉active/passive
active(活动节点)--> HEARTBEAT(心跳信息)--> passive(备用节点)
解释:活动节点在固定的周期内,向备用节点发送自己的心跳状态信息,使得备用节点能够获知其健康状态与否;如果在活动节点规定的周期之外备用节点依然没有获得对方的心跳信息,就将其取而代之。
问题:
passive node的数量?
资源有哪些,怎样切换?
☉shared storage:(节点之间可以共享存储)
NAS:文件共享服务器(NFS,Samba);
SAN:存储区域网络,块级别的共享;
☉Network partition(网络分区)采取的措施:
隔离设备:
node隔离:STONITH
(关闭其他节点(Shoot The Other Node In The Head);电源交换机;隔离整个节点)
资源隔离:fence
☉quorum:投票机制
with quorum: > total/2
without quorum: <= total/2
☉HA Service:
例如:nginx service:ip/nginx(高可用服务的资源)
☉TWO nodes Cluster?
辅助设备:ping node, quorum disk;
★HA Cluster实现方案:
vrrp协议的实现
keepalived
ais:完备HA集群;
heartbeat
corosync
keepalived:
1.vrrp协议
★vrrp协议:Virtual Redundant Routing Protocol(虚拟路由冗余协议)
☉术语:
虚拟路由器:Virtual Router
虚拟路由器标识:VRID(0-255)
物理路由器:
master:主设备(一个)
backup:备用设备(可以多个)
priority:优先级
VIP:Virtual IP
VMAC:Virutal MAC (00-00-5e-00-01-VRID)
☉通告:心跳,优先级等;周期性进行通告;
☉抢占式,非抢占式;
☉安全工作:
认证:无认证、简单字符认证、MD5
☉工作模式:
主/备:单虚拟路径器;
主/主:主/备(虚拟路径器1),备/主(虚拟路径器2)
★VRRP的工作过程为:
(1) 虚拟路由器中的路由器根据优先级选举出Master。Master 路由器通过发送免费ARP 报文,将自己的虚拟MAC 地址通知 给与它连接的设备或者主机,从而承担报文转发任务;
(2) Master 路由器周期性发送VRRP 报文,以公布其配置信息(优先级等)和工作状况;
(3) 如果Master 路由器出现故障,虚拟路由器中的Backup 路由器将根据优先级重新选举新的Master;
(4) 虚拟路由器状态切换时,Master 路由器由一台设备切换为另外一台设备,新的Master 路由器只是简单地发送一个携带 虚拟路由器的MAC 地址和虚拟IP地址信息的免费ARP 报文,这样就可以更新与它连接的主机或设备中的ARP 相关信 息。网络中的主机感知不到Master 路由器已经切换为另外一台设备。
(5) Backup 路由器的优先级高于Master 路由器时,由Backup 路由器的工作方式(抢占方式和非抢占方式)决定是否重新 选举Master。
由此可见,为了保证Master路由器和Backup路由器能够协调工作,VRRP需要实现以下功能:
Master 路由器的选举;
Master 路由器状态的通告;
同时,为了提高安全性,VRRP 还提供了认证功能;
2.keepalived:
★vrrp协议的软件实现,原生设计的目的为了高可用ipvs服务:
vrrp协议完成地址流动;
为vip地址所在的节点生成ipvs规则(在配置文件中预先定义);
为ipvs集群的各RS做健康状态检测;
基于脚本调用接口通过执行脚本完成脚本中定义的功能,进而影响集群事务;
★组件:
核心组件:
vrrp stack
ipvs wrapper
checkers
控制组件:配置文件分析器
IO复用器
内存管理组件
附图:
keepalived组件及原理
3.keepalived安装配置
★HA Cluster的配置前提:
各节点时间必须同步;(ntp, chrony实现)
确保iptables及selinux不会成为阻碍;
各节点之间可通过主机名互相通信(对Keepalived并非必须,建议使用/etc/hosts文件实现);
各节点之间的root用户可以基于密钥认证的ssh服务完成互相通信;(并非必须)
★安装:
CentOS 6.4+ 之后的版本直接随光盘发行,所以直接yum安装即可;
★程序环境:
配置文件:/etc/keepalived/keepalived.conf
主程序:/usr/sbin/keepalived
Unit File:keepalived.service
★配置文件组件部分:
★
★
实验:配置keepalived实现高可用
1.配置前环境准备
我这里准备了两台CentOS 7 的虚拟主机,分别作为node1,和node2,因为要做为高可用集群,所以,配置前必须要保证两台主机节点的时间同步,确保iptables的规则允许,关闭selinux;最好各节点能通过主机名并基于密钥认证的ssh服务完成通信。
这里以node1为例,演示配置前环境准备
1)首先编辑/etc/hosts文件使各节点能够通过名称解析
2)基于ssh服务首先生成密钥对
[[email protected] ~]# ssh-keygen -t rsa -P ‘‘ Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 17:b0:35:1e:3b:b3:ed:a2:8d:c5:5d:a0:a9:9c:2f:1c [email protected] The key‘s randomart image is: +--[ RSA 2048]----+ | . + | | = + | | . * . | | O . | | S = . . | | .E= o . | | .+.+ o | | o* . | | o.o | +-----------------+
把生成的秘钥对复制到node2
[[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected] The authenticity of host ‘node2 (10.1.249.203)‘ can‘t be established. ECDSA key fingerprint is cb:36:da:6b:15:d6:60:25:a7:28:f1:bf:b3:22:ce:c0. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]‘s password: # 第一次需要输入密码 Number of key(s) added: 1 Now try logging into the machine, with: "ssh ‘[email protected]‘" and check to make sure that only the key(s) you wanted were added. # 连接node2节点,发现可以不需要通过输入密码后就可以连接 [[email protected] ~]# ssh node2 ‘ifconfig‘ eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.1.249.203 netmask 255.255.0.0 broadcast 10.1.255.255 inet6 fe80::20c:29ff:fe2b:b6e7 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:2b:b6:e7 txqueuelen 1000 (Ethernet) RX packets 9524 bytes 883990 (863.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 783 bytes 149357 (145.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 140 bytes 11244 (10.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 140 bytes 11244 (10.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3)同步时间
[[email protected] ~]# date;ssh node2 ‘date‘ Fri Nov 4 19:43:37 CST 2016 Fri Nov 4 19:43:37 CST 2016 [[email protected] ~]# date;ssh node2 ‘date‘ Fri Nov 4 19:43:44 CST 2016 Fri Nov 4 19:43:44 CST 2016 [[email protected] ~]# date;ssh node2 ‘date‘ Fri Nov 4 19:43:50 CST 2016 Fri Nov 4 19:43:50 CST 2016
2.安装keepalived,并编辑配置文件,如下:
[[email protected] ~]# cd /etc/keepalived/ [[email protected] keepalived]# ls keepalived.conf [[email protected] keepalived]# cp keepalived.conf{,.bak} # 首先备份 [[email protected] keepalived]# ls keepalived.conf keepalived.conf.bak [[email protected] keepalived]# vim keepalived.conf # 编辑配置文件 ! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_group4 224.0.100.19 } vrrp_instance VI_1 { state MASTER interface eno16777736 virtual_router_id 17 priority 100 advert_int 1 authentication { auth_type PASS auth_pass f7111b2e } virtual_ipaddress { 10.1.252.73/16 dev eno16777736 } }
节点node1已经编辑好了,节点2和1除了state和priority不同外,其余的都相同,所以这里我把节点1的配置文件复制到node2,然后编辑
[[email protected] keepalived]# scp keepalived.conf node2:/etc/keepalived/ keepalived.conf [[email protected] keepalived]# vim keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_group4 224.0.100.19 } vrrp_instance VI_1 { state BACKUP # 备用节点 interface eno16777736 virtual_router_id 17 priority 98 # 优先级要比主节点低 advert_int 1 authentication { auth_type PASS auth_pass f7111b2e # 要转移的ip地址和认证方式一定不能修改 } virtual_ipaddress { 10.1.252.73/16 dev eno16777736 } }
以上是关于HA Cluster和KeepAlived的主要内容,如果未能解决你的问题,请参考以下文章