程序员Linux架构

Nginx+Keepalived主从模式-负载均衡高可用

2017-03-28  本文已影响949人  皇阿玛PLUS

目录

简介

1.准备工作

2.部署Nginx

3.部署Keepalived

4.Nginx+Keepalived的高可用测试


简介

这种方案,使用一个VIP地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备机在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案并不经济实惠。

关于Nginx版本

Mainline version:开发版
Stable version:稳定版
Legacy versions:遗留的老版本

官方地址:http://nginx.org/ ,找到“news”中,最新的一个stable version
下载地址:http://nginx.org/download/ ,找到这个包的下载链接,右键复制链接地址

规划:

LB-01:192.168.1.191 nginx+keepalived-master
LB-02:192.168.1.192 nginx+keepalived-backup
VIP:192.168.1.99

OS:CentOS 6.8 X64

架构图:
Nginx+Keepalived主从架构

1.准备工作

1.1 关闭SELinux
  [root@example01 ~]# vim /etc/sysconfig/selinux
  SELINUX=disabled
1.2 关闭IPTABLES防火墙
  [root@example01 ~]# vim /etc/sysconfig/iptables
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
  # 允许VRRP(虚拟路由冗余协议)通信
  -A INPUT -p vrrp -d -j ACCEPT
  # 允许Keepalived虚拟路由组播地址通信
  -A INPUT -d 224.0.0.18 -j ACCEPT


  [root@example01 ~]# service iptables restart
  iptables: Applying firewall rules:                         [  OK  ]
1.3 安装wget
  [root@example01 ~]# yum -y install wget

准备工作到此为止,reboot命令,重启两台服务器,使得SELinux配置生效

2.部署nginx

2.1 安装编译工具及库文件
  [root@example01 ~]# yum -y install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.2 安装nginx
  [root@example01 ~]# cd /usr/local/src/
  [root@example01 src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
  [root@example01 src]# tar -zxvf nginx-1.6.2.tar.gz 
  [root@example01 src]# cd nginx-1.6.2
  [root@example01 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx \
  --with-http_stub_status_module \
  --with-http_ssl_module
  [root@example01 nginx-1.6.2]# make && make install
  • 配置报错:
    ./configure: error: the HTTP rewrite module requires the PCRE library.
    You can either disable the module by using --without-http_rewrite_module
    option, or install the PCRE library into the system, or build the PCRE library
    statically from the source with nginx by using --with-pcre=<path> option.
  • 解决办法:
  [root@example01 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx \
  > --without-http_rewrite_module

2.3 配置nginx

2.3.1 创建nginx运行使用的用户www
  [root@example01 nginx-1.6.2]# /usr/sbin/groupadd www
  [root@example01 nginx-1.6.2]# /usr/sbin/useradd -g www www
2.3.2 启动nginx

启动服务

  [root@example01 nginx-1.6.2]# /usr/local/nginx/sbin/nginx

重载nginx配置

  [root@example01 nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s reload

开机启动

  [root@example01 src]# vim /etc/rc.local 
  # Nginx
  /usr/local/nginx/sbin/nginx
2.3.3 编辑index.html文件

编辑LB-01:192.168.1.191

  [root@example01 nginx-1.6.2]# vim /usr/local/nginx/html/index.html 
   14 <h1>Welcome to nginx!Server01</h1>

编辑LB-02:192.168.1.192

  [root@example02 nginx-1.6.2]# vim /usr/local/nginx/html/index.html 
   14 <h1>Welcome to nginx!Server02</h1>
2.3.4 配置nginx.conf
    2 user  www www;
    3 worker_processes  1;

   35    upstream my Server {
   36        ip_hash;

   37         server 192.168.1.191:80;
   38         server 192.168.1.192:80;
   39     }

Tips:

2.3.5 浏览器访问:

http://192.168.1.191

LB-01:192.168.1.191

http://192.168.1.192

LB-02:192.168.1.192
nginx其它命令:
  /usr/local/nginx/sbin/nginx -s reload            # 重新载入配置文件
  /usr/local/nginx/sbin/nginx -s reopen            # 重启 Nginx
  /usr/local/nginx/sbin/nginx -s stop              # 停止 Nginx

3.部署keepalived

3.1 安装keepalived
  [root@example01 src]# yum -y install keepalived
查看keepalived版本
  [root@example01 src]# keepalived -v
  Keepalived v1.2.13 (03/19,2015)
3.2 修改keepalived的配置文件

LB-01:192.168.1.191的配置

  [root@example01 src]# vim /etc/keepalived/keepalived.conf
  vrrp_script chk_nginx {
     script "/etc/keepalived/nginx_check.sh"    # 检测nginx状态的脚本路径
     interval 2                 # 检测时间间隔2s
     weight -20                 # 如果脚本的条件成立,权重-20
  }

  vrrp_instance VI_1 {
      state MASTER              # 服务状态;MASTER(工作状态)BACKUP(备用状态)
      interface eth0              # VIP绑定网卡
      virtual_router_id 51      # 虚拟路由ID,主、备节点必须一致
      mcast_src_ip 192.168.1.191  # 本机IP
      nopreempt                # 优先级高的设置,解决异常回复后再次抢占的问题
      priority 100              # 优先级;取值范围:0~254;MASTER > BACKUP
      advert_int 1              # 组播信息发送间隔,主、备节点必须一致,默认1s
      authentication {          # 验证信息;主、备节点必须一致
          auth_type PASS          # VRRP验证类型,PASS、AH两种
          auth_pass 1111          # VRRP验证密码,在同一个vrrp_instance下,主、从必须使用相同的密码才能正常通信
      }
    track_script {           # 将track_script块加入instance配置块
          chk_nginx         # 执行Nginx监控的服务
      }
      virtual_ipaddress {         # 虚拟IP池,主、备节点必须一致,可以定义多个VIP
          192.168.1.99          # 虚拟IP
      }
  }

LB-02:192.168.1.192的配置

  [root@example02 src]# vim /etc/keepalived/keepalived.conf
  vrrp_script chk_nginx {
     script "/etc/keepalived/nginx_check.sh"
     interval 2
     weight -20
  }

  vrrp_instance VI_1 {
      state BACKUP
      interface eth0
      virtual_router_id 51
      mcast_src_ip 192.168.1.192
      priority 90
      advert_int 1
      authentication {
          auth_type PASS
          auth_pass 1111
      }
      track_script {
          chk_nginx
      }
      virtual_ipaddress {
          192.168.1.99
      }   
  }   
3.3 编写nginx状态监测脚本
  [root@example01 keepalived]# vim /etc/keepalived/nginx_check.sh
  #!/bin/bash
  A=`ps -C nginx –no-header |wc -l`
  if [ $A -eq 0 ];then
          /usr/local/nginx/sbin/nginx
          sleep 2
          if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
                  killall keepalived
          fi
  fi

脚本要求:如果 nginx 停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程, keepalied将虚拟 ip 绑定到 BACKUP 机器上。

3.4 保存脚本,赋予执行权限
  [root@example01 keepalived]# chmod +x /etc/keepalived/nginx_check.sh 
  [root@example01 keepalived]# ll
  total 8
  -rw-r--r--. 1 root root 3602 Mar 27 23:46 keepalived.conf
  -rwxr-xr-x. 1 root root  191 Mar 27 23:53 nginx_check.sh
3.5 启动keepalived

开机启动

  [root@example02 src]# chkconfig keepalived on

启动服务

  [root@example01 keepalived]# service keepalived start
  Starting keepalived:                                       [  OK  ]

4.keepalived+nginx的高可用测试

4.1 查看服务器上的地址

查看MASTER的地址:

  [root@example01 keepalived]# ip add
  1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
      link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
      inet 127.0.0.1/8 scope host lo
      inet6 ::1/128 scope host 
         valid_lft forever preferred_lft forever
  2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
      link/ether 00:0c:29:37:2c:86 brd ff:ff:ff:ff:ff:ff
      inet 192.168.1.191/24 brd 192.168.1.255 scope global eth0
      inet 192.168.1.99/32 scope global eth0                    # 注意,此时MASTER上存在一个VIP
      inet6 fe80::20c:29ff:fe37:2c86/64 scope link 
         valid_lft forever preferred_lft forever

查看BACKUP的地址:

  [root@example02 src]# ip add
  1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
      link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
      inet 127.0.0.1/8 scope host lo
      inet6 ::1/128 scope host 
         valid_lft forever preferred_lft forever
  2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
      link/ether 00:0c:29:5b:9e:6b brd ff:ff:ff:ff:ff:ff
      inet 192.168.1.192/24 brd 192.168.1.255 scope global eth0
      inet6 fe80::20c:29ff:fe5b:9e6b/64 scope link 
         valid_lft forever preferred_lft forever
4.2 关闭MASTER上的nginx,keepalived会将它重新启动
  [root@example01 keepalived]# /usr/local/nginx/sbin/nginx -s stop
4.3 关闭MASTER上的keepalived,VIP会切换到BACKUP上
  [root@example01 keepalived]# service keepalived stop
  Stopping keepalived:                                       [  OK  ]
4.4 验证VIP的漂移
验证方法1:通过ip add查看VIP的漂移
  [root@example01 keepalived]# ip add
  1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
      link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
      inet 127.0.0.1/8 scope host lo
      inet6 ::1/128 scope host 
         valid_lft forever preferred_lft forever
  2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
      link/ether 00:0c:29:37:2c:86 brd ff:ff:ff:ff:ff:ff
      inet 192.168.1.191/24 brd 192.168.1.255 scope global eth0
      inet6 fe80::20c:29ff:fe37:2c86/64 scope link 
         valid_lft forever preferred_lft forever


  [root@example02 src]# ip add
  1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
      link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
      inet 127.0.0.1/8 scope host lo
      inet6 ::1/128 scope host 
         valid_lft forever preferred_lft forever
  2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
      link/ether 00:0c:29:5b:9e:6b brd ff:ff:ff:ff:ff:ff
      inet 192.168.1.192/24 brd 192.168.1.255 scope global eth0
      inet 192.168.1.99/32 scope global eth0                    # 注意,此时存在一个VIP
      inet6 fe80::20c:29ff:fe5b:9e6b/64 scope link 
         valid_lft forever preferred_lft forever
验证方法2:通过浏览器访问VIP

浏览器访问:http://192.168.1.99
刷新页面,显示“Welcome to nginx!Server02”,表示已经VIP已经漂移到了BACKUP服务器上

高可用

到这里,整个部署就已经完成了!

Tips:

上一篇下一篇

猜你喜欢

热点阅读