cloudfoundry-community-attic / cf-services-contrib-release

release repository for community contributed services
Apache License 2.0
46 stars 60 forks source link

Redis service not getting connected from sample application at port 5000 #161

Open Gangabhavani opened 9 years ago

Gangabhavani commented 9 years ago

Hi, I have a running Bosh-lite cloud-foundry-instance.To test services,I have written a sample python application to connect to redis service, which has been created as said in the link. https://github.com/cloudfoundry-community/cf-services-contrib-release

my sample application(testredis) looks like

Get Redis credentials

if 'VCAP_SERVICES' in os.environ: services = json.loads(os.environ['VCAP_SERVICES'])['redis-2.6'][0] redis_env = services['credentials'] print redis_env else: redis_env = dict(hostname='localhost', port=6379, password='') myredis_host = redis_env['hostname'] myredis_port = int(redis_env['port']) myredis_pwd = redis_env['password'] myredis_env = dict(host=myredis_host, port=myredis_port, password=myredis_pwd) print myredis_env

Connect to redis

try: r = redis.Redis(myredis_env) r.info() except redis.ConnectionError as erdesc: r = None print "Not connected to Redis... " , erdesc @app.route('/') def keys(): if r: current_hits = r.incr('hits') return 'Hits: {}\n'.format(current_hits) + 'Available Keys: ' + str(r.keys()) else: return 'No Redis connection available!' @app.route('/') def get_current_values(key): if r: current_values = r.lrange(key, 0, -1) return str(current_values) else: abort(503) @app.route('//') def add_value(key, s): if r: r.rpush(key, s) return 'Added {} to {}.'.format(s, key) else: abort(503) if name == 'main**':

Run the app, listening on all IPs with our chosen port number

app.run(host='0.0.0.0', port=port)

The application manifest contains the binding details of redis instance like

applications:

  • name: testredis

    memory: 128MB

    disk_quota: 256MB

    random-route: true

    command: python main.py

    services:

    • myredis

The application has binded successfully with redis and got the details as follows

cf services Getting services in org vedams / space cfspace as admin... OK

name service plan bound apps last operation
mymongo mongodb default create succeeded
mypgsql postgresql default create succeeded
myredis redis default testredis2, testredis create succeeded

cf env testredis Getting env variables for app testredis2 in org vedams / space cfspace as admin... OK

System-Provided: { "VCAP_SERVICES": { "redis-2.6": [ { "credentials": { "host": "10.244.1.94", "hostname": "10.244.1.94", "name": "7715b1bc-4d0c-46ae-9159-9016a0dd6756", "password": "f6aa70e6-2b7b-42bd-8e54-0f7629fbfb89", "port": 5000 }, "label": "redis-2.6", "name": "myredis", "plan": "default", "tags": [ "key-value", "nosql" ] } ] } }

{ "VCAP_APPLICATION": { "application_id": "413e5e12-1fdc-4ecb-b86b-a33d61ca9115", "application_name": "testredis2", "application_uris": [ "testredis2-unpenetrant-urease.10.244.0.34.xip.io" ], "application_version": "d297bf90-bd11-4e57-bbb5-e9be76778d61", "limits": { "disk": 256, "fds": 16384, "mem": 128 }, "name": "testredis2", "space_id": "8e39e3e3-12b7-48de-ae89-5d7af39fd77b", "space_name": "cfspace", "uris": [ "testredis2-unpenetrant-urease.10.244.0.34.xip.io" ], "users": null, "version": "d297bf90-bd11-4e57-bbb5-e9be76778d61" } }

No user-defined env variables have been set

No running env variables have been set

No staging env variables have been set

The application is running successfully but not connected to redis instance from DEA.

Getting error in app logs like connection refused to redis.

What could be the problem?

rkoster commented 9 years ago

You have to make sure access to the services network is allowed by the cf security groups. In the integrations specs this is done here. @Gangabhavani if you get it to work could you adapt the documentation from here to the cf-services-contrib repo and make a PR for it?