'having trouble writing to redis

i recently created a redis pod in kubernetes. I am having trouble writing to the redis database. I am getting an error cant write to read only replica

when i do kubectl get pv i see the following:

    local-pv1   1Gi        RWO            Retain           Bound    default/data-redis-0   local-storage            61m
local-pv2   1Gi        RWO            Retain           Bound    default/data-redis-1   local-storage            61m
local-pv3   2Gi        RWO            Retain           Bound    default/data-redis-2   local-storage            61m

kubectl get pods and this leads to...

redis-0                                   1/1     Running   0             63m
redis-1                                   1/1     Running   0             63m
redis-2                                   1/1     Running   0             62m
redis-python-deployment-9cf76d5b5-hw2nf   1/1     Running   2 (28m ago)   28m
redis-python-deployment-9cf76d5b5-vf2f4   1/1     Running   1 (28m ago)   28m
redis-python-deployment-9cf76d5b5-wjxwn   1/1     Running   2 (28m ago)   28m
redis-python-deployment-9cf76d5b5-wlsjj   1/1     Running   0             28m

where redis-0 is the master and redis-1 and redis-2 are the slaves.

now im able to do this: kubectl exec -it redis-0 sh and then redis-cli and im able to perform the following: xadd test * value 1

Now I'm testing pushing data to this redis database.

import os
import random

import redis


def connect_to_redis():
    hostname = os.environ.get("REDISHOST", "localhost")
    password = os.environ["REDISPASSWORD"]
    port = os.environ["REDISPORT"]
    r = redis.Redis(hostname, port=int(port), password=password)
    return r


def send_data(redis_conn: redis.Redis, max_messages: int) -> None:
    count = 0
    while count < max_messages:
        try:
            data = {"price": random.random() * 100, "volume": random.random() * 10}
            resp = redis_conn.xadd(name="omega-orderbook", fields=data)
            print(resp)
            count += 1
        except Exception as e:
            print(f"error occured at {e}")


if __name__ == "__main__":
    r = connect_to_redis()
    send_data(r, 10)

I've called the above python file and built it in teh following image: docker build -t redis_practice

Error im receiving is You can't write against a read only replica.

My redis headless service:

    # Headless service for stable DNS entries of StatefulSet members.
apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  clusterIP: None
  ports:
    - port: 6379
      targetPort: 6379
      name: redis
  selector:
    app: redis

I do have a redis config that i have used from here: https://gist.github.com/bharathirajatut/dcebde585eba5ac8b1398b8ed653d32d

Is there anyway for me to refer to redis-0 in my python code? currently i have it as:

redis.Redis('redis-service', host=6379, password=mysecretpass)

Thanks

edit: ok, i managed to connect to redis after getting the ip address of the master pod. is there another way to connect?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source