FXMAR 发表于 2018-12-31 14:58:04

keepalived+nginx双机热备+负载均衡

  http://blog.csdn.net/e421083458/article/details/30092795
  keepalived+nginx双机热备+负载均衡
  最近因业务扩展,需要将当前的apache 转为nginx(web), 再在web前端放置nginx(负载均衡)。同时结合keepalived 对前端nginx实现HA。
  nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能。在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由主进程发出,Slave(worker)进程所有的超时任务都会被Master中止,属于非阻塞式任务模型。
  Keepalived是Linux下面实现VRRP 备份路由的高可靠性运行件。基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接。二者结合,可以构架出比较稳定的软件lb方案。
http://img.blog.csdn.net/20140611194032875?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZTQyMTA4MzQ1OA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
  准备4台电脑来做这个实验:
  192.168.232.132      web服务器
  192.168.232.133      web服务器
  192.168.232.134      keepalived nginx
  192.168.232.135      keepalived nginx
  虚拟IP (VIP):192.168.232.16
134\135两个主机配置虚拟IP
  下面以135为例:
  vi /etc/sysconfig/network-scripts/ifcfg-eth2:0
   view plain copy print?

[*]  DEVICE=eth2:0
[*]  TYPE=Ethernet
[*]  ONBOOT=yes
[*]  BOOTPROTO=static
[*]  DNS1=192.168.232.2
[*]  IPADDR=192.168.232.16
[*]  NETMASK=255.255.255.0
[*]  GETWAY=192.168.232.2
  service network restart
  使用ifconfig查看效果:
   view plain copy print?

[*]  eth2      Link encap:EthernetHWaddr 00:0C:29:49:90:5B
[*]  inet addr:192.168.232.135Bcast:192.168.232.255Mask:255.255.255.0
[*]  inet6 addr: fe80::20c:29ff:fe49:905b/64 Scope:Link
[*]  UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
[*]  RX packets:66322 errors:0 dropped:0 overruns:0 frame:0
[*]  TX packets:31860 errors:0 dropped:0 overruns:0 carrier:0
[*]  collisions:0 txqueuelen:1000
[*]  RX bytes:67624991 (64.4 MiB)TX bytes:2723877 (2.5 MiB)
[*]  Interrupt:19 Base address:0x2000
[*]
[*]  eth2:0    Link encap:EthernetHWaddr 00:0C:29:49:90:5B
[*]  inet addr:192.168.232.16Bcast:192.168.232.255Mask:255.255.255.0
[*]  UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
[*]  Interrupt:19 Base address:0x2000
[*]
[*]  lo      Link encap:Local Loopback
[*]  inet addr:127.0.0.1Mask:255.0.0.0
[*]  inet6 addr: ::1/128 Scope:Host
[*]  UP LOOPBACK RUNNINGMTU:16436Metric:1
[*]  RX packets:22622 errors:0 dropped:0 overruns:0 frame:0
[*]  TX packets:22622 errors:0 dropped:0 overruns:0 carrier:0
[*]  collisions:0 txqueuelen:0
[*]  RX bytes:1236328 (1.1 MiB)TX bytes:1236328 (1.1 MiB)
  说明生效了。
134\135两个主机安装keepalived和nginx
  nginx安装:
  1、导入外部软件库
  rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/epel-release-6-5.noarch.rpm
  rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm
  rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
  以下添加注释
  mirrorlist=http://dmirr.iuscommunity.org/mirrorlist?repo=ius-el6&arch=$basearch
  以下删除注释
  #baseurl=http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/$basearch
  2、yum安装nginx
  yum install nginx
  keepalived安装:
  安装依赖
  yum -y install gcc gcc+ gcc-c++
  yum install popt-devel openssl openssl-devel libssl-dev libnl-devel popt-devel
  安装内核
  yum -y install kernel kernel-devel
  当前kernel代码建立连接 ln -s /usr/src/kerners/2.6....../ /usr/src/linux
  安装keepalived
  wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
  tar -zxvf keepalived-1.2.2.tar.gz
  cd keepalived-1.2.2
  ./configure
  make
  make install
  拷贝相应的文件
  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/
配置keeplived和nginx主机
  134/135执行都执行以下操作:
  vi /etc/nginx/conf.d/default.conf
   view plain copy print?

[*]  server {
[*]  listen       8088;
[*]  server_namelocalhost;
[*]
[*]  location / {
[*]  root   /var/www/html;
[*]  indexindex.html index.htm;
[*]  }
[*]
[*]  error_page   500 502 503 504/50x.html;
[*]  location = /50x.html {
[*]  root   /usr/share/nginx/html;
[*]  }
[*]  }
  135执行以下操作:
  vi /var/www/html/index.html
   view plain copy print?

[*]  
[*]  
[*]  Welcome to nginx!
[*]  
[*]  
[*]  Welcome to nginx! 192.168.232.135
[*]  
[*]  
  134执行以下操作:
  vi /var/www/html/index.html
   view plain copy print?

[*]  
[*]  
[*]  Welcome to nginx!
[*]  
[*]  
[*]  Welcome to nginx! 192.168.232.134
[*]  
[*]  
  134执行以下操作:
  vi /etc/keepalived/keepalived.conf
   view plain copy print?

[*]  ! Configuration File for keepalived
[*]
[*]  global_defs {
[*]  notification_email {
[*]  #acassen@firewall.loc
[*]  #failover@firewall.loc
[*]  #sysadmin@firewall.loc
[*]  }
[*]  #notification_email_from Alexandre.Cassen@firewall.loc
[*]  #smtp_server 192.168.200.1
[*]  #smtp_connect_timeout 30
[*]  router_id LVS_DEVEL
[*]  }
[*]
[*]  vrrp_script chk_http_port {
[*]  script "
页: [1]
查看完整版本: keepalived+nginx双机热备+负载均衡