2017-05-13 7 views
0

Ich habe einen Docker-Schwarm-Modus mit einem HAProxy-Container und 3 Python-Web-Anwendungen. Der Container mit HAProxy ist Port 80 offen und sollte die 3 Container meiner App ausgleichen (von leastconn).Load Balance Docker Schwarm

Hier ist meine docker-compose.yml Datei:

version: '3' 

services: 
    scraper-node: 
    image: scraper 
    ports: 
     - 5000 
    volumes: 
     - /profiles:/profiles 
    command: > 
     bash -c " 
     cd src; 
     gunicorn src.interface:app \ 
      --bind=0.0.0.0:5000 \ 
      --workers=1 \ 
      --threads=1 \ 
      --timeout 500 \ 
      --log-level=debug \ 
     " 
    environment: 
     - SERVICE_PORTS=5000 
    deploy: 
     replicas: 3 
     update_config: 
     parallelism: 5 
     delay: 10s 
     restart_policy: 
     condition: on-failure 
     max_attempts: 3 
     window: 120s 
    networks: 
     - web 

    proxy: 
    image: dockercloud/haproxy 
    depends_on: 
     - scraper-node 
    environment: 
     - BALANCE=leastconn 
    volumes: 
     - /var/run/docker.sock:/var/run/docker.sock 
    ports: 
     - 80:80 
    networks: 
     - web 

networks: 
    web: 
    driver: overlay 

Wenn ich diesen Schwarm einsetzen (docker stack deploy --compose-file=docker-compose.yml scraper) ich alle meine Container erhalten:

CONTAINER ID IMAGE    COMMAND     CREATED  STATUS   PORTS      NAMES 
245f4bfd1299 scraper:latest  "/docker-entrypoin..." 21 hours ago Up 19 minutes 80/tcp, 5000/tcp, 8000/tcp scraper_scraper-node.3.iyi33hv9tikmf6m2wna0cypgp 
995aefdb9346 scraper:latest  "/docker-entrypoin..." 21 hours ago Up 19 minutes 80/tcp, 5000/tcp, 8000/tcp scraper_scraper-node.2.wem9v2nug8wqos7d97zknuvqb 
a51474322583 scraper:latest  "/docker-entrypoin..." 21 hours ago Up 19 minutes 80/tcp, 5000/tcp, 8000/tcp scraper_scraper-node.1.0u8q4zn432n7p5gl93ohqio8e 
3f97f34678d1 dockercloud/haproxy "/sbin/tini -- doc..." 21 hours ago Up 19 minutes 80/tcp, 443/tcp, 1936/tcp scraper_proxy.1.rng5ysn8v48cs4nxb1atkrz73 

Und wenn ich den haproxy Container angezeigt werden einloggen es sieht aus wie er erkennt die 3 Python-Container:

INFO:haproxy:dockercloud/haproxy 1.6.6 is running outside Docker Cloud 
INFO:haproxy:Haproxy is running in SwarmMode, loading HAProxy definition through docker api 
INFO:haproxy:dockercloud/haproxy PID: 6 
INFO:haproxy:=> Add task: Initial start - Swarm Mode 
INFO:haproxy:=> Executing task: Initial start - Swarm Mode 
INFO:haproxy:==========BEGIN========== 
INFO:haproxy:Linked service: scraper_scraper-node 
INFO:haproxy:Linked container: scraper_scraper-node.1.0u8q4zn432n7p5gl93ohqio8e, scraper_scraper-node.2.wem9v2nug8wqos7d97zknuvqb, scraper_scraper-node.3.iyi33hv9tikmf6m2wna0cypgp 
INFO:haproxy:HAProxy configuration: 
global 
    log 127.0.0.1 local0 
    log 127.0.0.1 local1 notice 
    log-send-hostname 
    maxconn 4096 
    pidfile /var/run/haproxy.pid 
    user haproxy 
    group haproxy 
    daemon 
    stats socket /var/run/haproxy.stats level admin 
    ssl-default-bind-options no-sslv3 
    ssl-default-bind-ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DHE-DSS-AES128-SHA:DES-CBC3-SHA 
defaults 
    balance leastconn 
    log global 
    mode http 
    option redispatch 
    option httplog 
    option dontlognull 
    option forwardfor 
    timeout connect 5000 
    timeout client 50000 
    timeout server 50000 
listen stats 
    bind :1936 
    mode http 
    stats enable 
    timeout connect 10s 
    timeout client 1m 
    timeout server 1m 
    stats hide-version 
    stats realm Haproxy\ Statistics 
    stats uri/
    stats auth stats:stats 
frontend default_port_80 
    bind :80 
    reqadd X-Forwarded-Proto:\ http 
    maxconn 4096 
    default_backend default_service 
backend default_service 
    server scraper_scraper-node.1.0u8q4zn432n7p5gl93ohqio8e 10.0.0.5:5000 check inter 2000 rise 2 fall 3 
    server scraper_scraper-node.2.wem9v2nug8wqos7d97zknuvqb 10.0.0.6:5000 check inter 2000 rise 2 fall 3 
    server scraper_scraper-node.3.iyi33hv9tikmf6m2wna0cypgp 10.0.0.7:5000 check inter 2000 rise 2 fall 3 
INFO:haproxy:Launching HAProxy 
INFO:haproxy:HAProxy has been launched(PID: 12) 
INFO:haproxy:===========END=========== 

Aber wenn ich zu GET-http://localhost versuchen bekomme ich eine Fehlermeldung:

<html> 
    <body> 
    <h1>503 Service Unavailable</h1> 
    No server is available to handle this request. 
    </body> 
</html> 

Antwort

1

Es gibt zwei Probleme:

  1. Die command in docker-compose.yml Datei eine Zeile sein sollte.
  2. Das scraper Bild sollte Port 5000 (in seinem Dockerfile) freilegen.

Sobald ich diejenigen zu beheben, ich diesen Schwarm auf die gleiche Weise einsetzen (mit stack) und die proxy Behälter der Python-Container erkennen und konnten Gleichgewicht zwischen ihnen laden.

0

A 503 Fehler bedeutet in der Regel einen Gesundheitscheck an den Back-End-Server ist fehlgeschlagen.

Ihre Statistikseite könnte hier hilfreich sein: Wenn Sie die Maus über die LastChk Spalte von einem Ihrer Server-Backend DOWN Sie HAProxy eine vage Zusammenfassung geben, warum das Server DOWN: enter image description here

Es ist nicht sieht Wie Sie den Gesundheitscheck (option httpchk) für Ihr default_service Backend konfiguriert haben: Können Sie irgendeinen Ihrer Backend-Server direkt erreichen (zB curl --head 10.0.0.5:5000)? Von the HAProxy documentation:

[R] AHMEN 2xx und 3xx sind gültig betrachtet, während alle anderen solche mit einem Serverausfall zeigen, das Fehlen einer Antwort einschließlich.

Verwandte Themen