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

Posted 坚强的小蚂蚁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx负载均衡配合keepalived服务案例实战相关的知识,希望对你有一定的参考价值。

 

 

本实验用4台虚拟机,2台做负载均衡,2台做web服务器

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

 

一.  在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.etiantian.org;
        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.etiantian.org,网页应该显示www.etiantian.org237/238

本实验在web服务器上配置了2个虚拟主机,如果不加这个命令,就把节点服务器的第一个虚拟主机发给反向代理,也就是网页显示bbs.etiantian.org238/237,显然是错误的

2. proxy_set_header X-Forwarded-For $remote_addr;

此命令与web1/2服务器上虚拟主机的访问日志有关,也就是 /application/nginx/logs/access_www.log

如果使用192.168.0.230作为客户端进行测试时,不加这个命令不能显示客户端访问IP 192.168.0.230. 这个命令要在web服务器上开启log_format功能。日志的最后一个字段显示的是客户端访问IP

 

 

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

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

! Configuration File for keepalived

global_defs {
   notification_email {
     1816635208@qq.com
   }
   notification_email_from [email protected]
   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 [email protected]
   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

    }
}

 

 

三. 配置基于域名虚拟主机的WEB节点

1.  在web1和web2上配置nginx如下,推荐方法 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.etiantian.org;
        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.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;

    }
       access_log logs/access_www.log main;
   }
}

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

 

2. 创建站点目录及对应的测试文件,我把nginx安装在/application目录

web1 # for n in bbs www; do cd /application/nginx/html/; mkdir -p $n; echo "$n.etiantian.org237" > $n/index.html; done

cat bbs/index.html

bbs.etiantian.org237

cat www/index.html

www.etiantian.org237

结尾加上237来区分对应的web节点机器,在web2上做相同操作,把237改成238

 


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

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx -s reload

netstat -lntup | grep 80

 

4. 把域名加入hosts解析,本机进行访问测试

web1: echo "192.168.0.237 www.etiantian.org" >> /etc/hosts; echo "192.168.0.237 bbs.etiantian.org" >> /etc/hosts

检查虚拟主机配置结果:

curl www.etiantian.org 输出结果应该为www.etiantian.org237

curl bbs.etiantian.org 输出结果应该为 bbs.etiantian.org237

在web2上做相同测试。

 


以上是关于nginx负载均衡配合keepalived服务案例实战的主要内容,如果未能解决你的问题,请参考以下文章

分布式架构高可用架构篇_04_Keepalived+Nginx实现高可用Web负载均衡

Nginx+Keepalived+Tomcat+MySQL负载均衡& 通过nginx调度器访问Tom

Nginx负载均衡+keepalived高可用

Nginx在linux环境下(centos7)的安装负载均衡设置

Nginx LB+keepalived+varnish+NAMP架构部署wordpress实现动静分离

Nginx与keepalived实现负载均衡