日期:2012-07-04 12:30:00  来源:本站整理

CentOS 下 Nginx + Keepalived 配置高可用Web站点[服务器安全]

赞助商链接



  本文“CentOS 下 Nginx + Keepalived 配置高可用Web站点[服务器安全]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

  简述:

  近些年来,Nginx以其自身处理的并发数高,内存损耗小,配置简单,开源免费,支持Rewrite等一系列的有点成为网站Web端的首选软件,同时,Nginx最大的特点是他自身的负载均衡的功效,可以通过定义Upstream地址池,通过呼应的分发原则实现网站拜候的首选利器,目前的网站最基本的要求就是要安定高效,这里介绍的Keepalived+Nginx的架构方法就是结合这个来实现的,这里我们采取两台Nginx服务器作为前端,一主一从,Keepalived实近况态监测,保证Nginx正常对外供应服务,即主Nginx服务进程死掉之后,keepalived可以通过其自身的检测机制将网站的拜候切换到从Nginx上来.

  系统环境:CentOS 5.5

  开源软件:nginx-1.2.1.tar.gz ; keepalived-1.2.2.tar.gz

  主服务器IP:211.151.138.2 从服务IP:211.151.138.3 虚IP:211.151.138.5 你可以将你网站域名解析到 211.151.138.5 这一个公网IP上,这样主从服务器可以轮番接纳该虚IP,保证网站正常对外供应拜候

  keepalived 安装需求依靠 openssl , 直接yum安装便可,yum install openssl openssl-devel

  nginx 的安装这里就不介绍了,网上相关文档很多,这里侧重介绍 keepalived + nginx 的实现方法

  一: 安装 keepalived

  主从服务器都操作:

  下载 keepalived 软件包

  wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

  tar zxvf keepalived-1.2.2 .tar.gz

  ./configure

  make && make install

  默许情形下,keepalived 会安装在 /usr/local/keepalived 下,你可以通过 --prefix 定向其安装位置

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

  mkdir /etc/keepalived (默许情形下,keepalived 会读取 /etc/keepalived 下keepalived.conf 文件,假如你没有成立这个文件,keepalived也不会报错,但是你会发现,你所成立的关于keepalived的相关参数根本就没有表现,keepalived这一点做的让人挺迷惑的 )

  二:此时,我们成立 keepalived.conf 文件

  主服务器配置:

  vim /etc/keepalived/keepalived.conf , 键入以下内容

  global_defs {

  notification_email {

  admin@www.linuxidc.com (这里可以定义多个报警邮箱)

  }

  notification_email_from alarm@www.linuxidc.com (报警人)

  smtp_server 127.0.0.1

  smtp_connect_timeout 30

  router_id LVS_DEVEL

  }

  vrrp_script chk_http_port {

  script "/opt/tools/bin/check_ng.sh"

  interval 2 (检测的隔断)

  weight 2

  }

  vrrp_instance VI_1 {

  state MASTER (显示定义为主服务器)

  interface eth1 (绑定的网口,该网口即上面提到的两个IP的接口)

  virtual_router_id 51 (定义的ID,官方的是 51,主从服务器必须一向)

  mcast_src_ip 211.151.138.2 (主服务器的IP)

  priority 100 (优先级,肆意定义,但是一定要比从服务器高)

  advert_int 1

  authentication {

  auth_type PASS

  auth_pass 1111 (默许便可)

  }

  track_script {

  chk_http_port (调用检测脚本)

  }

  virtual_ipaddress {

  211.151.137.5 (绑定的虚IP)

  }

  }从服务器配置

  vim /etc/keepalived/keepalived.conf , 键入以下内容

  global_defs {

  notification_email {

  admin@www.linuxidc.com (这里可以定义多个报警邮箱)

  }

  notification_email_from alarm@www.linuxidc.com (报警人)

  smtp_server 127.0.0.1

  smtp_connect_timeout 30

  router_id LVS_DEVEL

  }

  vrrp_script chk_http_port {

  script "/opt/tools/bin/check_ng.sh"

  interval 2 (检测脚本履行的隔断)

  weight 2

  }

  vrrp_instance VI_1 {

  state BACKUP (显示定义为从服务器)

  interface eth1 (绑定的网口,该网口即上面提到的两个IP的接口)

  virtual_router_id 51 (定义的ID,官方的是 51,主从服务器必须一向)

  mcast_src_ip 211.151.138.3 (从服务器的IP)

  priority 50 (优先级,肆意定义,但是一定要比主服务器低)

  advert_int 1

  authentication {

  auth_type PASS

  auth_pass 1111 (默许便可)

  }

  track_script {

  chk_http_port (调用检测脚本)

  }

  virtual_ipaddress {

  211.151.137.5 (绑定的虚IP)

  }

  }

  我们编辑 vim /opt/tools/bin/check_ng.sh

  #!/bin/bash

  N= `ps -C nginx --no-header |wc -l`

  if [ $N -eq 0 ];then

  /usr/local/nginx/sbin/nginx

  sleep 3

  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

  killall keepalived

  fi

  fi

  这是一个针对nginx状况举行检测的脚本,第一次nginx服务死掉时,会重新启动,假如Nginx服务仍旧没有起来,则杀掉keepalived进程

  三:服务器上都启动 keepalived 进程

  /etc/init.d/keepalived start

  履行 /sbin/ip a 我们看一下IP的利用情形

  主服务器:

  eth1:
mtu 1500 qdisc pfifo_fast qlen 1000

  link/ether 80:c1:6e:71:f1:a2 brd ff:ff:ff:ff:ff:ff

  inet 211.151.138.2/27 brd 211.151.138.31 scope global eth1

  inet 211.151.138.5/32 scope global eth1

  可见虚IP已经挂载上了

  同时查看 tail -f /var/log/message , keepalived 日记显示以下:

  Jun 28 18:44:25 proxy1 Keepalived_vrrp: Using LinkWatch kernel netlink reflector...

  Jun 28 18:44:25 proxy1 Keepalived_vrrp: VRRP sockpool: [ifindex(3), proto(112), fd(10,11)]

  Jun 28 18:44:25 proxy1 Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded

  Jun 28 18:44:26 proxy1 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE

  Jun 28 18:44:27 proxy1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE (这里显示其角色为主服务)

  Jun 28 18:44:27 proxy1 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.

  Jun 28 18:44:27 proxy1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 211.151.137.5

  同时从服务器日记显示为:

  Jun 28 18:42:14 proxy2 Keepalived: Starting Keepalived v1.2.2 (06/21,2012)

  Jun 28 18:42:14 proxy2 Keepalived: Starting VRRP child process, pid=23588

  Jun 28 18:42:14 proxy2 Keepalived_vrrp: Registering Kernel netlink reflector

  Jun 28 18:42:14 proxy2 Keepalived_vrrp: Registering Kernel netlink command channel

  Jun 28 18:42:14 proxy2 Keepalived_vrrp: Registering gratutious ARP shared channel

  Jun 28 18:42:14 proxy2 Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.

  Jun 28 18:42:14 proxy2 Keepalived_vrrp: Configuration is using : 65440 Bytes

  Jun 28 18:42:14 proxy2 Keepalived_vrrp: Using LinkWatch kernel netlink reflector...

  Jun 28 18:42:14 proxy2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE (显示为BACKUP,从)

  Jun 28 18:42:14 proxy2 Keepalived_vrrp: VRRP sockpool: [ifindex(3), proto(112), fd(10,11)]

  此时我们关闭主上的keepalived服务,则从的日记变成:

  Jun 28 18:43:49 proxy2 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE

  Jun 28 18:43:50 proxy2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE

  Jun 28 18:43:50 proxy2 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.

  Jun 28 18:43:50 proxy2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 211.151.138.5

  可见他已经接纳了211.151.138.5 这一个虚IP,并且把自己的状况置为了主 MASTER

  此时再把本来主上的keepalived进程起来,则其日记为:

  Jun 28 18:44:26 proxy2 Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert

  Jun 28 18:44:26 proxy2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE

  Jun 28 18:44:26 proxy2 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.

  Jun 28 18:44:26 proxy2 avahi-daemon[6552]: Withdrawing address record for 211.151.138.5 on eth1

  表示接纳到了更高权限的消息,于是他把自己的状况置为了从,并且偿还 211.151.138.5 这个IP

  如此,就实现了高可用 HA


  以上是“CentOS 下 Nginx + Keepalived 配置高可用Web站点[服务器安全]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • Windows 2008R2 HyperV下安装CentOS6,网卡无痛设置
  • CentOS 6.x 利用安装光盘作为yum源
  • CentOS 下 Nginx + Keepalived 配置高可用Web站点
  • CentOS 5.8布置web缓存服务器(squid+memcached)
  • centos6.2改正yum的源为安装光盘
  • CentOS6 yum搭建Linux+Nginx+PHP+MYSQL(LNMP)
  • CentOS 6.2 安装Nginx并设置为开机服务
  • CentOS 6.2布置DNS
  • CentOS6.2利用yum安装LAMP及phpMyadmin
  • CentOS下配置PHP+phpMyAdmin
  • centos下mysql主从同步快速设置步骤分享
  • 在Windows主机上按时备份远程VPS(CentOS)数据的批处理
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .