分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
综述:
浏览器访问虚拟IP: 192.168.1.57, 该虚拟IP被Keepalived接管,两个Keepalived进程分别运行在物理IP为192.168.1.56和192.168.1.59服务器上,这两个服务器上都运行着Nginx server。Nginx server都监听虚拟IP 192.168.1.57. Nginx背后有一些web app集群,已经配置成upstream.
为了防止Nginx自己成为单点瓶颈,这里采用了双Nginx server的方式。每个Server都是Ubuntu 12.04.
假定我的第一台Ubuntu server物理IP地址是192.168.1.56,已经安装了Nginx server,现在安装Keepalived,后面称这台为master server.
apt-get install keepalived
另外一台Ubuntu server物理IP是192.168.1.59, 也安装Nginx server和Keapalived. 后面称这台为backup server. Nginx的安装以及配置不是本文关注内容,请参考我的其他文章。
现在开始配置master server. 安装完成keepalived后,通过查看脚本/etc/init.d/keepalived里面发现,启动配置文件的路径是
CONFIG=/etc/keepalived/keepalived.conf
但是该文件现在还不存在。所以我创建一个,内容如下:
# Settings for notifications global_defs { notification_email { csfreebird@gmail.com # Email address for notifications } notification_email_from keepalived@your_company.com # The from address for the notifications smtp_server 127.0.0.1 smtp_connect_timeout 15}# Define the script used to check if haproxy is still working vrrp_script chk_http_port { script "/etc/keepalived/check_nginx.sh" # check Nginx is alive or not interval 2 # weight 2}# Configuation for the virtual interface vrrp_instance VI_1 { interface eth0 state MASTER # set this to BACKUP on the other machine priority 101 # set this to 100 on the other machine virtual_router_id 51 smtp_alert # Activate email notifications authentication { auth_type PASS auth_pass 1111 # Set this to some secret phrase } # The virtual ip address shared between the two loadbalancers virtual_ipaddress { 192.168.1.57 } # Use the script above to check if we should fail over track_script { chk_http_port }}
说明:
1. smtp_server必须要用127.0.0.1
2. 自己要创建一个检查nginx进程的脚本
/etc/keepalived/check_nginx.sh
内容如下:
!/bin/bash# try to start nginx if nginx process is dead # shutdonw keepalived process if start nginx failed pid=`ps -C nginx --no-header |wc -l`if [ $pid -eq 0 ];then service nginx start sleep 3 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then service keepalived stop fifi
ping 192.168.1.57PING 192.168.1.57 (192.168.1.57) 56(84) bytes of data.64 bytes from 192.168.1.57: icmp_req=1 ttl=64 time=0.024 ms64 bytes from 192.168.1.57: icmp_req=2 ttl=64 time=0.020 ms
对192.168.1.59做相同的配置,略有变化的是:
vrrp_instance VI_1 { interface eth0 state BACKUP // changed priority 100 // changed
通过轮流关闭master和backup,证明keepalived已经有效工作了。
参考资料:Keepalived官方文档真是够差的。
http://blog.csdn.net/zmj_88888888/article/details/8825471