Skip to content
lorebow

Step 8 of 9, about 3 min

Connect your apps

The address your apps use, why Redis stays private, and how to look inside with Redis Insight.

Redis stays inside the cluster. Apps in the cluster connect to a Service by its DNS name, and nothing else needs a way in:

InstallApps connect to
Replication, Helmredis-master.redis.svc.cluster.local:6379 for writes, redis-replicas for reads
Replication, YAMLredis-primary.redis.svc.cluster.local:6379
Sentinel, Helmredis.redis.svc.cluster.local:26379, master set mymaster
Sentinel, YAMLredis-sentinel.redis.svc.cluster.local:26379, master set mymaster
Shared Sentinel, YAMLsentinel.redis.svc.cluster.local:26379, master set orders, payments, …
Cluster, Helmredis-cluster-leader.redis.svc.cluster.local:6379
Cluster, YAMLredis-cluster.redis.svc.cluster.local:6379

#Don't expose it through an Ingress or MetalLB

  • An Ingress can't carry it. Ingress routes HTTP by host and path. Redis speaks its own protocol over plain TCP, so an Ingress has nothing to route on. Some controllers can forward raw TCP ports, but that is exposure without any of an Ingress's benefits.
  • A LoadBalancer (MetalLB or a cloud one) puts it on your network, and for Sentinel and Cluster it doesn't even work. A Sentinel answers "the primary is at 10.244.1.7", a pod IP that a client outside the cluster can't reach. A Cluster node answers MOVED 9546 10.244.2.4:6379, the same problem. Only replication's single primary works through one load-balanced address.
  • Redis is a database, not a website. Anything that can reach port 6379 can try passwords as fast as the network allows, and a correct one reads, deletes or rewrites everything.

When an app outside the cluster truly needs Redis, move the app in, or connect over a VPN into the cluster network. If neither is possible and you run replication: a LoadBalancer Service for the primary only (Bitnami: master.service.type: LoadBalancer), limited to known client addresses with master.service.loadBalancerSourceRanges, with TLS on.

#Let only your apps connect

The password is one lock. A NetworkPolicy is a second: pods that aren't on the list can't open a connection at all. It only works if your cluster's network plugin enforces NetworkPolicy (Calico and Cilium do), so test it after applying, as shown below.

Bitnami creates a NetworkPolicy already, but with allowExternal: true it lets every pod in. Close it:

sentinel-values.yaml (add)yaml
networkPolicy:
  enabled: true
  allowExternal: false

Now only Redis's own pods and pods labelled redis-client: "true" connect (the label is <release>-client, and the release here is redis). Add that label to your app's pod template. The OT chart creates no NetworkPolicy: use the YAML tab with the label clusterId: redis-cluster.

Check it from a throwaway pod, first without the label, then with it (use your primary's Service from the table above):

bash
kubectl -n redis run probe --rm -it --restart=Never --image=redis:8.10.3 -- \
  redis-cli -h redis-primary -t 3 ping
kubectl -n redis run probe --rm -it --restart=Never --image=redis:8.10.3 --labels=redis-client=true -- \
  redis-cli -h redis-primary -t 3 ping

The first should time out. The second reaches Redis and gets NOAUTH Authentication required, which is the password doing its job.

#Look inside with Redis Insight

Redis itself has no web page. redis-cli, which this guide uses, is its command line. Redis Insight is Redis's separate graphical app for browsing keys, running commands and watching memory: a desktop app, or the container image redis/redisinsight serving a web page on port 5540.

If you're not sure which one something is, look at the image:

bash
kubectl -n redis get pods -o custom-columns='POD:.metadata.name,IMAGES:.spec.containers[*].image'

redis, bitnami/redis and quay.io/opstree/redis are Redis servers. redis/redisinsight is the UI. Nothing in this guide installs the UI.

Don't expose Redis Insight either. It has no login of its own, and it stores the passwords of every database you add. Anyone who reaches its page can read and change all your data. Run it only while you need it, reached through kubectl port-forward, which needs your cluster credentials:

redisinsight.yamlyaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redisinsight
  namespace: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redisinsight
  template:
    metadata:
      labels:
        app: redisinsight
        redis-client: "true"   # allowed through the NetworkPolicy above
    spec:
      containers:
        - name: redisinsight
          image: redis/redisinsight:3.8.0
          env:
            - name: RI_APP_HOST
              value: "0.0.0.0"
            - name: RI_APP_PORT
              value: "5540"
          ports:
            - containerPort: 5540
          livenessProbe:
            httpGet:
              path: /api/health/
              port: 5540
          volumeMounts:
            - name: data
              mountPath: /data
      volumes:
        - name: data
          emptyDir: {}   # saved connections go when the pod does

There is deliberately no Service: nothing in or out of the cluster can reach it except your port-forward.

bash
kubectl apply -f redisinsight.yaml
kubectl -n redis port-forward deploy/redisinsight 5540

Open http://localhost:5540 and add a database with an address from the table above and the password from redis-auth. Give Redis Insight the Sentinel address and port 26379, and it lists the master sets to pick from; give it any Cluster address, and it finds the other nodes. When you're done:

bash
kubectl -n redis delete deploy redisinsight

The desktop app works too for replication: kubectl -n redis port-forward svc/redis-master 6379 (svc/redis-primary with YAML) and connect it to localhost:6379. It can't follow Sentinel or Cluster, for the same reason a LoadBalancer can't: they answer with pod addresses only reachable inside the cluster.