0

I have a following scenario in the k8s cluster.

  1. AWS managed redis cluster which is exposed by a upstream service called redis. I have opened a tunnel locally using kube-proxy.
curl 127.0.0.1:31997/api/v1/namespaces/intekspersistence/services/redis
{
  "kind": "Service",
  "apiVersion": "v1",
  "metadata": {
    "name": "redis",
    "namespace": "intekspersistence",
    "selfLink": "/api/v1/namespaces/intekspersistence/services/redis",
    ...
  "spec": {
    "type": "ExternalName",
    "sessionAffinity": "None",
    "externalName": "xxx.xxx.usw2.cache.amazonaws.com"
  },
  "status": {
    "loadBalancer": {

    }
  }

As shown, I am able to route to the redis service locally and it's poinintg to the Actual redis host. Now I am trying to validate and ping the redis host using the below python script.

from redis import Redis
import logging

logging.basicConfig(level=logging.INFO)
redis = Redis(host='127.0.0.1:31997/api/v1/namespaces/intekspersistence/services/redis')

if redis.ping():
    logging.info("Connected to Redis")

Upon running this, It's throwing error as host not found. [Probably due to inclusion of port in the host].

python test.py
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    if redis.ping():
  File "/home/appsadm/.local/lib/python2.7/site-packages/redis/client.py", line 1378, in ping
    return self.execute_command('PING')
  File "/home/appsadm/.local/lib/python2.7/site-packages/redis/client.py", line 898, in execute_command
    conn = self.connection or pool.get_connection(command_name, **options)
  File "/home/appsadm/.local/lib/python2.7/site-packages/redis/connection.py", line 1192, in get_connection
    connection.connect()
  File "/home/appsadm/.local/lib/python2.7/site-packages/redis/connection.py", line 563, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error -2 connecting to 127.0.0.1:31997/api/v1/namespaces/intekspersistence/services/redis:6379. Name or service not known.

Is there a workaround to trim the port from host?? Or to route to the host using the above python module.

Rajiv Rai
  • 235
  • 4
  • 16
  • What you are accessing via curl appears to be the K8s API and not redis. The `host` arg to the redis object should be a hostname only, not a full path with port. It's very strange to see a path like that for redis (which doesn't use http). – jordanm Aug 25 '21 at 14:00
  • Also, ExternalName services do not do routing. They simply tell kube-dns to override what the real DNS is. No routing involved. – jordanm Aug 25 '21 at 14:02
  • But the requirement is to cover all the environments, i.e. redis hostname can't be known at build time, and i have to make sure if the cluster exists while deployment. – Rajiv Rai Aug 25 '21 at 14:07
  • Personally, the way I solve that issue is I have the terraform that creates the elasticache instance also create a configmap inside the cluster that contains the connection information. I then export that configmap as env variables to the relevant pods. – jordanm Aug 25 '21 at 14:23
  • While you can make ExternalName work for unecrypted redis, it will not work properly for anything that uses SSL due to hostname differences. – jordanm Aug 25 '21 at 14:24

0 Answers0