sdfsdnfslk 发表于 2018-12-30 15:11:40

Nginx+keepalived 负载均衡高可用配置CentOS6.4

  Nginx+keepalived 负载均衡高可用配置CentOS6.4
  
  
一:测试环境
服务器DELL-R720
系统版本:CentOS6.4 64bit
虚拟化 KVM虚拟机系统CentOS6.4 64bit
虚拟机4台:
Nginx01,nginx02,web01,web02
网络环境:
Nginx01:192.168.40.30
Nginx02:192.168.40.31
Web01:192.168.40.86
Web02:192.168.40.87
Vip:192.168.40.32


二:服务配置
1>Nginx01
#wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
#rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
#yum list |grep nginx
nginx-release-centos.noarch             6-0.el6.ngx                     installed
nginx.x86_64                           1.2.8-1.el6.ngx                   nginx
nginx-debug.x86_64                     1.2.8-1.el6.ngx                   nginx
#yum -y install nginx
#vi /etc/nginx/nginx.conf
user            nginx;
worker_processes1;

error_log/var/log/nginx/error.log;
#error_log/var/log/nginx/error.lognotice;
#error_log/var/log/nginx/error.loginfo;

pid      /var/run/nginx.pid;

events {
    worker_connections1024;
}

http {
    include       /etc/nginx/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_log/var/log/nginx/access.logmain;

    sendfile      on;
    #tcp_nopush   on;

    #keepalive_timeout0;
    keepalive_timeout65;

    #gzipon;
   
    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;

upstreamtest{
    server   192.168.40.86;
    server   192.168.40.87;
}

server{
      listen80;
      server_name192.168.40.32;
      location / {
      proxy_pass      http://test;
      proxy_set_header   Host             $host;
      proxy_set_header   X-Real-IP      $remote_addr;
      proxy_set_header   X-Forwarded-For$proxy_add_x_forwarded_for;
      }
      }

}

>Keepalived 配置信息
#vi /etc/keepalived/keepalived.conf

! 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 "/opt/nginx_pid.sh"   #nginx 服务检测脚本
      interval 2
      weight 2
}

vrrp_instance VI_1 {
    state MASTER   #nginx02修改为BACKUP
    interface eth0
    virtual_router_id 55
    priority 100      #nginx02 修改为 80低于MASTER即可
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
}   
track_script {             #指定一下检测的脚本http_port
    http_port
    }
    virtual_ipaddress {
      192.168.40.32
    }
}

2>付上nginx服务检测脚本
# vi /opt/nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
                /etc/init.d/nginx start
                sleep 3
                if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
                     killall keepalived
                fi
fi

3>nginx02配置与01相同,keepalived稍作修改

4>启动nginx01 02 上的nginx 和keepalived服务

三:测试
1>负载均衡测试
http://songxj.blog.运维网.com/attachment/201304/7/620981_1365307327lBRd.jpg
刷新
http://songxj.blog.运维网.com/attachment/201304/7/620981_1365307327hcv1.jpg
Down掉40.86这台web再测试只有87这台web响应。

2>HA 测试
Ip a 结果显示vip在nginx01上
# ip a
1: lo:mtu 16436 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:mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:57:3b:80 brd ff:ff:ff:ff:ff:ff
    inet 192.168.40.30/24 brd 192.168.40.255 scope global eth0
    inet 192.168.40.32/32 scope global eth0
    inet6 fe80::5054:ff:fe57:3b80/64 scope link
       valid_lft forever preferred_lft forever

移动01这台nginx的配置文件然后down掉nginx服务,使其nginx服务不能启动
#/etc/init.d/nginx status
nginx is stopped

然后ip a发现VIP地址已经飘到nginx02上去了
# ip a
1: lo:mtu 16436 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:mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:95:45:5f brd ff:ff:ff:ff:ff:ff
    inet 192.168.40.31/24 brd 192.168.40.255 scope global eth0
    inet 192.168.40.32/32 scope global eth0
    inet6 fe80::5054:ff:fe95:455f/64 scope link
       valid_lft forever preferred_lft forever




页: [1]
查看完整版本: Nginx+keepalived 负载均衡高可用配置CentOS6.4