(NGINX LB + docker-compose)停止1个服务,现在只使用另一个

我在Docker中使用NGINX,并使用2节点服务。

负载平衡正在工作。 不知道这是怎么回事,但是我的页面加载到ping1,然后从服务ping2加载CSS文件,然后从ping1加载下一个文件,…我认为这主要是一个完整的页面加载从ping1和从ping2下一个。

哪一个更标准?

这里是docker-compose.yml

version: "2" services: ping1: ports: - "80" build: context: ./1 dockerfile: Dockerfile networks: - front-tier ping2: build: context: ./1 dockerfile: Dockerfile networks: - front-tier nginx: build: ./nginx ports: - "80:80" networks: - front-tier networks: front-tier: driver: bridge 

至于我的第二个问题,我试图想象如何使用Jenkins来取消ping2,更新它,然后把它提出来,然后对ping1做同样的事情。

现在我只是手动testing,并使用

 docker-compose stop ping2 

服务停止运行,但是nginx需要一段时间才能实现,并通过ping1进行路由。

我在Chrome上加载80端口,第一个请求是通过ping1的页面加载,第二个是CSS文件,这将是ping2,从ping1加载需要18-90秒之间的任何时间,只是说整个时间“待定”

NGINX错误

我如何解决这个问题,在路由到上游之前检查,如果它是“健康的” ,也许通过我手动设置端点?

这里是nginx.conf

 events { worker_connections 20000; use epoll; multi_accept on; } http { upstream ping { server ping1:80 max_fails=1 fail_timeout=1s; server ping2:80 max_fails=1 fail_timeout=1s; } limit_req_zone $binary_remote_addr zone=one:10m rate=18000r/m; limit_conn_zone $binary_remote_addr zone=addr:10m; keepalive_timeout 65; keepalive_requests 100000; sendfile on; tcp_nopush on; tcp_nodelay on; client_body_buffer_size 128k; client_max_body_size 10m; client_header_buffer_size 1k; large_client_header_buffers 4 4k; output_buffers 1 32k; postpone_output 1460; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 5; open_file_cache_errors off; server { listen 80; server_tokens off; gzip on; gzip_disable "MSIE [1-6]\."; gzip_comp_level 5; gzip_vary on; gzip_min_length 1000; gzip_proxied any; gzip_types text/html application/x-javascript text/css application/javascript text/javascript text/plain text/xml application/json application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf application/xml font/eot font/opentype font/otf image/svg+xml image/vnd.microsoft.icon; gzip_buffers 16 8k; location / { limit_req zone=one; limit_conn addr 10; proxy_pass http://ping/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_set_header Connection ""; proxy_connect_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_temp_path /etc/nginx/proxy_temp; proxy_send_timeout 600; proxy_read_timeout 600; } location /stats { stub_status on; allow all; } } } 

我的页面加载到ping1,然后从服务ping2加载CSS文件,然后从ping1下一个文件,…我认为这主要是从ping1和下一个从ping2一个整页加载。

这是因为默认方法是循环法。

请参阅http://nginx.org/en/docs/http/load_balancing.html 。 尤其是:

nginx支持以下负载均衡机制(或​​方法):

  • 循环 – 对应用程序服务器的请求以循环方式分发,
  • 最less连接 – 下一个请求被分配给活动连接数最less的服务器,
  • ip-hash – 哈希函数用于确定下一个请求(基于客户端的IP地址)应该select哪个服务器。

[…]

当没有专门configuration负载均衡的方法时,默认为round-robin。

[…]

要configurationip-hash负载平衡,只需将ip_hash指令添加到服务器(上游)组configuration:

 upstream myapp1 { ip_hash; server srv1.example.com; server srv2.example.com; server srv3.example.com; } 

至于我的第二个问题,我试图想象如何使用Jenkins来取消ping2,更新它,然后把它提出来,然后对ping1做同样的事情。

不需要按照这个顺序来做。 所有你需要的是一个命令:

 docker-compose up --build -d ping2 

(然后重复ping1)

我相信,不会停止容器,直到图像build成后,在这一点上,它将停止并立即重新创build它。

我不知道为什么nginx挂了这么久,但是使用ip_hash应该避免发生在页面中间,使用上面的docker-compose命令应该使停机时间极less。