keepalived
简介
Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到, 并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived 自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
vrrp协议
vrrp作用
vrrp协议的软件实现,原生设计主的目的为了高可用的ipvs服务。
-
于vrrp协议完成地址流动
-
为了集群内的所有节点生成ipvs规则
-
ipvs集群的各RS做健康监测
-
于脚本调用接口通过执行脚本完成脚本中定义的功能,进而影响集群事物
组件
核心组件:
-
vrrp stack:vrrp协议的实现;
-
ipvs wrapper:为了集群内的所有节点生成ipvs规则等功能;
-
checkers:为ipvs集群的各RS做健康状态监测;
控制组件:
-
实现配置文件的分析和加载;
-
IO复用器;
-
内存管理组件;
配置文件主要配置核心组件。
配置
HA Cluster的配置前提:
- 各节点时间必须同步;
- 确保iptables及selinux不会成为阻碍;(如果了解规则,可适当添加规则,较为复杂)
- 各节点之间可通过主机名互相通信;
- 确保各节点用于集群服务的接口支持MULTICAST通信;
- 实验环境
- 一台服务器
- 客户端数台
- 时间同步
vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
server 172.16.0.1 iburst
默认server是指向网上,这里把它注释掉了,我添加了一条直接指向了服务器。
systemctl chrony.service restart
重启服务生效
chronyc sources
查看时间源同步差距
date
查看时间是否正确
- 确保iptables及selinux不会成为阻碍
systemctl status firewalld.service
查看firewalld.service服务是否停止
getenforce
查看是否关闭(Disabled)
如果是enforcing,就需要修改/etc/selinux/config文件。
# 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 - No SELinux policy is loaded.
SELINUX=Disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
- 各节点之间可通过主机名互相通信
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 centos7.waz.com
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.40.184 node1.wanganzhi.com node1
192.168.40.185 node2.wanganzhi.com node2
192.168.40.186 node3.wanganzhi.com node3
192.168.40.186 node4.wanganzhi.com node4
- 确保各节点用于集群服务的接口支持MULTICAST通信
ifconfig
查看网卡是否支持MULTICAST
安装配置
- keepalived安装配置
CentOS 6.4以后 直接base仓库就可以提供,yum源配光盘或源仓库可直接安装
yum -y install keepalived
主配置文件:/etc/keepalived/keepalived.conf
主程序文件:/usr/sbin/keepalived
Unit File:keepalived.service
Unit File的环境配置文件:/etc/sysconfig/keepalived
-
配置文件配置段
TOP HIERACHY GLOBALCONFIGURATION Globaldefinitions Staticroutes/addresses VRRPD CONFIGURATION VRRPsynchronizationgroup(s):vrrp同步组; VRRPinstance(s):每个vrrpinstance即一个vrrp路由器; LVS CONFIGURATION Virtualservergroup(s) Virtualserver(s):ipvs集群的vs和rs;
-
单主配置示例:
cd /etc/keepalived/
vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_fromkeepalived@localhost
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 eno33
virtual_router_id 14
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 571f97b2
}
virtual_ipaddress {
10.1.0.91/16 dev eno33
}
}
配置完毕后,启动Keepalived服务
/etc/init.d/keepalived start
检查配置结果,查看是否有虚拟ip xx.x.x.xx
ip addr |grep xx.x.x.xx
如果出现ip,就表示Keepalived服务单主配置成功
网友评论