Search code examples
redisopenshiftopenshift-3

OpenShift v3 connect app with redis. Connection Refused


I have created a redis 3.2 application from the default image catalog.

I'm trying to connect a python app that runs inside the same project with the redis db.

This is what the Python application uses to connect to redis:

REDIS_HOST = 'localhost'
REDIS_PORT = 6379
REDIS_PASSWORD = os.environ.get('REDIS_PASSWORD') or 'test'


redis = aioredis.create_redis_pool(
    (REDIS_HOST, int(REDIS_PORT)),
    password=REDIS_PASSWORD,
    minsize=5,
    maxsize=10,
    loop=loop,
)

The deployment fails with an ConnectionRefusedError: [Errno 111] Connection refused.

My guess is that I need to use another value for REDIS_HOST, but I couldn't figure what to use.

Does anyone know how to fix this?


Solution

  • After you deployed from the image catalog a number of objects will have been created for you. One of those objects is a service, which is used to load balance requests to the Pods it fronts. Service names for a project can be retrieved using the client tools via oc get svc.

    This service name should be used to connect to your redis instance. If you deploy redis before your Python application, some environment variables should already be populated which can be used, for example REDIS_SERVICE_HOST and REDIS_SERVICE_PORT.

    So from your application you can connect via the service ip or service name, where service name is redis then redis.StrictRedis(host='redis', port=6379, password='secret')

    The redis password may have been generated for you. In that case it is retrievable from the redis secret which could also be mounted from your python app