keepalived实现nginx高可用


1.拓扑环境

nginx 主 10.1.60.155

nginx 从 10.1.60.157

2.去官网下载最新版本的keepalived,下载地址:http://www.keepalived.org/download.html

解压:tar zxvf keepalived-1.2.9.tar.gz

进入目录编译安装:

cd keepalived-1.2.9

./configure --prefix=/usr/local/keepalived

make

make install

复制命令到系统命令:

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

添加开机启动:

chkconfig keepalived on

编辑主nginx10.1.60.155keepalived配置文件:

cd /usr/local/keepalived/etc/keepalived/

cp keepalived.conf keepalived.conf.bak

cat keepalived.conf

! Configuration File for keepalived

global_defs {

      notification_email {

      x@a.com #邮件地址

     }

     notification_email_from keepalived157@a.com

     smtp_server 10.1.60.155

     smtp_connect_timeout 30

     router_id LVS_DEVEL

}

vrrp_script chk_http_port {

 script "/usr/local/keepalived/nginx_pid.sh"

 interval 2

 weight 2

}

 track_script {

 chk_http_port

 }

vrrp_instance VI_1 {

 state MASTER #这里是主所以填MASTER

 interface eth0 #您的ip配在哪个网卡上就填哪个

 virtual_router_id 100 #路由优先级值越大优先级越高

 priority 100

 advert_int 1

 authentication {

 auth_type PASS

 auth_pass founder

 }

 track_interface {

 eth0

 }

 virtual_ipaddress {

 10.1.60.254 #虚拟vip ip

 }

}

编辑从nginx10.1.60.157keepalived配置文件:

cat keepalived.conf

! Configuration File for keepalived

global_defs {

      notification_email {

      x@a.com #邮件地址

     }

     notification_email_from keepalived157@a.com

     smtp_server 10.1.60.155

     smtp_connect_timeout 30

     router_id LVS_DEVEL

}

vrrp_script chk_http_port {

 script "/usr/local/keepalived/nginx_pid.sh"

 interval 2

 weight 2

}

 track_script {

 chk_http_port

 }

vrrp_instance VI_1 {

 state BACKUP #这里是从所以填BACKUP

 interface eth0 #您的ip配在哪个网卡上就填哪个

 virtual_router_id 50 #路由优先级值越大优先级越高

 priority 100

 advert_int 1

 authentication {

 auth_type PASS

 auth_pass founder

 }

 track_interface {

 eth0

 }

 virtual_ipaddress {

 10.1.60.254 #虚拟vip ip

 }

}

启动两台keepalived服务

service keepalived start

打开vip ip 进行测试:

http://10.1.60.254

QQ截图20131224152637

Whatever is worth doing is worth doing well.