Linux HA集群之Keepalived

大纲

一、什么是Keepalived

二、Keepalived工作原理

三、Keepalived + LVS的实现

一、什么是Keepalived

Keepalived是用C写的简单的一个路由软件,这个项目的主要目标是对Linux系统和基于Linux的基础设施提供简单而强大负载均衡和高可用性。负载均衡架构依赖于众所周知的和广泛使用的Linux虚拟服务器(IPVS)内核模块提供第四层负载均衡。另一方面,高可用性是通过VRRP协议实现。

Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。

二、Keepalived工作原理

Layer3,4&7工作在IP/TCP协议栈的IP层,TCP层,及应用层,原理分别如下:

  • Layer3:Keepalived使用Layer3的方式工作式时,Keepalived会定期向服务器群中的服务器发送一个ICMP的数据包(既我们平时用的Ping程序),如果发现某台服务的IP地址没有激活,Keepalived便报告这台服务器失效,并将它从服务器群中剔除,这种情况的典型例子是某台服务器被非法关机。Layer3的方式是以服务器的IP地址是否有效作为服务器工作正常与否的标准。
  • Layer4:如果您理解了Layer3的方式,Layer4就容易了。Layer4主要以TCP端口的状态来决定服务器工作正常与否。如web server的服务端口一般是80,如果Keepalived检测到80端口没有启动,则Keepalived将把这台服务器从服务器群中剔除。
  • Layer7:Layer7就是工作在具体的应用层了,比Layer3,Layer4要复杂一点,在网络上占用的带宽也要大一些。Keepalived将根据用户的设定检查服务器程序的运行是否正常,如果与用户的设定不相符,则Keepalived将把服务器从服务器群中剔除。

三、Keepalived + LVS实现Web的高可用

系统环境

CentOS5.8 x86_64

Director

Director1    172.16.1.101

Director2    172.16.1.105

RealServer

node1.network.com    node1    172.16.1.103

node2.network.com    node2    172.16.1.104

软件包

  • ipvsadm-1.24-13.el5.x86_64.rpm
  • keepalived-1.2.1-5.el5.x86_64.rpm
  • httpd-2.2.15-47.el6.centos.1.x86_64.rpm

拓扑图


1、时间同步

[[email protected] ~]# ntpdate s2c.time.edu.cn
[[email protected] ~]# ntpdate s2c.time.edu.cn
[[email protected] ~]# ntpdate s2c.time.edu.cn
[[email protected] ~]# ntpdate s2c.time.edu.cn

可根据需要在每个节点上定义crontab任务
[[email protected] ~]# which ntpdate
/sbin/ntpdate
[[email protected] ~]# echo "*/5 * * * * /sbin/ntpdate s2c.time.edu.cn &> /dev/null" >> /var/spool/cron/root 
[[email protected] ~]# crontab -l
*/5 * * * * /sbin/ntpdate s2c.time.edu.cn &> /dev/null

2、主机名称要与uname -n保持一致,并通过/etc/hosts解析

Director1
[[email protected] ~]# hostname Director1
[[email protected] ~]# uname -n
Director1
[[email protected] ~]# sed -i ‘[email protected]\(HOSTNAME=\).*@\[email protected]‘  /etc/sysconfig/network

Director2
[[email protected] ~]# hostname Director2
[[email protected] ~]# uname -n
Director2
[[email protected] ~]# sed -i ‘[email protected]\(HOSTNAME=\).*@\[email protected]‘  /etc/sysconfig/network

node1
[[email protected] ~]# hostname node1.network.com
[[email protected] ~]# uname -n
node3.network.com
[[email protected] ~]# sed -i ‘[email protected]\(HOSTNAME=\).*@\[email protected]‘  /etc/sysconfig/network

node2
[[email protected] ~]# hostname node2.network.com
[[email protected] ~]# uname -n
node2.network.com
[[email protected] ~]# sed -i ‘[email protected]\(HOSTNAME=\).*@\[email protected]‘  /etc/sysconfig/network

node1添加hosts解析
[[email protected] ~]# vim /etc/hosts
[[email protected] ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1		CentOS5.8 CentOS5 localhost.localdomain localhost
::1		localhost6.localdomain6 localhost6
172.16.1.101	Director1
172.16.1.105	Director2
172.16.1.103	node1.network.com 	node1
172.16.1.104	node2.network.com 	node2

拷贝此hosts文件至Director2
[[email protected] ~]# scp /etc/hosts Director2:/etc/
The authenticity of host ‘director2 (172.16.1.105)‘ can‘t be established.
RSA key fingerprint is 13:42:92:7b:ff:61:d8:f3:7c:97:5f:22:f6:71:b3:24.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘director2‘ (RSA) to the list of known hosts.
hosts                                               100%  328     0.3KB/s   00:00    

拷贝此hosts文件至node1
[[email protected] ~]# scp /etc/hosts node1:/etc/
The authenticity of host ‘node1 (172.16.1.103)‘ can‘t be established.
RSA key fingerprint is 1e:87:cd:f0:95:ff:a8:ef:19:bc:c6:e7:0a:87:6b:fa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘node1,172.16.1.103‘ (RSA) to the list of known hosts.
[email protected]‘s password: 
hosts                                            100%  328     0.3KB/s   00:00

拷贝此hosts文件至node2
[[email protected] ~]# scp /etc/hosts node2:/etc/
The authenticity of host ‘node2 (172.16.1.104)‘ can‘t be established.
RSA key fingerprint is 1e:87:cd:f0:95:ff:a8:ef:19:bc:c6:e7:0a:87:6b:fa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘node2,172.16.1.104‘ (RSA) to the list of known hosts.
[email protected]‘s password: 
hosts                                                     100%  328     0.3KB/s   00:00

3、关闭iptables和selinux

Director1
[[email protected] ~]# service iptables stop
[[email protected] ~]# vim /etc/sysconfig/selinux 
[[email protected] ~]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#	enforcing - SELinux security policy is enforced.
#	permissive - SELinux prints warnings instead of enforcing.
#	disabled - SELinux is fully disabled.
#SELINUX=permissive
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#	targeted - Only targeted network daemons are protected.
#	strict - Full SELinux protection.
SELINUXTYPE=targeted

Director2
[[email protected] ~]# service iptables stop
[[email protected] ~]# vim /etc/sysconfig/selinux 
[[email protected] ~]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#	enforcing - SELinux security policy is enforced.
#	permissive - SELinux prints warnings instead of enforcing.
#	disabled - SELinux is fully disabled.
#SELINUX=permissive
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#	targeted - Only targeted network daemons are protected.
#	strict - Full SELinux protection.
SELINUXTYPE=targeted

node1
[[email protected] ~]# service iptables stop
[[email protected] ~]# vim /etc/sysconfig/selinux 
[[email protected] ~]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#	enforcing - SELinux security policy is enforced.
#	permissive - SELinux prints warnings instead of enforcing.
#	disabled - SELinux is fully disabled.
#SELINUX=permissive
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#	targeted - Only targeted network daemons are protected.
#	strict - Full SELinux protection.
SELINUXTYPE=targeted

node2
[[email protected] ~]# service iptables stop
[[email protected] ~]# vim /etc/sysconfig/selinux 
[[email protected] ~]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#	enforcing - SELinux security policy is enforced.
#	permissive - SELinux prints warnings instead of enforcing.
#	disabled - SELinux is fully disabled.
#SELINUX=permissive
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#	targeted - Only targeted network daemons are protected.
#	strict - Full SELinux protection.
SELINUXTYPE=targeted
时间: 2024-08-10 21:30:13

Linux HA集群之Keepalived的相关文章

HA集群之keepalived详解/基于keepalived+LVS-DR构建HA主备模型(一)

一.理论部分:     keepalived是vrrp协议的实现:原生设计目的为高可用ipvs服务:keepalived能够配置文件中的定义生成ipvs规则:并能够对各RealServer的健康状态进行检测:  vrrp协议:虚拟冗余路由协议:早期只是主要在路由器上提供的一种非常简单的完成将多个物理设备组建成一个虚拟设备,并且在多个物理设备之间漂移地址一种协议:非常轻量化,性能非常好.而keepalived无非就是通过vrrp协议在Linux主机上通过一个守护进程,把Linux主机扮演成路由器,

Linux HA集群

HA(High Availability,高可用)集群的主要目的是提高服务的在线率,也就是缩短平均无故障的时间.实际上就是当一个提供服务的服务节点不在线时,有另一个提供相同服务的节点可以继续提供服务,避免出现单点故障. HA集群相关概念 一.相关名词解释 1.在线率 在线率是衡量HA集群的一个重要指标,就算方式如下: 在线率=平均无故障时间/(平均修复时间 + 平均无故障时间) 所以提高系统的可用性: 1).增加平均无故障时间 2).缩短平均修复时间 (可以通过冗余机制实现) 2.资源 这里的资

HA 集群软件 keepalived 详解3

在前两篇文章中我们队keepalived做了相关介绍,这里环境还是和之前保持的一样,这次我们主要介绍vrrp_script模块,在上次介绍keepalived基础HA功能时用到了vrrp_script这个模块,此模块专门用于对集群中服务自己进行监控,与此模块一起使用的还有track_script模块,在此模块中可以引用监控脚本,命令组合,shell语句等,以实现对服务端口多方面的监控,track_script模块主要用来调用vrrp_script模块使keepalived执行对集群服务资源的检查

Linux HA集群之Corosync + Pacemaker + DRBD + MySQL实现MySQL高可用

大纲 一.系统环境及所需软件包 二.高可用环境准备工作 三.DRBD的安装与基本配置 四.Corosync的安装与基本配置 五.基于crm配置资源 一.系统环境及所需软件包 系统环境 CentOS5.8 x86_64 node1.network.com    node1    172.16.1.101 node2.network.com    node2    172.16.1.105 软件包 corosync-1.2.7-1.1.el5.x86_64.rpm pacemaker-1.0.12-

Linux HA集群之 Corosync + Pacemaker + drbd + MySQL

大纲 一.什么是DRBD 二.DRBD工作原理 三.DRBD相关概念 四.DRBD配置过程 五.Corosync + Pacemaker + DRBD + MySQL实现 一.什么是DRBD Distributed Replicated Block Device(DRBD)是一个基于软件的.无共享.复制的存储解决方案,在服务器之间的对块设备(硬盘.分区.逻辑卷等)进行镜像. DRBD镜像数据 实时性:当应用对磁盘的数据进行修改时,复制立即发生 透明性:应用程序的数据存储在镜像设备上是独立和透明的

Linux HA集群之Heartbeat

大纲 一.什么是高可用集群 二.什么是高可用性 三.高可用集群相关概念 四.Heartbeat v1版示例 五.Heartbeat v2版示例 六.Heartbeat v3版示例 一.什么是高可用集群 高可用集群,英文原文为High Availability Cluster,简称HA Cluster,简单的说,集群(cluster)就是一组计算机,它们作为一个整体向用户提供一组网络资源.这些单个的计算机系统 就是集群的节点(node).高可用性集群(HA cluster)是指如单系统一样地运行并

Linux HA集群之Corosync基础概念

大纲 一.什么是AIS和OpenAIS 二.什么是corosync 三.corosync安装与配置 一.什么是AIS和OpenAIS 应用接口规范AIS是用来定义应用程序接口API的开放性规范的集合这些应用程序作为中间件为应用服务提供一种开放.高移植性的程序接口.是在实现高可用应用过程中是亟需的.服务可用性论坛SA Forum是一个开放性论坛它开发并发布这些免费规范.使用AIS规范的应用程序接口API可以减少应用程序的复杂性和缩短应用程序的开发时间这些规范的主要目的就是为了提高中间组件可移植性和

架构之高可用性(HA)集群(Keepalived)

Keepalived简介 Keepalived是Linux下一个轻量级别的高可用解决方案.高可用(High Avalilability,HA),其实两种不同的含义:广义来讲,是指整个系统的高可用行,狭义的来讲就是之主机的冗余和接管, 它与HeartBeat RoseHA 实现相同类似的功能,都可以实现服务或者网络的高可用,但是又有差别,HeartBeat是一个专业的.功能完善的高可用软件,它提供了HA 软件所需的基本功能,比如:心跳检测.资源接管,检测集群中的服务,在集群节点转移共享IP地址的所

Linux集群(keepalived介绍,Keepalived配置高可用集群,Keepa+mysql

一.Linux集群概述 根据功能划分为两大类:高可用和负载均衡 (1)高可用集群通常为两台服务器,台工作,另外一台作为准备,当提供服务的机器宕机,另外一台将接替继续提供服务. 实现高可用的开源软件有:heartbeat,keepalived (2)负载均衡集群:需要有一台服务器作为分发器,它负责吧用户的请求分发给后端的服务器处理,在这个集群里,除了分发器外,就是给用户提供服务的服务器了,这些服务器数量最少为2 实现负载均衡的开源软件有LVS,keepalived,haproxy,nginx,商业