Reputation: 223
I am developing an application (car-app) which uses socket.io. and now I am going to deploy it to kubernetes cluster. Then I use Redis pub/sub function to communicate.
My app structure:
Backend: NodeJS
Frontend: ReactJS
Mongodb
Then I am trying to connect Redis in NodeJS. It can be run on my localhost, but it cannot run on my GKE cluster.
(NodeJS)
const redis = require('redis');
const REDISPORT = 6379;
const subscriber = redis.createClient(REDISPORT, redis);
Error while running on GKE cluster:
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
I think this maybe caused by service connection and my redis deployment and service are configured below.
apiVersion: apps/v1
kind: Deployment
metadata:
name: car-redis-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: car-redis
spec:
containers:
- name: car-redis
image: redis:latest
ports:
- containerPort: 6379
name: http-port
selector:
matchLabels:
app: car-redis
apiVersion: v1
kind: Service
metadata:
name: car-redis-service
spec:
ports:
- port: 6379
protocol: TCP
targetPort: 6379
selector:
app: car-redis
type: NodePort
Upvotes: 0
Views: 3119
Reputation: 1218
It works on localhost because host might be already configured with redis
running, so your node
is looking for 127.0.0.1:6379
and it's able to connect to one without any errors.
Coming to k8s, your deployed application
is looking for redis
within the same container. So, you are getting an error while doing the same.
While coming to GKE or cloud, you need to configure your node
application with the particular host ip
or url
on which your redis
application is running. As, I can see you are already running a redis
in your k8 cluster, if it is the same cluster as your node
application deployment you can directly connect it with the service using something like this
<service-name>.<namespace-name>.svc.cluster.local
How to communicate between ns?
From your example, make sure your node
app supports redis
url instead of just port. And add your configs car-redis-service.default.svc.cluster.local:6379
in your node
app it should work without any issues.
Upvotes: 1