首页 > 解决方案 > Nginx 负载均衡器在 Docker 中运行的高可用性

问题描述

我正在运行我的后端 Web 服务器的 nginx 负载平衡器容器,

由于我只有一个容器作为 nginx 负载均衡器运行,因此在容器死亡/崩溃的情况下,客户端无法访问网络服务器,

下面是 nginx.conf

events {}
http {
  upstream backend {
      server 1.18.0.2;
      server 1.18.0.3;
   }

   # This server accepts all traffic to port 80 and passes it to the upstream.
   # Notice that the upstream name and the proxy_pass need to match.

   server {
      listen 80;

      location / {
          proxy_pass http://backend;
      }
   }
}

下面是 Dockerfile:

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

我正在运行如下所示的 nginx 容器

docker run -d -it -p 8082:80 nginx-ls

我正在使用 http://serve-ip:8082 访问上面的服务器

容器可能随时崩溃或死机,在这种情况下,我的应用程序无法访问,所以我尝试运行像下面这样的另一个容器,但由于我使用相同的端口而出现以下错误,并且我们无法在同一主机中重新使用相同的端口.

docker run -d -it -p 8082:80 nginx-ls

06a20239bd303fab2bbe161255fadffd5b468098424d652d1292974b1bcc71f8
docker: Error response from daemon: driver failed programming external connectivity on endpoint 
suspicious_darwin (58eeb43d88510e4f67f618aaa2ba06ceaaa44db3ccfb0f7335a739206e12a366): Bind for 
0.0.0.0:8082 failed: port is already allocated.

所以我在不同的端口运行,它工作正常

 docker run -d -it -p 8083:80 nginx-ls

但是当容器 8082 关闭时,我们如何告诉/配置客户端使用端口 8083 容器

还是有其他最佳方法来实现具有高可用性的 nginx 负载均衡器?

注意:由于某些原因,我不能使用 docker-compose

标签: linuxdockernginxload-balancingnginx-reverse-proxy

解决方案


推荐阅读