nginx负载均衡配合keepalived服务案例实战

本实验用4台 centos6 虚拟机,2台做负载均衡,2台做web服务器,都先装上nginx

lb01:192.168.0.235  --主负载均衡器

lb02:192.168.0.236  --备负载均衡器

web1:192.168.0.237

web2:192.168.0.238

VIP:192.168.0.248

一. 在web1和web2上配置基于域名的虚拟主机,以web1为例

 

1. 配置nginx.conf,  推荐方法 egrep -v "#|^$" nginx.conf.default > nginx.conf生成简洁配置再修改

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    

    server {
        listen       80;
        server_name  bbs.axinfu.com;
        location / {
            root   html/bbs; #站点目录
            index  index.html index.htm;
        }
        access_log logs/access_bbs.log main; #这里用于测试proxy_set_header X-Forwarded-For $remote_addr命令
    }

    server {
        listen       80;
        server_name  www.axinfu.com;
        location / {
            root   html/www; #站点目录
            index  index.html index.htm;

    }
       access_log logs/access_www.log main;
   }
}

注意

1. 这里有2个虚拟主机,bbs.axinfu.com和www.axinfu.com,这里故意把bbs放在上面,以便测试lb01和lb02上nginx的配置命令proxy_set_header Host $host;

2.类似于$remote_addr(客户端地址),$host这样的内置变量可参考 http://www.cnphp.info/nginx-embedded-variables-lasted-version.html

2. 在站点目录建立测试文件

我的nginx目录为/usr/local/nginx/,在此目录下新建bbs,www两个目录,都新建index.html,分别写入

hello, web1, i am bbs

hello, web1, i am www

在web2上配置相似,在index.html中分别写入hello, web2, i am bbs/ hello, web2, i am www

3. 检查语法并重启nginx服务

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx -s reload

netstat -lntup | grep 80

4. 把域名加入hosts解析,在/etc/hosts写入

192.168.0.237  bbs.axinfu.com

192.168.0.237  www.axinfu.com

5. 测试

curl bbs.axinfu.com  应该返回 hello, web1, i am bbs

curl www.axinfu.com  应该返回 hello, web1, i am www

二.  在lb01和lb02上配置nginx负载均衡如下,cat nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream www_server_pools {
        server 192.168.0.237:80 weight=1;
        server 192.168.0.238:80 weight=1;

    }
    server {
        listen       192.168.0.248; #指定监听的VIP
        server_name  www.axinfu.com;
        location / {
            proxy_pass http://www_server_pools;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }

    }
}

此配置仅代理里www.etiantian.org域名

命令说明

1. proxy_set_header Host $host; 

这个命令的作用是识别代理的哪个虚拟主机,这里代理的是www.axinfu.com( server_name写明了)

本实验在web服务器上配置了2个虚拟主机,如果不加这个命令,访问192.168.0.248时就把节点服务器的第一个虚拟主机发给反向代理,也就是bbs.axinfu.com

2. proxy_set_header X-Forwarded-For $remote_addr;

此命令与web1/2服务器上虚拟主机的访问日志有关,也就是 /usr/local/nginx/logs/access_www.log,正常情况下,这个文件最后一个字段应显示访客IP

比如使用192.168.0.230作为客户端进行测试时,不加这个命令第一个字段显示的是反向代理服务器的IP,最后一个字段显示的为"-",加上上面的命令,日志的最后一个字段显示的是客户端访问IP,”192.168.0.230"

这个命令要在web服务器上开启log_format功能,就是在nginx配置文件中的log_format块加上”http_x_forwarded_for"

3. 如果不要负载均衡,只是需要转发功能。那么删掉upstream代码块,在proxy_pass直接写要转发到的地址就好,比如proxy_pass http://127.0.0.1:9000 

三.  在lb01和lb02上配置keepalived服务

1. 在lb01上keepalived服务单实例主节点的配置如下,cat keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     1816635208@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id lb01          
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 55   #主备的虚拟路由ID号要一样
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.248/24 dev eth0 label eth0:1
    }
}

2. lb02上keepalived服务单实例备节点配置如下,cat keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     1816635208@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 55
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.248/24 dev eth0 label eth0:1

    }
}

四. 测试

先开启lb01的nginx和keepalived, 然后开启lb02的nginx和keepalived

在网页上输入 http://192.168.0.248,刷新即可看到切换hello, web1, i am www, hello, web2, i am www.

关闭lb01的keepalived,可以看到lb02启动切换到VIP:192.168.0.248, 可用ip add | grep 192.168.0.248查看

刷新页面192.168.0.248,内容还和之前一样,则说明服务正常

五. 可能存在的问题及注意

1. 要把防火墙关闭

2. lb02上开启nginx有时候会出现VIP地址冲突,不能分配的问题

在/etc/sysctl.conf加入内核参数配置,net.ipv4.ip_nonlocal_bind = 1 表示启动nginx而忽略配置中监听的IP是否存在, 执行sysctl -p使修改生效,注意环境是centos6

3. 默认情况下keepalived仅在对方机器停机或者keepalived停掉的时候才会接管业务,但在实际工作中,如果nginx服务停止而keepalived还在工作的情况下,就会导致用户访问的VIP无法找到对应的业务

通俗点说,就是lb01的nginx挂了,但是keeplived是好的,怎么让lb02来接管业务

第一种方法,写个守护进程脚本,当nginx挂了就停掉本地的keepalived

#!/bin/bash
while true
do
    if [ `netstat -lntup | grep nginx | wc -l` -ne 1 ];then
        /etc/init.d/keepalived stop
    fi
    sleep 5
done

第二种方法,在keepalived的配置文件中,加入检测服务脚本,检测监本如下

cat chk_nginx_proxy.sh

#!/bin/bash
if [ `netstat -lntup | grep nginx | wc -l` -ne 1 ];then
    /etc/init.d/keepalived stop
fi

增加执行权限,chmod +x chk_nginx_proxy.sh。 然后修改keepalived.conf,完整配置如下

lb01 # cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     1816635208@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id lb01
}

vrrp_script chk_nginx_proxy { #定义vrrp脚本
script "/server/scripts/chk_nginx_proxy.sh" #脚本位置
interval 2
weight 2
} vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id
55 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.248/24 dev eth0 label eth0:1 }

track_script{
chk_nginx_proxy #触发检查
} }

当lb01停掉nginx后,keepalived2秒内会被自动停掉,VIP释放,由lb02接管,这样就实现了即使服务宕机也会进行IP漂移,业务切换

原文地址:https://www.cnblogs.com/regit/p/8251664.html