文章目录
- 一、Keepalived 双击热备基础知识
-
- 1.1 Keepalived 案例分析
- 1.2 Keepalived 工具介绍
- 1.3 Keepalived 实现原理剖析
- 二、Keepalived 的部署
-
- 2.1 Keepalived 案例讲解
- 2.2 Keepalived 安装与启动
- 2.3 配置 Keepalived master 服务器
- 2.4 配置 Keepalived slave 服务器
- 三、实验
-
- 3.1 实验准备
- 3.2 步骤
-
- 3.2.1 配置主调度器(192168.100.21)
- 3.2.2 配置辅调度器(192.168.100.25)
- 3.2.3 配置存储服务器(192.168.100.24)
- 3.2.4 配置节点服务器(192.168.100.22)
- 3.2.5 配置节点服务器(192.168.100.23)
- 3.3测试
一、Keepalived 双击热备基础知识
1.1 Keepalived 案例分析
- 企业应用中,单台服务器承担应用存在单点故障的危险
- 单点故障一旦发生,企业服务将发生中断,造成极大的危害
1.2 Keepalived 工具介绍
专为 LVS 和 HA 设计的一款健康检查工具
- 支持故障自动切换(Failover)
- 支持节点健康状态检查(Health Checking)
- 官方网站:http://www.keepalived.org/
1.3 Keepalived 实现原理剖析
Keepalived 采用 VRRP 热备份协议
- 实现 Linux 服务器的多机热备功能
VRRP(虚拟路由冗余协议)是针对路由器的一种备份解决方案
- 由多台路由器组成一个热备组,通过共用的虚拟 IP 地址对外提供服务
- 每个热备组内同时只有一台主路由器提供服务,其他路由器处于冗余状态
- 若当前在线的路由器失败,则其他路由器会根据设置的优先级自动接替虚拟 IP 地址,继续提供服务
二、Keepalived 的部署
2.1 Keepalived 案例讲解
- Keepalived 可实现多机热备,每个热备组有多台服务器
- 双击热备的故障切换是由虚拟 IP 地址的漂移来实现的,适用于各种应用服务器
- 实现基于 Web 服务的双机热备
2.2 Keepalived 安装与启动
- 在 LVS 群集环境中应用时,也需用到 ipvsadm 管理工具
- YUM 安装 Keepalived
- 启用 Keepalived 服务
2.3 配置 Keepalived master 服务器
- Keepalived 配置目录位于/etc/keepalived/
- keepalived.conf 是主配置文件:
◆ global_defs {…} 区段指定全局参数
◆ vrrp_instance 实例名称 {…} 区段指定 VRRP 热备参数
◆ 注释文字以 “!” 符号开头
◆ 目录 samples ,提供了许多配置样例作为参考 - 常用配置选项:
◆ router_id HA_TEST_R1:本路由器(服务器)的名称
◆ vrrp_instance VI_1:定义 VRRP 热备实例
◆ state MASTER :热备状态,MASTER 表示主服务器
◆ interface ens33:承载 VIP 地址的物理接口
◆ virtual_router_ip 1:虚拟路由器的 ID 号,每个热备组保持一致
◆ priority 100:优先级,数值越大优先级越高
◆ advert_int 1:通告间隔秒数(心跳频率)
◆ auth_type PASS:认证类型
◆ auth_pass 123456:密码子串
◆ virtual_ipaddress {vip}:指定漂移地址(VIP),可以有多个
2.4 配置 Keepalived slave 服务器
- Keepalived 备份服务器的配置与 master 的配置有三个选项不同
◆ router_id:设为自有名称
◆ state:设为 BACKUP
◆ priority:值低于主服务器 - 其它选项与 master 相同
三、实验
3.1 实验准备
IP地址规划:
漂移地址(VIP):192.168.100.100
主调度器:192.168.100.21
辅调度器:192.168.100.25
WEB服务器1:192.168.100.22
WEB服务器2:192.168.100.23
存储服务器:192.168.100.24
3.2 步骤
3.2.1 配置主调度器(192168.100.21)
'【1】调整/proc响应参数'
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost network-scripts]# sysctl -p ###生效
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
'【2】清除负载分配策略'
[root@localhost /]# ipvsadm -C
'【3】调整keepalived参数'
[root@localhost ~]# yum -y install keepalived ipvsadm
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vi keepalived.conf
global_defs {
router_id HA_TEST_R1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.100.100
}
}
virtual_server 192.168.100.100 80 {
delay_loop 15
lb_algo rr
lb_kind DR
persistence 60
protocol TCP
real_server 192.168.100.22 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.100.23 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
[root@localhost keepalived]# systemctl start keepalived ####启动keepalived
[root@localhost keepalived]# systemctl enable keepalived ####开机启动keepalived
[root@localhost keepalived]# ip addr show dev ens33 ####查看主控制IP地址和漂移地址
3.2.2 配置辅调度器(192.168.100.25)
'【1】调整/proc响应参数'
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost network-scripts]# sysctl -p ###生效
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
'【2】清除负载分配策略'
[root@localhost /]# ipvsadm -C
'【3】调整keepalived参数'
[root@localhost ~]# yum -y install keepalived ipvsadm
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vi keepalived.conf
global_defs {
router_id HA_TEST_R2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.100.100
}
}
virtual_server 192.168.100.100 80 {
delay_loop 15
lb_algo rr
lb_kind DR
persistence 60
protocol TCP
real_server 192.168.100.22 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.100.23 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
[root@localhost keepalived]# systemctl start keepalived ####启动keepalived
[root@localhost keepalived]# systemctl enable keepalived ####开机启动keepalived
[root@localhost keepalived]# ip addr show dev ens33 ####查看主控制IP地址和漂移地址
3.2.3 配置存储服务器(192.168.100.24)
rpm -q nfs-utils ###如果没装,yum -y install nfs-utils
rpm -q rpcbind ###如果没装,yum -y install rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# vi /etc/exports
/opt/51xit 192.168.100.0/24 (rw,sync)
/opt/52xit 192.168.100.0/24 (rw,sync)
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# mkdir /opt/51xit /opt/52xit
[root@localhost ~]# echo "this is 51xit" >/opt/51xit/index.html
[root@localhost ~]# echo "this is 52xit" >/opt/52xit/index.html
3.2.4 配置节点服务器(192.168.100.22)
'【1】配置虚拟IP地址'
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ifconfig
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.100 dev lo:0
[root@localhost network-scripts]# route add -host 192.168.100.100 dev lo:0
'【2】调整/proc响应参数'
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@localhost network-scripts]# sysctl -p
'【3】安装httpd 挂载测试页'
[root@localhost ~]# showmount -e 192.168.100.24 ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.44:
/opt/51xit (everyone)
/opt/52xit (everyone)
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mount 192.168.100.24:/opt/51xit /var/www/html/
[root@localhost ~]# vi /etc/fstab
192.168.100.24:/opt/51xit/ /var/www/html/ nfs rw,tcp,intr 0 1 ###开机自动挂载,注意格式对齐
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
3.2.5 配置节点服务器(192.168.100.23)
同 192.168.100.22 的配置,可以参考 3.2.4
3.3测试
在网页输入如下:
192.168.100.22
在网页输入如下:
192.168.100.23
在网页输入如下:
192.168.100.100
打开抓包工具,会发现192.168.100.21主调度器,一直在发VRRP报文
打开抓包工具,会发现192.168.100.25从调度器,一直在发VRRP报文