理论+实验——LVS+Keepalived 高可用群集

一、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响应参数'
[[email protected] 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
[[email protected] 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】清除负载分配策略'
[[email protected] /]# ipvsadm -C
'【3】调整keepalived参数'
[[email protected] ~]# yum -y install keepalived ipvsadm
[[email protected] ~]# cd /etc/keepalived/
[[email protected] keepalived]# cp keepalived.conf keepalived.conf.bak
[[email protected] 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
	}
    }
}
[[email protected] keepalived]# systemctl start keepalived                  ####启动keepalived
[[email protected] keepalived]# systemctl enable keepalived                 ####开机启动keepalived
[[email protected] keepalived]# ip addr show dev ens33                         ####查看主控制IP地址和漂移地址

在这里插入图片描述

3.2.2 配置辅调度器(192.168.100.25)

'【1】调整/proc响应参数'
[[email protected] 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
[[email protected] 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】清除负载分配策略'
[[email protected] /]# ipvsadm -C
'【3】调整keepalived参数'
[[email protected] ~]# yum -y install keepalived ipvsadm
[[email protected] ~]# cd /etc/keepalived/
[[email protected] keepalived]# cp keepalived.conf keepalived.conf.bak
[[email protected] 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
	}
    }
}
[[email protected] keepalived]# systemctl start keepalived                  ####启动keepalived
[[email protected] keepalived]# systemctl enable keepalived                 ####开机启动keepalived
[[email protected] 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
[[email protected] ~]# systemctl start nfs
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# vi /etc/exports
/opt/51xit 192.168.100.0/24 (rw,sync)
/opt/52xit 192.168.100.0/24 (rw,sync)
[[email protected] ~]# systemctl restart nfs
[[email protected] ~]# systemctl restart rpcbind
[[email protected] ~]# systemctl enable nfs
[[email protected] ~]# systemctl enable rpcbind
[[email protected] ~]# mkdir /opt/51xit /opt/52xit
[[email protected] ~]# echo "this is 51xit" >/opt/51xit/index.html
[[email protected] ~]# echo "this is 52xit" >/opt/52xit/index.html

3.2.4 配置节点服务器(192.168.100.22)

'【1】配置虚拟IP地址'
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-lo ifcfg-lo:0
[[email protected] network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.255
ONBOOT=yes
[[email protected] network-scripts]# ifup lo:0
[[email protected] network-scripts]# ifconfig

在这里插入图片描述

[[email protected] network-scripts]# vi /etc/rc.local 
/sbin/route add -host 192.168.100.100 dev lo:0
[[email protected] network-scripts]# route add -host 192.168.100.100 dev lo:0
'【2】调整/proc响应参数'
[[email protected] 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
[[email protected] network-scripts]# sysctl -p
'【3】安装httpd 挂载测试页'
[[email protected] ~]# showmount -e 192.168.100.24                   ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.44:
/opt/51xit  (everyone)
/opt/52xit (everyone)
[[email protected] ~]# yum -y install httpd
[[email protected] ~]# mount 192.168.100.24:/opt/51xit /var/www/html/
[[email protected] ~]# vi /etc/fstab 
192.168.100.24:/opt/51xit/ /var/www/html/        nfs     rw,tcp,intr     0 1         ###开机自动挂载,注意格式对齐
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# 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报文
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ZG_66/article/details/108749566