keepalived用于实现高可用集群
-
它的工作原理就是VRRP(虚拟冗余路由协议)
配置高可用的web集群
环境说明:
web1:eth0->192.168.88.100/24
web2:eth0->192.168.88.200/24
配置keepalived
# 在两台web服务器上安装keepalived[root@web1 ~]# yum install -y keepalived httpd[root@web2 ~]# yum install -y keepalived httpd# 修改配置文件[root@web1 ~]# vim /etc/keepalived/keepalived.conf 12 router_id web1 # 设置本机在集群中的唯一识别符 13 vrrp_iptables # 自动配置iptables放行规则 ... ... 20 vrrp_instance VI_1 { 21 state MASTER # 状态,主为MASTER,备为BACKUP 22 interface eth0 # 网卡 23 virtual_router_id 51 # 虚拟路由器地址 24 priority 100 # 优先级 25 advert_int 1 # 发送心跳消息的间隔 26 authentication { 27 auth_type PASS # 认证类型为共享密码 28 auth_pass 1111 # 集群中的机器密码相同,才能成为集群 29 } 30 virtual_ipaddress { 31 192.168.88.80/24 # VIP地址 32 } 33 }# 删除下面所有行# 打开一个新的终端监控日志,新日志将出持续显示在屏幕上。退出按ctrl+c[root@web1 ~]# tail -f /var/log/messages | grep -i keepalived# 在前一个终端启动服务[root@web1 ~]# systemctl start keepalived# 等几秒服务完全启动后,可以查看到vip[root@web1 ~]# ip a s eth0 # eth0将会增加额外的88.80地址# 配置web2[root@web1 ~]# scp /etc/keepalived/keepalived.conf 192.168.88.200:/etc/keepalived/[root@web2 ~]# vim /etc/keepalived/keepalived.conf 12 router_id web2 # 改id 13 vrrp_iptables ... ... 20 vrrp_instance VI_1 { 21 state BACKUP # 改状态 22 interface eth0 23 virtual_router_id 51 24 priority 80 # 改优先级 25 advert_int 1 26 authentication { 27 auth_type PASS 28 auth_pass 1111 29 } 30 virtual_ipaddress { 31 192.168.88.80/24 32 } 33 }# 启动服务[root@web2 ~]# systemctl start keepalived# 查看地址,eth0不会出现vip[root@web2 ~]# ip a s eth0# 测试,现在访问88.80,看到是web1上的内容[root@client1 ~]# curl http://192.168.88.80/192.168.99.100# 在web2上监控日志[root@web2 ~]# tail -f /var/log/messages | grep -i keepalived# 关闭web1上的keepalived,观察web2的日志输出[root@web1 ~]# systemctl stop keepalived.service # 测试,现在访问88.80,看到是web2上的内容[root@client1 ~]# curl http://192.168.88.80/apache web server2# 在web2上查看vip,可以查看到vip 192.168.88.80[root@web2 ~]# ip a s eth0