- keepalived 的原理请自行百度,这里利用其ip漂移功能,实现 主从web的高可用。
ip漂移指一个虚拟ip,在2个真实ip(主从机)之间漂移,外部访问这个虚拟ip。
达到的效果:主从机的web都开启服务。主机web宕机,从机web开始接管服务。等待主机开始服务后,主机开始对外服务,从机待命。
- 安装。
apt-get install keepalived或其他方式安装
- 配置
主机:
xlj@ubuntu:~$ cat /etc/keepalived/keepalived.conf global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 4 router_id LVS_DEVEL } vrrp_script chk_nc { script "/home/xlj/check.sh" interval 6 weight -20 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.111.111 } track_script { chk_nc } #notify_master /home/yonyou/nchome0509xin/keepalive/start.sh #notify_backup /home/yonyou/nchome0509xin/keepalive/stop.sh }
从机:
xlj@ubuntu:~$ cat /etc/keepalived/keepalived.conf global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 4 router_id LVS_DEVEL } vrrp_script chk_nc { script "/home/xlj/check.sh" interval 6 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.111.111 } track_script { chk_nc } #notify_master /home/yonyou/nchome0509xin/keepalive/start.sh #notify_backup /home/yonyou/nchome0509xin/keepalive/stop.sh }
配置提示:
- state为 MASTER或BACKUP
- virtual_router_id 2机要相同
- authentication 为防止非法加入要求的授权,2机要相同。
- virtual_ipaddress 为要虚拟的ip,2机相同。
- track_script 是健康检查脚本配置,本例是调用check.sh判断。
- 其他参数可以百度
其中,健康检查脚本主从机都相同:
xlj@ubuntu:~$ cat /home/xlj/check.sh #!/bin/bash COUNT=1 COUNT=`ps -ef | grep java | grep -v grep | wc -l` if [ $COUNT -gt 0 ] ; then exit 0 else exit 1 fi xlj@ubuntu:~$
意思是检查java进程是否存在,存在则返回0,代表ok。
- 启动 keepalived服务
主从机分别启动服务:service keepalived restart
- 查看keepalived日志。默认日志在 /var/log/syslog:
xlj@ubuntu:~$ tail -f /var/log/syslog Jun 17 16:26:08 ubuntu Keepalived_vrrp[9306]: Registering gratuitous ARP shared channel Jun 17 16:26:08 ubuntu Keepalived_vrrp[9306]: Opening file '/etc/keepalived/keepalived.conf'. Jun 17 16:26:08 ubuntu Keepalived_vrrp[9306]: Configuration is using : 69085 Bytes Jun 17 16:26:08 ubuntu Keepalived_healthcheckers[9305]: Registering Kernel netlink command channel Jun 17 16:26:08 ubuntu Keepalived_healthcheckers[9305]: Opening file '/etc/keepalived/keepalived.conf'. Jun 17 16:26:08 ubuntu Keepalived_healthcheckers[9305]: Configuration is using : 11714 Bytes Jun 17 16:26:08 ubuntu Keepalived_healthcheckers[9305]: Using LinkWatch kernel netlink reflector... Jun 17 16:26:08 ubuntu Keepalived_vrrp[9306]: Using LinkWatch kernel netlink reflector... Jun 17 16:26:08 ubuntu Keepalived_vrrp[9306]: VRRP_Instance(VI_1) Entering BACKUP STATE Jun 17 16:26:08 ubuntu Keepalived_vrrp[9306]: VRRP_Script(chk_nc) succeeded
- 然后就可以 实验了,反复启停主从机的web服务,观察了。
注意,访问的是虚拟ip:
http://192.168.111.111:8080/
- 提示:
web页面看不出到底访问的是主机还是从机,建议你的web应用提供一个页面针对主机还是从机显示不同的信息。