bitnami / charts

Bitnami Helm Charts
https://bitnami.com
Other
9.06k stars 9.23k forks source link

[bitnami/redis-cluster] how can I make access to k8s redis cluster with Python and Golang? #5576

Closed shibuiwilliam closed 3 years ago

shibuiwilliam commented 3 years ago

Hi,

I am trying to make a connection to k8s redis cluster with Python and Golang. I have difficulty connecting to the cluster. Does anybody know how to connect and send set/get to the redis cluster via Python? Any suggestion?

This is my current redis cluster.

$ kubectl get pods,deploy,statefulset,svc
NAME                                     READY   STATUS      RESTARTS   AGE
pod/python                               1/1     Running     0          82m
pod/redis-cluster-0                      1/1     Running     0          11m
pod/redis-cluster-1                      1/1     Running     0          11m
pod/redis-cluster-2                      1/1     Running     0          11m
pod/redis-cluster-3                      1/1     Running     0          11m
pod/redis-cluster-4                      1/1     Running     0          11m
pod/redis-cluster-5                      1/1     Running     0          11m
pod/redis-cluster-cluster-create-vrc56   0/1     Completed   0          11m

NAME                             READY   AGE
statefulset.apps/redis-cluster   6/6     11m

NAME                             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)              AGE
service/kubernetes               ClusterIP   10.50.128.1    <none>        443/TCP              24d
service/redis-cluster            ClusterIP   10.50.132.73   <none>        6379/TCP             11m
service/redis-cluster-headless   ClusterIP   None           <none>        6379/TCP,16379/TCP   11m

Python connection with rediscluster.

from rediscluster import RedisCluster

host = "redis-cluster.default.svc.cluster.local"
port = 6379
password = "******"

startup_nodes = [{"host": host, "port": port, "password": password}]
host_port_remap = [
    {
        "from_host": "redis-cluster-0",
        "from_port": 6379,
        "to_host": "127.0.0.1",
        "to_port": 6379,
    },
    {
        "from_host": "redis-cluster-1",
        "from_port": 6379,
        "to_host": "127.0.0.1",
        "to_port": 6379,
    },
    {
        "from_host": "redis-cluster-2",
        "from_port": 6379,
        "to_host": "127.0.0.1",
        "to_port": 6379,
    },
    {
        "from_host": "redis-cluster-3",
        "from_port": 6379,
        "to_host": "127.0.0.1",
        "to_port": 6379,
    },
    {
        "from_host": "redis-cluster-4",
        "from_port": 6379,
        "to_host": "127.0.0.1",
        "to_port": 6379,
    },
    {
        "from_host": "redis-cluster-5",
        "from_port": 6379,
        "to_host": "127.0.0.1",
        "to_port": 6379,
    },
]
rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True, host_port_remap=host_port_remap)

After I ran the script, I get the following error.

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/rediscluster/nodemanager.py", line 85, in _validate_host_port_remap
    socket.inet_aton(item.get('from_host', '0.0.0.0').strip())
OSError: illegal IP address string passed to inet_aton

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/rediscluster/client.py", line 365, in __init__
    pool = connection_pool_cls(
  File "/usr/local/lib/python3.8/site-packages/rediscluster/connection.py", line 149, in __init__
    self.nodes = NodeManager(
  File "/usr/local/lib/python3.8/site-packages/rediscluster/nodemanager.py", line 53, in __init__
    self._validate_host_port_remap(host_port_remap)
  File "/usr/local/lib/python3.8/site-packages/rediscluster/nodemanager.py", line 88, in _validate_host_port_remap
    raise RedisClusterConfigError("Both from_host and to_host in host_port_remap rule must be a valid ip address")
rediscluster.exceptions.RedisClusterConfigError: Both from_host and to_host in host_port_remap rule must be a valid ip address
marcosbc commented 3 years ago

Hi @shibuiwilliam, note that you cannot access 127.0.0.1:6379 from outside of the Kubernetes pod where each Redis Cluster node is running.

It is probable that if you enable cluster.externalAccess for it to work properly: https://github.com/bitnami/charts/tree/master/bitnami/redis-cluster#cluster-management-parameters

github-actions[bot] commented 3 years ago

This Issue has been automatically marked as "stale" because it has not had recent activity (for 15 days). It will be closed if no further activity occurs. Thanks for the feedback.

github-actions[bot] commented 3 years ago

Due to the lack of activity in the last 5 days since it was marked as "stale", we proceed to close this Issue. Do not hesitate to reopen it later if necessary.