szs 发表于 2018-11-14 08:24:10

keepalived + nginx

  centos6.5-x86_64
  nginx-1.4.5
  keepalived-1.2.5
  master:   192.168.1.126
  slave :   192.168.1.128
  vip   :   192.168.1.130
  一、软件下载:
  wget   http://www.keepalived.org/software/keepalived-1.2.5.tar.gz
  http://nginx.org/en/download.html
  二、安装nginx
  参考http://dodowolf.blog.51cto.com/793581/1596684
  三、keepalived (以下为源码安装 或 #yum-y install keepalived)
  1.安装依赖包
  #yum -y install gcc gcc+ gcc-c++ popt openssl openssl-devellibnllibnl-devel
  #yum -y install kernel kernel-devel
  当前kernel代码建立连接 ln -s /usr/src/kerners/2.6....../ /usr/src/linux
  2.安装keepalived
  # tar -zxvf keepalived-1.2.5.tar.gz
  # cd keepalived-1.2.5
  # ./configure
  # make
  # make install
  3.cp相应文件到系统目录
  #cp /usr/local/sbin/keepalived /usr/sbin/
  #cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
  #cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
  #cp -r /usr/local/etc/keepalived/ /etc/
  4.修改配置文件内容
  /etc/keepalived/keepalived.conf
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ! Configuration File for keepalived
  global_defs {
  #   notification_email {
  #    root@localhost
  #   }
  #   notification_email_from admin@localhost
  #   smtp_server localhost
  #   smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_instance VI_1 {
  state MASTER      #BACKUP
  interface eth0
  virtual_router_id 51
  priority 100      #BACKUP 可设置为 99
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  track_interface {
  eth0
  }
  virtual_ipaddress {
  192.168.1.130
  }
  notify_master "/etc/keepalived/notify.sh master"
  notify_backup "/etc/keepalived/notify.sh backup"
  notify_fault"/etc/keepalived/notify.sh fault"
  }
  #vi /etc/keepalived/notify.sh
  #!/bin/bash
  # time: 2015-1-5
  # description: An example of notify script
  #
  vip=192.168.1.130
  contact='XXX@163.com'
  notify() {
  mailsubject="`hostname` to be $1: $vip floating"
  mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
  echo $mailbody | mail -s "$mailsubject" $contact
  }
  case "$1" in
  master)
  notify master
  /etc/rc.d/init.d/nginx restart
  exit 0
  ;;
  backup)
  notify backup
  /etc/rc.d/init.d/nginx stop
  exit 0
  ;;
  fault)
  notify fault
  /etc/rc.d/init.d/nginx stop
  exit 0
  ;;
  *)
  echo 'Usage: `basename $0` {master|backup|fault}'
  exit 1
  ;;
  esac
  四、Tell kernel to allow binding non-local IP into the hosts and apply the changes:
$ echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.conf  
$ sysctl -p
  五、测试
  1.master、slave分别启动nginx\keepalived
  # /usr/local/nginx/sbin/nginx
  # servicekeepalivedstart
  2.测试
  浏览器分别访问master,slave   测试是否正常
  访问vip 可以看到显示的是master
  在master上执行# servicekeepalived stop
  访问vip显示slave
  可以通过 /var/log/messages 查看切换日志
  注意:1.以上是keepalived挂掉的情况下才切换到slave,如果nginx挂掉 不会切换,无法访问;所以做下以下设置
  2.service network restart 一定要在keepalived停止的时候才执行,不要然后keepalived不会正常运行
  六、应用层(web)的双机热备(比如nginx进程被意外kill、web端口不通)
  1.主要是利用keepalived的track_script检测脚本的原理实现的
  2.配置文件的修改
  /etc/keepalived/keepalived.conf
  ~~~~~~~~~~~~~~~~~~~~~~~
  ! Configuration File for keepalived
  global_defs {
  #   notification_email {
  #    root@localhost
  #   }
  #   notification_email_from admin@localhost
  #   smtp_server localhost
  #   smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_script chk_nginx_down {
  script "ps -C nginx --no-header 2>&- 1>&- && exit 0 || exit 1"
  interval 1
  weight -2
  }
  vrrp_instance VI_1 {
  state MASTER      #BACKUP
  interface eth0
  virtual_router_id 51
  priority 100      #BACKUP可设置为99
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  track_interface {
  eth0
  }
  track_script {
  chk_nginx_down
  }
  virtual_ipaddress {
  192.168.1.130
  }
  notify_master "/etc/keepalived/notify.sh master"
  notify_backup "/etc/keepalived/notify.sh backup"
  notify_fault"/etc/keepalived/notify.sh fault"
  }
  七、以下介绍如何设置 外部邮箱发送邮件通知:
  修改/etc/mail.rc
  vi /etc/mail.rc
  set from=mail@163.com
  smtp=smtp.163.com
  set smtp-auth-user=username
  smtp-auth-password=password
  smtp-auth=login
  说明:
  from是发送的邮件地址
  smtp是发生的外部smtp服务器的地址
  smtp-auth-user是外部smtp服务器认证的用户名
  smtp-auth-password是外部smtp服务器认证的用户密码
  smtp-auth是邮件认证的方式
  配置成功后,就可以使用了
  可以发送一封邮件测试一下:
  mail -s "test" user@sohu.com
页: [1]
查看完整版本: keepalived + nginx