Redis connection refused when using traefik
Asked Answered
S

2

1

I'm having a hard time trying to configure one redis container for all my applications using traefik. This is my configuration:

1 - Docker compose for Traefik and Redis: version: '2'

services:
  proxy:
    container_name: traefik
    image: traefik:1.3.6-alpine
    command: --docker
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
    networks:
      - proxy
    labels:
      - traefik.frontend.rule=Host:monitor.company.dev
      - traefik.port=8080

redis:
    container_name: main_redis
    image: redis:3.2
    restart: always
    volumes:
      - ./data/redis:/data
    networks:
      - proxy
    labels:
      - traefik.backend=main-redis
      - traefik.default.protocol=http
      - traefik.frontend.rule=Host:main-redis.company.dev
      - traefik.docker.network=proxy
      - traefik.port=6379

networks:
  proxy:
    external: true

2 - Docker compose for my PHP Application.

version: '2'

services:
  ...


  php:
    container_name: myapp_php
    build: ./docker/php # php:7.1-fpm base image

    networks:
      - internal
      - proxy
    labels:
      - traefik.enable=false
      - traefik.docker.network=proxy
    expose:
      - 9000

networks:
  proxy:
    external: true
  internal:
    external: false

I tried to connect my php application to main-redis.company.dev on both ports 6379 and 80 but I get a Redis::connect(): connect() failed: Connection refused message.

I also changed these stuff in my redis.conf: Commented the line with bind 127.0.0.1 And changed protected-mode to no

My docker containers are staying in the same network, so I think it should work. Anyone knows why am I having this problem?

Sitra answered 3/11, 2017 at 17:37 Comment(2)
so what's happening? You should make a clear question :)Exponible
I'm getting a "Connection refused" error when trying to connect from my app to the main redis.Sitra
P
2

First off, remove the traefik labels from your redis service definition, traefik is currently (Nov 2017) a HTTP proxy, so you can't expose the endpoint like that

See here:

https://github.com/containous/traefik/issues/10

https://github.com/containous/traefik/issues/1611

Then to connect the php service to the redis service, that looks like you are trying to do that within the same docker instance (rather than externally)

Instead of main-redis.company.dev:6379, it should be like on of these:

  • redis:6379
  • main_redis:6379
  • %PROJECT_NAME%_redis:6379

depending upon how you are deploying the container

Peregrinate answered 26/11, 2017 at 16:26 Comment(0)
D
3

2022 UPDATE to @djeeg answer

For some time now you can use TCP mode for your routers. To do that you need to define the labels with TCP instead of HTTP

    labels:
      - "traefik.enable=true"
      - "traefik.tcp.routers.redis.rule=HostSNI(`redis.example.com`)"
      - "traefik.tcp.routers.redis.entrypoints=redis" //6379 entrypoint
      - "traefik.tcp.routers.redis.tls.certresolver=myresolver" //let's encrypt resolver
      - "traefik.tcp.routers.redis.service=redis"
      - "traefik.tcp.services.redis.loadbalancer.server.port=6379"

once you got that working in order to connect assuming you are using TSL you will get error Error: Protocol error, got "H" as reply type byte.

To prevent this you need to do things.

  1. Allow for tls connection in the connection string
  2. Setup SNI for your DNS name or provide public certificate file via cert pr cacert
redis-cli -u redis://redis.example.com:6379 --tls --sni redis.example.com
Disqualification answered 12/7, 2022 at 11:25 Comment(0)
P
2

First off, remove the traefik labels from your redis service definition, traefik is currently (Nov 2017) a HTTP proxy, so you can't expose the endpoint like that

See here:

https://github.com/containous/traefik/issues/10

https://github.com/containous/traefik/issues/1611

Then to connect the php service to the redis service, that looks like you are trying to do that within the same docker instance (rather than externally)

Instead of main-redis.company.dev:6379, it should be like on of these:

  • redis:6379
  • main_redis:6379
  • %PROJECT_NAME%_redis:6379

depending upon how you are deploying the container

Peregrinate answered 26/11, 2017 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.