lzf79 发表于 2018-12-31 08:39:37

Nginx + keepalived 主备负载均衡搭建实战

  2. 安装配置过程
  软件下载
  wget http://nginx.org/download/nginx-1.2.3.tar.gz
  wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.12/pcre-8.12.tar.gz
  wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
  1) Nginx安装配置(两台机器一样安装)
  # pcrc-8.12
  tar zxvf pcre-8.12.tar.gz
  cd pcre-8.12
  ./configure
  make && make install
  # nginx-1.2.3
  groupadd www
  useradd –g www www
  tar zxvf nginx-1.2.3.tar.gz
  cd nginx-1.2.3
  ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  make && make install
  2) Keepalived安装配置(两台机器都要安装)
  tar zxvf keepalived-1.2.7.tar.gz
  cd keepalived-1.2.7
  ./configure –prefix=/usr/local/keepalived
  make && make install
  mkdir /etc/keepalived
  cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
  cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
  chkconfig --add keepalived
  chkconfig keepalived on
  3) Keepalived 配置
  (主keepalived即nginx-1上的配置)
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  root@189.cn
  }
  notification_email_from root@189.cn
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_script Monitor_Nginx {
  script "/usr/local/scripts/nginx_pid.sh"
  interval 2
  weight 2
  }
  //定义一个检测nginx程序的脚本
  vrrp_instance VI_1 {
  state MASTER
  interface eth1   // 实例绑定到一块稳定的网卡上
  virtual_router_id 51
  priority 180    // 评定优先级,数值大的为master
  advert_int 1    // 心跳检测时间,单位秒
  authentication {   // 主备nginx之间的通信认证
  auth_type PASS
  auth_pass 1234
  }
  track_script {
  Monitor_Nginx
  }
  virtual_ipaddress {   // 配置VIP、子网掩码、VIP要游动的网卡
  220.180.112.14/27 dev eth0
  220.180.112.15/27 dev eth0
  172.16.10.99/24 dev eth1
  }
  }
  (备keepalived 上的配置)
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  root@189.com
  }
  notification_email_from root@189.cn
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_script Monitor_Nginx {
  script "/usr/local/scripts/nginx_pid.sh"
  interval 2
  weight 2
  }
  vrrp_instance VI_1 {
  state BACKUP
  interface eth1
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1234
  }
  track_script {
  Monitor_Nginx
  }
  virtual_ipaddress {
  220.180.112.14/27 dev eth0
  220.180.112.15/27 dev eth0
  172.16.10.99/24 dev eth1
  }
  }
  (/usr/local/scripts/nginx_pid.sh脚本的内容)
  #!/bin/bash
  A=`ps -C nginx --no-header |wc -l`
  if [ $A -eq 0 ];then      // 判断nginx是否未启用
  /usr/local/nginx/sbin/nginx   // 运行nginx
  sleep 3
  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  //3秒后检测,如果nginx启动出错就关闭keepalived
  killall keepalived
  fi
  fi
  4) Nginx配置
  两台nginx的配置一样,如下:
  userwww www;
  worker_processes1;
  #error_loglogs/error.log;
  #error_loglogs/error.lognotice;
  #error_loglogs/error.loginfo;
  #pid      logs/nginx.pid;
  events {
  use epoll;
  worker_connections51200;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  access_loglogs/access.logmain;
  sendfile      on;
  #tcp_nopush   on;
  #keepalive_timeout0;
  keepalive_timeout65;
  gzipon;
  #####################################
  # web
  upstream web {
  server 172.16.10.10;
  server 172.16.10.11;
  ip_hash;
  }
  # pic
  upstream pic {
  server 172.16.10.20;
  server 172.16.10.21;
  ip_hash;
  }
  # natip
  upstream natip {
  server 172.16.10.20:8080;
  server 172.16.10.21:8080;
  ip_hash;
  }
  #########################################
  # web
  server {
  listen      80;
  server_name www.abc.com;
  location / {
  proxy_pass      http://web;
  proxy_set_header    Host            $host;
  proxy_set_header    X-Real-IP       $remote_addr;
  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  location /status {
  stub_status on;
  access_log off;
  error_log off;
  }
  }
  # pic
  server {
  listen      80;
  server_name pic.abc.com;
  location / {
  proxy_pass      http://pic;
  proxy_set_header    Host            $host;
  proxy_set_header    X-Real-IP       $remote_addr;
  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  location /status {
  stub_status on;
  access_log off;
  error_log off;
  }
  }
  # natip
  server {
  listen      8080;
  server_name 172.16.10.4;
  location / {
  proxy_pass      http://natip;
  proxy_set_header    Host            $host;
  proxy_set_header    X-Real-IP       $remote_addr;
  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  location /status {
  stub_status on;
  access_log off;
  error_log off;
  }
  }
  }

页: [1]
查看完整版本: Nginx + keepalived 主备负载均衡搭建实战