Unable to connect to redis cluster on kubernetes from my golang application deployed within the same cluster

8/12/2020

I deployed a redis cluster on Kubernetes with bitnami helm charts (https://github.com/bitnami/charts/tree/master/bitnami/redis-cluster).

I can successfully connect to the Redis cluster from within the Kubernetes cluster by running the below commands: 1. kubectl run my-redis-release-client --rm -it --image docker.io/bitnami/redis:4.0.11-debian-9 -- bash

  1. redis-cli -h redis-cluster-0.redis-cluster-headless.redis

But I am unable to connect to redis cluster from my golang application deployed within the same cluster.

The redis connection string uri I used on my golang application is "redis://redis-cluster-0.redis-cluster-headless.redis:6379". This is following the "redis-pod-name.redis-service-name.namespace" convention.

NOTE: I want to be able to access the redis cluster from only within the Kubernetes cluster. I don’t want to grant external access. Please help...

-- Silcod
docker
go
kubernetes
kubernetes-helm
redis-cluster

1 Answer

8/12/2020

Headless service is if you don't need load-balancing and a single Service IP. Headless service is not for accessing the redis cluster from only within the Kubernetes cluster

You can create a service to expose redis. Below is an example to create a ClusterIP type which only let's you connect to it from within the cluster and not from outside the cluster.

apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: default
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis

The pod or deployment of redis need to have matching label app: redis

Then you can connect to it using redis.default.svc.cluster.local:6379 to connect to it from Golang app.

-- Arghya Sadhu
Source: StackOverflow