简体   繁体   中英

redis cluster K8S connection

I'm running a redis-cluster in K8S:

kubectl get services -o wide
redis-cluster                              ClusterIP      10.97.31.167     <none>        6379/TCP,16379/TCP   22h   app=redis-cluster

When connecting to the cluster IP from the node itself connection is working fine:

redis-cli -h 10.97.31.167 -c
10.97.31.167:6379> set some_val 1
-> Redirected to slot [11662] located at 10.244.1.9:6379
OK
  1. Is there some way I can access the redis server from my local development VM without exposing every single pod as a service?
  2. When deploying my application to run inside the cluster itself (later, when in production), I should use the cluster IP too, or should I use the internal IPs of the pods as master IPs of the redis-master servers?

Simple forwarding to the remote machine won't work:

devvm:ssh -L 6380:10.97.31.167:6379 -i user.pem admin@k8snode.com

On dev VM:

root@devvm:~# redis-cli -h 127.0.0.1 -p 6380 -c
127.0.0.1:6380> set jaheller 1
-> Redirected to slot [11662] located at 10.244.1.9:6379

The redis connection is timeouting at this point.

I belive in all scenarios you just need to expose the service using kubernetes Service object of type:

  • Cluster IP ( in case you are consuming it inside the cluster )

  • NodePort ( for external access )

  • LoadBalancer ( in case of public access and if you are on cloud provider)

  • NodePort with external loadbalancer ( for public external access if you are on local infrastructure )

Dont need to worry about individual pods. The Service will take care of them.

Docs:

https://kubernetes.io/docs/concepts/services-networking/service/

I don't think you need any port redirection. You have to build an ingress controller upon your cluster though, ie nginx ingress controller

Then you just set up a single ingress service with exposed access, which will serve a cluster traffic.

Here is an example of Ingress Controller to Access cluster service:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: redis-cluster-ing
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: redis-cluster
          servicePort: 6379

You may check a step-by-step instruction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM