如何在主动 - 被动设置中使用故障切换将两个Nginx容器设置为反向代理? [英] How to set up two Nginx containers as a reverse proxy in an active-passive set up with failover?

查看:160
本文介绍了如何在主动 - 被动设置中使用故障切换将两个Nginx容器设置为反向代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Linux-EC2服务器上设置了一个Nginx容器。我的Nginx配置文件如下:

  server {
listen 80;
server_name client-dev.com;
位置/ {
proxy_pass http://dev-client.1234.io:5001/;
proxy_redirect关闭;

## proxy_set_header Host $ host;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;

client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
server {
listen 80;
server_name client-test.com;
位置/ {
proxy_pass http://test-client-1234.io:5005/;
proxy_redirect关闭

## proxy_set_header Host $ host;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;

client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}

它将具有不同端口的请求传递到端口80。现在,我需要一个冗余的Nginx处于主动被动模式。如果Nginx容器掉线/停止。



为此,需要在同一个服务器中设置另一个Nginx容器?如果是这样,应如何设置自动进行故障切换?



我看过上游选项,但正如我发现的,这种情况不会奏效。我拥有的proxy_pass是外部和动态的,我使用docker-cloud的脚本来获取它们。



另外还有一种名为docker-gen的方法,不知道有多少有用的,如果有的话,我更喜欢使用另一种方式?



任何帮助都不会赞赏。

解决方案

我可以想到以下选项:


  1. Kubernetes:您可以为您的nginx设置创建一个部署,并使用活动度探测器。 Kubernetes将使用您提供的http请求/间隔来探测nginx容器,如果pod不健康,则会被杀死并重新创建。在您的kubernetes集群中使用多个节点,您甚至可以从节点故障中减轻。


  2. Docker Swarm:使用具有多个节点的docker swarm模式,可以缓解节点故障,但nginx的健康状况应该由外部自定义脚本检查,可以使用 bash curl


  3. 使用keepalived独立主机:这是使用keepalived的传统的nginx主动/被动集群。你也可以使用这个与码头,但它将是脏的。因为所有的容器在一个主机上将被动。



I have set up a Nginx container on a Linux-EC2 server. My Nginx config file is as follows:

server {
    listen      80;
    server_name     client-dev.com;
    location / {
        proxy_pass         http://dev-client.1234.io:5001/;
        proxy_redirect     off;

        ##proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}
server {
    listen      80;
    server_name     client-test.com;
    location / {
        proxy_pass         http://test-client-1234.io:5005/;
        proxy_redirect     off;

        ##proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}

It passes the requests with different ports to port 80. Now, I need to make a redundant Nginx in an active-passive mode. In case, if the Nginx container goes down/stops.

To do so, would I need to set another Nginx container in the same server? If so, how should it be setup to do the failover automatically?

I have looked at "upstream" option, but as I found, it would not work for this case. The proxy_pass that I have are external and dynamic that I get them using a script from docker-cloud.

There is another way named "docker-gen" however, I'm not sure how much useful it would be, and I prefer to use another way if there is any?

Any help would be appreciated.

解决方案

I can think of following options:

  1. Kubernetes: You can create a deployment for your nginx setup and use liveliness probes. Kubernetes will probe the nginx container with http request/interval you provide, if the pod is not healthy, it will be killed and recreated. Using multi nodes in your kubernetes cluster you can even mitigate from node failure.

  2. Docker Swarm: Using docker swarm mode with multiple nodes, you can mitigate from node failure, but nginx health should be checked by an external custom script that can be done with bash and curl.

  3. Using standalone hosts with keepalived: This is traditional nginx active/passive cluster using keepalived. You can also use this with docker but it would be dirty. because all of your containers will be passive on one host.

这篇关于如何在主动 - 被动设置中使用故障切换将两个Nginx容器设置为反向代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆