准备四台机器
hostname | IP | 说明 |
---|---|---|
lb01 | 10.0.0.30 | keepalived主服务器(nginx主负载均衡) |
lb02 | 10.0.0.60 | Keepalived备服务器(Nginx辅负载均衡) |
www01 | 10.0.0.40 | www01 |
www02 | 10.0.0.50 | www02 |
安装keepalived(lb01,lb02)
[root@lb01 ~]# yum install keepalived -y [root@lb01 ~]# rpm -qa keepalived keepalived-1.3.5-19.el7.x86_64
[root@lb02 ~]# yum install -y keepalived [root@lb02 ~]# rpm -qa keepalived keepalived-1.3.5-19.el7.x86_64
配置主keepalived服务器 lb01-master
1.备份旧配置文件,定义新配置文件 [root@lb01 keepalived]# cp keepalived.conf{,.bak} [root@lb01 keepalived]# ls keepalived.conf keepalived.conf.bak 2.根据自己机器修改配置 vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 }
#定义虚拟IP,也就是VIP,提供给用户的高可用地址,绑定网络接口ens33,别名ens33:1,主备节点要相同 virtual_ipaddress { 192.168.17.100/24 dev ens33 label ens33:1 } } [root@lb01 keepalived]# systemctl restart keepalived [root@lb01 keepalived]# ps -ef |grep keepalived root 1299 1 0 10:38 ? 00:00:00 /usr/sbin/keepalived -D root 1300 1299 0 10:38 ? 00:00:00 /usr/sbin/keepalived -D root 1301 1299 0 10:38 ? 00:00:00 /usr/sbin/keepalived -D root 1303 1200 0 10:38 pts/0 00:00:00 grep --color=auto keepalived ##检查是否生成新的VIP地址192.168.17.100 [root@lb01 keepalived]# ip addr | grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 10.0.0.30/24 brd 10.0.0.255 scope global noprefixroute ens33 inet 192.168.17.100/24 scope global ens33:1 inet6 fe80::c537:24bd:f23f:695e/64 scope link noprefixroute
配置keepalived备服务器lb02-BACKUP
注意lb02和lb01的参数区别
vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 #虚拟路由IP跟主节点一致 priority 100 #优先级备用节点一般比主节点低50 advert_int 1 #MASTER和BACKUP之间的通信检查时间,默认是1秒 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.17.100/24 dev ens33 label ens33:1 } } [root@lb02 keepalived]# systemctl restart keepalived [root@lb02 keepalived]# ps -ef |grep keepalived root 1285 1 0 10:49 ? 00:00:00 /usr/sbin/keepalived -D root 1286 1285 0 10:49 ? 00:00:00 /usr/sbin/keepalived -D root 1287 1285 0 10:49 ? 00:00:00 /usr/sbin/keepalived -D root 1293 1171 0 10:51 pts/0 00:00:00 grep --color=auto keepalived [root@lb02 keepalived]# ip addr | grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 10.0.0.60/24 brd 10.0.0.255 scope global noprefixroute ens33 inet6 fe80::8d10:27e0:1c5b:e2a1/64 scope link tentative noprefixroute dadfailed inet6 fe80::b5c6:7506:252b:95e/64 scope link noprefixroute
检查配置结果,是否出现虚拟IP(VIP)
- 正确的结果应该是没有出现VIP
- 因为lb02位BACKUP,当主节点正常的时候,它不会接管VIP
- 若是lb02看到了VIP,表示配置出错了!
如果出现lb01、lb02抢夺VIP资源,排查故障思路
- 主备服务器是否能够通信,可以ping检测
- 是否没有正确配置防火墙,或是忘记关闭防火墙
- 两台服务器的keepalived.conf是否写错(例如虚拟路由ID是否不一致)
高可用主备服务切换实践
停止主服务器的keepalived或者直接关机
[root@lb01 keepalived]# ip addr | grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 10.0.0.30/24 brd 10.0.0.255 scope global noprefixroute ens33 inet 192.168.17.100/24 scope global ens33:1 inet6 fe80::c537:24bd:f23f:695e/64 scope link noprefixroute [root@lb01 keepalived]# systemctl stop keepalived [root@lb01 keepalived]# ip addr | grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 10.0.0.30/24 brd 10.0.0.255 scope global noprefixroute ens33 inet6 fe80::c537:24bd:f23f:695e/64 scope link noprefixroute 可以看到,关闭keepalived服务后,VIP也消失了
检查BACKUP机器状态
[root@lb02 keepalived]# ip addr | grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 10.0.0.60/24 brd 10.0.0.255 scope global noprefixroute ens33 inet 192.168.17.100/24 scope global ens33:1 inet6 fe80::8d10:27e0:1c5b:e2a1/64 scope link tentative noprefixroute dadfailed inet6 fe80::b5c6:7506:252b:95e/64 scope link noprefixroute
可以看到备节点lb02此时已经接管了VIP,并且这个接管期间,备节点还会主动发送ARP广播,让局域网内所有的客户端更新本地ARP缓存表,以便访问新接管VIP服务的节点。
恢复主节点,查看VIP动态
[root@lb01 keepalived]# systemctl restart keepalived [root@lb01 keepalived]# ip addr | grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 10.0.0.30/24 brd 10.0.0.255 scope global noprefixroute ens33 inet 192.168.17.100/24 scope global ens33:1 inet6 fe80::c537:24bd:f23f:695e/64 scope link noprefixroute
此时会发现,再次启动keepalived主服务,MASTER很快会接管VIP,备服务器正确应该是释放了VIP,此时用户的请求也就再次转移到新机器了。
明确单实例主备模式配置文件区别
区别参数 | MASTER参数 | BACKUP参数 |
---|---|---|
router_id | lb01 | lb02 |
state | MASTER | BACKUP |
priority | 150 | 100 |
标签:00,keepalived,实例,scope,ens33,root,inet,搭建 From: https://www.cnblogs.com/junwured/p/17524448.html