yundian 发表于 2013-4-8 09:11:47

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

一:测试环境服务器DELL-R720系统版本:CentOS6.4 64bit虚拟化 KVM虚拟机系统CentOS6.4 64bit虚拟机4台:Nginx01,nginx02,web01,web02网络环境:Nginx01:192.168.40.30Nginx02:192.168.40.31Web01:192.168.40.86Web02:192.168.40.87Vip: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 nginxnginx-release-centos.noarch             6-0.el6.ngx                     installednginx.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.confuser            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/bashA=`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                fifi
3>nginx02配置与01相同,keepalived稍作修改
4>启动nginx01 02 上的nginx 和keepalived服务
三:测试1>负载均衡测试刷新Down掉40.86这台web再测试只有87这台web响应。
2>HA 测试Ip a 结果显示vip在nginx01上# ip a1: lo: <LOOPBACK,UP,LOWER_UP> 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 forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> 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 statusnginx is stopped
然后ip a发现VIP地址已经飘到nginx02上去了# ip a1: lo: <LOOPBACK,UP,LOWER_UP> 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 forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> 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

sanhutrees 发表于 2013-4-8 09:20:23

我抢、我抢、我抢沙发~

link 发表于 2013-5-17 17:14:35

下雨了,别忘了打伞,湿身是小,淋病就麻烦啦*^_^*

378 发表于 2013-5-20 07:00:29

所有的男人生来平等,结婚的除外。

吸毒的虫子 发表于 2013-5-22 16:35:04

俺从不写措字,但俺写通假字!

火冰狐 发表于 2013-5-27 13:48:19

学习了,谢谢分享、、、

yanfangsheng123 发表于 2013-5-31 16:13:17

生我之前谁是我,生我之后我是谁?
页: [1]
查看完整版本: Nginx+keepalived 负载均衡高可用配置CentOS6.4