问题
I have 2 springboot applications running on ports 9000 and 9001. I have also run HaProxy using a docker container. My config file is as follows:
global
defaults
mode http
timeout connect 5000ms
timeout client 5000ms
timeout server 5000ms
frontend http-in
bind *:80
acl has_web1 path_beg /web1
acl has_web2 path_beg /web2
use_backend web1 if has_web1
use_backend web2 if has_web2
default_backend web1
backend web1
server web1 127.0.0.1:9000 check
backend web2
server web2 127.0.0.1:9001 check
When I try to access a url, say localhost/web1, it is giving error code 503.
Can someone tell me why is it like this? The docker compose file is:
version: '3'
services:
#web1:
# image: dockercloud/hello-world
# container_name: web1
# ports:
# - "81:80"
#web2:
# image: dockercloud/hello-world
# container_name: web2
# ports:
# - "82:80"
haproxy:
build: ./haproxy
container_name: haproxy
ports:
- "80:80"
And the Dockerfile is:
FROM haproxy:1.7
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
I am working on Windows, if that matters.
Edit: I switched the version of HaProxy to 2.1.2 and got the following warnings:
haproxy | [NOTICE] 020/002817 (1) : New worker #1 (6) forked
haproxy | [WARNING] 020/002817 (6) : Server web1/web1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
haproxy | [ALERT] 020/002817 (6) : backend 'web1' has no server available!
haproxy | [WARNING] 020/002818 (6) : Server web2/web2 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
haproxy | [ALERT] 020/002818 (6) : backend 'web2' has no server available!
Is it because of the docker container?? I am running it using the commaand
docker-compose up --build
Edit:
Changed my docker compse file to this:
version: '3'
services:
#web1:
# image: dockercloud/hello-world
# container_name: web1
# ports:
# - "81:80"
#web2:
# image: dockercloud/hello-world
# container_name: web2
# ports:
# - "82:80"
haproxy:
build: ./haproxy
container_name: haproxy
ports:
- "80:80"
extra_hosts:
- "dockerhost:<My IP>"
and config file to this:
global
defaults
mode http
timeout connect 5000ms
timeout client 5000ms
timeout server 5000ms
frontend http-in
bind *:80
acl has_web1 path_beg /web1
acl has_web2 path_beg /web2
use_backend web1 if has_web1
use_backend web2 if has_web2
default_backend web1
backend web1
server web1 dockerhost:9000 check
backend web2
server web2 dockerhost:9001 check
Now the errors are gone, but I'm able to access only localhost, not localhost/web1, localhost/web2 or any other. But the backends are being formed as there is no error or warning in the console. The error code I'm getting is 404.
来源:https://stackoverflow.com/questions/59947463/haproxy-configuration-file