keepalived concept

keepalived Overview

A, HA cluster related terms

 

1. Node (node)

HA host running a separate process, called a node, the node is a core component of HA, running the operating system and high-availability software services on each node in a high availability cluster nodes have primary and secondary points, respectively termed master node / backup nodes, each node has a unique host name, and have their own set of resources, such as disks, file systems, network addresses and application services, usually on the primary node running one or more applications services, and in general to monitor the node standby state

2. Resources (resource)

A resource is an entity node can be controlled, and when a node fails, the other takes over these resources can be nodes, the HA cluster software, can be used as the resource entities are:

(1) disk partition, the file system

(2) IP address VIP

(3) Application Services

(4) NFS file system

3. Event (event)

That is something that may occur in the cluster, such as node system failure, network connectivity failure, network card failure, application failure, these things will happen resource node metastasis, HA test is carried out based on these things

4. The operation (action)

HA response mode, the action is controlled by a shell script, for example, when a node fails, the backup node will perform well in advance when an event occurs by setting script services shut down or start, and then takes over the failed node's resources

Two, keepalived Profile

keepalived under linux is a lightweight high-availability solution that achieved with HACMP similar functions, can achieve high availability service or network, but there are differences: hacmp is a professional, full-featured high availability software, it provides the basic functions needed for HA software, such as heartbeat and resources to take over the system detects the cluster service, shared ip address of the owner transferred between cluster nodes, etc., hacmp powerful, but relatively cumbersome to deploy and use, but also business software, compared with hacmp, keepalived mainly achieved through virtual routing redundancy high-availability features, although he did not hacmp powerful, but keepalived deployment using relatively simple, all configuration only needs a configuration file to complete.

Three, keepalived use

keepalived originally designed for the lvs, designed to monitor the status of each service node of the cluster system, which according to layer3,4 & 5 exchange mechanism to detect the status of each service node, if a service node is abnormal or faulty work , keepaived will be detected and removed from the service node failure cluster system appear, and after a failed node back to normal, keepalived service node and can automatically re-added to this cluster, the work all done automatically, without manual interventions need to be done manually just to repair the failed node.

keepalived later joined the VRRP function, VRRP is an acronym for Virtual Router Redundancy Protocol (Virtual Router Redundancy Protocol), the purpose of which appears to solve the problem of static routing single point of failure occurs, the network can be achieved through continuous VRRP, stable operation, therefore, on the one hand having servers keepalived condition monitoring and fault isolation functions, on the other hand also having a function of HA Cluster, the following details the procedure the VRRP

Four, VRRP protocol and working principle

In the real network environment, the communication between the host is done by configuring static routing, and the router between hosts event of failure, communication failure will occur, so in this communication mode, the router becomes a single bottleneck point, in order to solve this problem on the introduction of the VRRP

VRRP protocol is a protocol standby mode, the switching device can be transparently without affecting the data communication between the host network failures by VRRP, which involves two concepts: the physical router and virtual router

VRRP可以将两台或者多台物理路由器设备虚拟成一个虚拟路由器,这个虚拟路由器通过虚拟IP(一个或多个)对外提供服务,二在虚拟路由器内部,是多个物理路由器协同工作,同一时间只有一台物理路由器对外提供服务,这台物理路由器被称之为主路由器(处于master状态角色)。它拥有对外提供的虚拟ip,提供各种网络功能,比如arp请、icmp、数据转发等,二其他物理路由器不拥有对外提供的虚拟ip,也不提供对外网络功能,仅仅接收master的vrrp状态通告信息,这些路由器被统称为备份路由器(处于backup角色)。当主路由器失效时,处于backup角色的备份路由器将重新进行选举,产生一个新的主路由器进入master角色继续对外服务,整个切换过程对于用户来说完全同名

在一个虚拟路由器中,只有处于master角色的路由器会一直发送vrrp数据包,处于backup角色的路由器只接受master发过来的报文信息,用来监控master运行状态,因此,不会发生master抢占的现象,除非它的优先级更高,而当master不可用时,backup也就无法收到master发过来的报文信息,于是就认定master出现故障,接着多台backup就会进行选举,优先级最高的backup将成为新的master,这种选举并进行角色的过程非常快,因此也就保证了服务的持续可用性

五、keepalived的体系结构

keepaived是一个高度模块化的软件,结构简单,但扩展性很强,下图是官方给出的keepalived的体系结构

 

可以看出来,keepalived的体系结构从整体上分为两层,分别是用户空间层和内核空间层。下面介绍一个keepalived两层结构的详细组成及实现的功能。

内核空间层处于最底层,它包括ipvs和netlink两个模块。ipvs模块是keepalived引入的一个第三方模块,通过ipvs可以实现基于ip的负载均衡集群。ipvs默认包含在lvs集群软件中。

这里有一个误区,由于keepalived可以和ipvs一起很好的工作,所以很多初学者都认为keepalived是一个负载均衡的软件,这种理解是错误的

 

内核模块:

IPVS:主要用于通过IPVS跟lvs进行整合,是lvs的核心模块,跟lvs一块使用的

NETLINK:主要实现一些网络的功能

 

用户模块:主要用于高可用

checker:检查服务状态

vrrp stack:用于DS高可用

 

六、keepalived的安装与配置

yum install keepalived

yum安装keepalived默认配置文件在/etc/keepalived/keepalived.conf

 

 

配置文件主要分为三类分别是:

(1)全局配置

(2)VRRP配置

(3)LVS配置

基于mysql或者mariadb数据库的高可用如下配置

复制代码
! Configuration File for keepalived

global_defs {
notification_email {
}
}
vrrp_script chk_mysql { #check_mysql是为执行脚本计划起的名字
script "/root/sh/check_mysql.sh" #指定要执行脚本的路径
interval 2 ##脚本执行间隔,每2s检测一次
}

vrrp_instance HA_1 {
state BACKUP
interface ens33
virtual_router_id 151
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.254.250
}
track_script {
chk_mysql
}
}

vrrp_script chk_mysql { #check_mysql是为执行脚本计划起的名字
script "/root/sh/check_mysql.sh" #指定要执行脚本的路径
interval 2 ##脚本执行间隔,每2s检测一次
}

 

vrrp_instance HA_1 {
state BACKUP
interface ens33
virtual_router_id 151
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.254.250
}
track_script {
chk_mysql
}
}
复制代码

基于web网站的负载均衡高可用如下配置

复制代码
keepalived:
yum install keepalived
vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
#全局配置
global_defs {
notification_email {
#收件人地址
}
#邮件服务器
}
VRRP配置DS1
vrrp_instance VI_1 {
state MASTER #角色类型MASTER|BACKUP
interface ens33 #网卡名称
virtual_router_id 51 #虚拟路由id(需要与BACKUP一致)
priority 100 #优先级
advert_int 1 #没1秒检查一次
#nopreempt #非抢占模式

authentication {
auth_type PASS #认证类型 主备之间必须一样
auth_pass 1111 #认证密码 主备之间必须一样
}
virtual_ipaddress {
192.168.254.250 #虚拟ip(vip)
}
}

#LVS配置
virtual_server 192.168.254.250 80 {
delay_loop 3 #健康检查时间间隔
lb_algo rr #负载均衡调度算法
lb_kind DR #负载均衡转发规则
protocol TCP #协议

real_server 192.168.254.18 80 { #要监控的real_server的ip和端口号
weight 1 #权重
TCP_CHECK { #基于tcp协议的检查
connect_timeout 3 #连接时间超时
retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
}
}


real_server 192.168.254.19 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
复制代码

 

 

上述配置虽然可以预防RS的单点故障,但是DS的单点故障并没有消除,所以需要给DS在加一个备份机器

上述配置中拷贝一份到备机上可以用scp keepalived.conf [email protected]:/etc/keepalived

需要改写的地方:

state MASTER #角色类型MASTER|BACKUP

priority 55 #优先级(这里需要设置比master机器小的值)

 

Guess you like

Origin www.cnblogs.com/itzhao/p/11267658.html