linux下用容器搭建nginx负载均衡

Linux 安装 Nginx - docker

转载自::https://www.cnblogs.com/wangwangfei/p/13524070.html

Linux 下安装 Nginx

一、获取镜像

docker pull nginx

二、启动镜像

docker run -p 8888:80 -d nginx

验证:http://xx.xxx.xx.xxxx:8888

三、配置文件外置

在主机上创建目录/disk2/docker/nginx,在该目录下创建conf,logs,www三个目录

在/disk2/docker/nginx/conf目录下新增文件default.conf,写入如下内容,

复制代码

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

复制代码

在/disk2/docker/nginx/www目录下新增文件index.html,写入如下内容:

复制代码

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx in docker!</title>
<body>
<h1>docker!!!!</h1>
</body>
</html>

复制代码

四、挂载外部配置文件启动

 docker run -p 8888:80 -v /disk2/docker/nginx/conf:/etc/nginx/conf.d -v /disk2/docker/nginx/www:/usr/share/nginx/html -v /disk2/docker/nginx/logs:/var/log/nginx -d nginx

验证:http://xx.xxx.xx.xxxx:8888

Nginx负载均衡配置

转载自:https://www.cnblogs.com/liushui0306/p/13186023.html

nginx配置

前面一篇https://www.cnblogs.com/liushui0306/p/13177226.html在/usr/local/nginx/conf目录在nginx.conf配置里面把默认监听端口改成了82

配置单个tomcat:

当我们访问nginx对外端口82时,让nginx指向8081

#配置8081端口
[root@VM_0_11_centos ~]# vim /usr/local/nginx/conf/nginx.conf

复制代码

#把location 这项改成proxy_pass http://49.233.x.x:8081;注意后面有个分号
server {
        listen       82;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://49.233.xx.xx:8081;
            root   html;
            index  index.html index.htm;
        }

复制代码

编辑完成后:wq保存退出,执行reload,重新加载nginx文件

[root@VM_0_11_centos sbin]# ./nginx -s reload

接着在浏览器输入http://49.233.x.x:82 就能访问到8081的tomcat首页了,之前是访问的nginx页面,说明监听成功

负载均衡配置方式

轮询访问

 一台服务配置好了,接着把三个tomcat服务全部放到一块,让访问http://49.233.x.x:81能自动分配到8081、8082、8083这三个服务器上,实现负载均衡.

vim /usr/local/nginx/conf/nginx.conf,将多个服务器ip地址填进去就可以了

复制代码

upstream tomcats {
            server  49.233.xx.xx:8081;
            server  49.233.xx.xx:8082;
            server  49.233.xx.xx:8083;
          }
    server {
        listen       82;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://tomcats;
            root   html;
            index  index.html index.htm;
        }

复制代码

编辑完成后:wq保存退出,执行reload,重新加载nginx文件

[root@VM_0_11_centos ~]# cd /usr/local/nginx/sbin
[root@VM_0_11_centos sbin]# ./nginx -s reload

接着在浏览器输入http://49.233.x.x:82,多次刷新,会发现tomcat首页轮询显示Home8081、Home8082、Home8083

加权轮询

如果3台服务器的性能和配置不一样,我们希望访问某台服务器的权重加大,只需加一个参数就可以

复制代码

 upstream tomcats {
            server  49.233.216.185:8081 weight=2;
            server  49.233.216.185:8082 weight=4;
            server  49.233.216.185:8083 weight=4;
          }
    server {
        listen       82;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://tomcats;
            root   html;
            index  index.html index.htm;
        }

复制代码

跟上面步骤一样,改完之后./nginx -s reload,访问10次,会有4次出现Home8083

ip_hash解决session共享问题

涉及到不同的ip访问多个服务器会出现session不共享的问题,可以加个参数:ip_hash,就可以解决了

复制代码

upstream tomcats {
            ip_hash;
            server  49.233.216.185:8081 weight=2;
            server  49.233.216.185:8082 weight=4;
            server  49.233.216.185:8083 weight=4;
          }

复制代码

设置链接超时

复制代码

location / {
            proxy_pass http://tomcats;
            proxy_connect_timeout 36000s;
            proxy_send_timeout 36000s;
            proxy_read_timeout 36000s;
            root   html;
            index  index.html index.htm;
        }

复制代码

proxy_connect_timeout 为连接应用服务器的超时时间,单位为秒
proxy_send_timeout 为发送请求到应用服务器的超时时间,单位为秒
proxy_read_timeout 为等待应用服务器响应的超时时间,单位为秒

upstream模块

upstream模块主要负责负载均衡的配置,通过默认的轮询调度方式来分发请求到后端服务器,配置参数

  • ip_hash:指定请求调度算法,默认是weight权重轮询调度,可以指定

  • server host:port:分发服务器的列表配置

  • down:表示该主机暂停服务

  • max_fails:表示失败最大次数,超过失败最大次数暂停服务

  • fail_timeout:表示如果请求受理失败,暂停指定的时间之后重新发起请求

复制代码

upstream name
{
ip_hash;
server 192.168.x.xx:8080;
server 192.168.x.xx:8081 down;
server 192.168.x.xx:8082 max_fails=3;
server 192.168.x.xx:8083 fail_timeout=20s;
server 192.168.x.xx:8084 max_fails=3 fail_timeout=20s;
}

复制代码

到此为止负载均衡环境就脚本搭建完成,那么对外只需开放81端口,就可以了,8081,8082,8083这三个服务端口可以关掉,这样用户就感知到是只访问一个服务。
当然这个不是完美的,当81端口这个主机服务挂掉时候,那整个服务就挂了,所以需要有备机服务,在多个机器上搭建备机服务(和主机服务环境一致),同样可以设置权重
这样多个机器,其中某个服务机器挂了也不影响。

Nginx负载均衡+监控状态检测

转载自:https://www.cnblogs.com/DjangoBlog/p/7133174.html

Nginx负载均衡+监控状态检测

想用Nginx或者Tengine替代LVS,即能做七层的负载均衡,又能做监控状态检测,一旦发现后面的realserver挂了就自动剔除,恢复后自动加入服务池里,可以用Tengine的ngx_http_upstream_check_module模块。该模块在Tengine-1.4.0版本以前没有默认开启,它可以在配置编译选项的时候开启:./configure --with-http_upstream_check_module。

Nginx.conf 配置

http {
    upstream fire_server{
    ip_hash;
    server 192.168.1.1:80;
    server 192.168.1.2:80;

    check interval=3000 rise=2 fall=5 timeout=1000 type=http ;
    check_http_send "GET /status.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx ;
    }

    server {
        listen       80;
        server_name  localhost default;

        location / {
            proxy_pass http://fire_server;
            access_log logs/fire_server_access.log main;
            error_log logs/error.log debug;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }   
}
  • check interval 指令可以打开后端服务器的健康检查功能。
指令后面的参数意义是:

interval:向后端发送的健康检查包的间隔。
fall(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down。
rise(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up。
timeout: 后端健康请求的超时时间。
default_down: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的。
type:健康检查包的类型,现在支持以下多种类型
    tcp:简单的tcp连接,如果连接成功,就说明后端正常。
    ssl_hello:发送一个初始的SSL hello包并接受服务器的SSL hello包。
    http:发送HTTP请求,通过后端的回复包的状态来判断后端是否存活。
    mysql: 向mysql服务器连接,通过接收服务器的greeting包来判断后端是否存活。
    ajp:向后端发送AJP协议的Cping包,通过接收Cpong包来判断后端是否存活。
port: 指定后端服务器的检查端口。
  • check_http_send 指令
该指令可以让负载均衡器模拟向后端realserver发送,监控检测的http包,模拟LVS的检测。
  • check_http_expect_alive 指令
check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
返回指定HTTP code,符合预期就算检测成功

RealServer配置

        location = /status.html {
            root html;
            access_log logs/access.log main;
        }

后端realserver配置,只需要保证 curl http://realserver_ip/status.html 能访问到即可。

测试

  • 移除realserver的status.html即可模拟服务不可用,负载均衡器会在N次检测后发现realserver不服务,error_log里会打印。移回status.html即立马恢复服务。
2015/04/04 22:00:42 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80
2015/04/04 22:00:43 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80
2015/04/04 22:00:44 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80
...
enable check peer: 192.168.1.1:80

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些关于使用Docker搭建Nginx负载均衡测试环境的指导。 以下是大致步骤: 1. 安装Docker和Docker Compose。 2. 创建一个新的Docker Compose文件。在该文件中,我们将定义两个服务:一个Nginx服务和一个Web服务。下面是一个简单的示例: ``` version: '3' services: nginx: image: nginx:latest ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro depends_on: - web web: image: nginx:latest ``` 这里我们使用了官方的Nginx镜像,并将其映射到本地的80端口。我们还定义了一个名为web的服务,它将在Nginx之后启动。 3. 创建一个Nginx配置文件。在上一步中,我们将Nginx配置文件挂载到容器中。因此,我们需要创建一个Nginx配置文件nginx.conf,并将其放置在与Docker Compose文件相同的目录中。以下是一个简单的示例配置文件: ``` worker_processes 1; events { worker_connections 1024; } http { upstream web_backend { server web:80; } server { listen 80; location / { proxy_pass http://web_backend; } } } ``` 在这个配置文件中,我们定义了一个名为web_backend的upstream,它将代理到我们定义的web服务。然后,我们创建了一个Nginx服务器,监听80端口,并将所有请求代理到web_backend上。 4. 启动容器。在Docker Compose文件所在的目录中,运行以下命令启动容器: ``` docker-compose up -d ``` 这将启动两个服务,并将它们连接起来。您现在可以通过访问http://localhost来测试负载均衡器。 希望这些指导对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值