I want to use caddy as a API gateway in docker swarm, but It seems caddy (or some other package) will cache the proxy destination dns lookup resault for several minutes.
caddy -version)?0.9.5
This is my docker-compose file:
version: '3'
services:
app:
image: muninn/caddy-microservice:app
ports:
- 12345:12345
deploy:
replicas: 3
gateway:
image: muninn/caddy-microservice:gateway
ports:
- 2015:2015
depends_on:
- app
deploy:
replicas: 1
placement:
constraints: [node.role == manager]
I use "docker stack deploy -c docker-compose.yml caddy" to start it,
and check if caddy can route http request to a random backend app container.
:2015 {
proxy / app:12345
}
Dockerfile:
FROM alpine
MAINTAINER Muninn <[email protected]>
LABEL caddy_version="0.9.5" architecture="amd64"
ARG plugins=cors
RUN apk add --no-cache openssh-client git tar curl
RUN curl --silent --show-error --fail --location \
--header "Accept: application/tar+gzip, application/x-gzip, application/octet-stream" -o - \
"https://caddyserver.com/download/build?os=linux&arch=amd64&features=${plugins}" \
| tar --no-same-owner -C /usr/bin/ -xz caddy \
&& chmod 0755 /usr/bin/caddy \
&& /usr/bin/caddy -version
EXPOSE 80 443 2015
VOLUME /root/.caddy
WORKDIR /www
COPY Caddyfile /etc/Caddyfile
ENTRYPOINT ["/usr/bin/caddy"]
CMD ["--conf", "/etc/Caddyfile", "--log", "stdout"]
curl http://my-host-ip:2015
I expect that caddy can balance requests to all backend apps.
I published my demo to docker cloud, so you can just clone the demo and run
or just copy the compose file above and run
docker-compose pull
docker stack deploy -c docker-compose.yml caddy
oh, you need to have docker engine 1.13 higher and switch it to swarm mode.
What if you set keepalive 0 inside proxy and try again?
@mholt Set keepalive 0 works, I'll try concurrent requests to caddy when keepalive is set to default. Perhaps everything goes well.
If that's the case, I suspect that is just Caddy reusing the connection it first opened, so no DNS lookups are being performed anyway; if this is true, it has nothing to do with DNS and is just a keepalive tweak to make in your config. Let me know if you still experience problems with keepalives disabled!
Thank you for your great work.
I tested it with a large number concurrent requests just now. All went well.
I'll use caddy instead of nginx in my project.