keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。Keepalived是自动完成,不需人工干涉。
基于vrrp协议完成地址流动
为vip地址所在的节点生成ipvs规则(在配置文件中预先定义)
为ipvs集群的各RS做健康状态检测
基于脚本调用接口完成脚本中定义的功能,进而影响集群事务,以此支持nginx、haproxy等服务
用户空间核心组件:
vrrp stack:VIP消息通告
checkers:监测real server
system call:实现 vrrp 协议状态转换时调用脚本的功能
SMTP:邮件组件
IPVS wrapper:生成IPVS规则
Netlink Reflector:网络接口
WatchDog:监控进程
控制组件:提供keepalived.conf 的解析器,完成Keepalived配置
IO复用器:针对网络目的而优化的自己的线程抽象
内存管理组件:为某些通用的内存管理功能(例如分配,重新分配,发布等)提供访问权限
各节点时间必须同步:ntp, chrony
关闭防火墙及SELinux
各节点之间可通过主机名互相通信:非必须
建议使用/etc/hosts文件实现:非必须
各节点之间的root用户可以基于密钥认证的ssh服务完成互相通信:非必须
设备名 | IP | VIP | 用途 |
ka1 | 172.25.254.10 | 172.25.254.100 | ka1 |
ka2 | 172.25.254.20 | 172.25.254.100 | ka2 |
rs1 | 172.25.254.110 | 172.25.254.100 | realserver |
rs2 | 172.25.254.120 | 172.25.254.100 | realserver |
cilent | 172.25.254.66 | 测试 |
systemctl disabled --now firewalld vim /etc/selinux/config
#realserver上安装apache yum install httpd -y #修改访问页面 echo server1 -172.25.254.110 > /etc/www/html/index.html echo server2 -172.25.254.120 > /etc/www/html/index.html #测试httpd服务是否正常 curl 172.25.124.110 curl 172.25.124.120 #ka上安装keepalived yum install keepalived -y vim /etc/keepalived/keepalived.conf #修改配置文件 systemctl enable --now keepalived.service
vim /etc/sysconfig/keepalived
vim /etc/rsyslog.conf systemctl restart keepalived.service systemctl restart rsyslog.service
mkdir -p /etc/keepalived/conf.d vim /etc/keepalived/keepalived.conf #将虚拟路由复制并注释,写明路径 vim /etc/keepalived/conf.d/172.25.254.100.conf #粘贴之前复制的内容
首先打开QQ邮件 ->点击账号与安全 ->点击安全设置 ->生成授权码
vim /etc/keepalived/mail.sh vim /etc/mail.rc echo hello | mail -s test XXXXXXXXXXXX@qq.com #测试是否能收到邮件
#ka上配置 yum install ipvsadm -y vim /etc/keepalived/keepalived.conf systemctl restart keepalived.service systemctl start ipvsadm.service ipvsadm -A -t 172.25.254.100:80 -s wrr ipvsadm -a -t 172.25.254.100:80 -r 172.25.254.110:80 -g -w 2 ipvsadm -a -t 172.25.254.100:80 -r 172.25.254.120:80 -g -w 1 systemctl stop ipvsadm.service
rs上配置
ip a a 172.25.254.100/32 dev lo #临时配置,重启后需重新配置
注意:此实验与上一个实验是独立的,ipvsadm和haproxy不能同时启动,之前keepalived配置文件中做的配置需要删除或注释
ka上配置 yum install haproxy -y systemctl restart haproxy vim /etc/keepalived/test.sh chmod +x /etc/keepalived/test.sh vim /etc/keepalived/keepalived.conf systemctl restart keepalived.service