Deploying redis into Kubernetes

Deploying redis into Kubernetes

Overview

We have two different options methods for deploying redis. We can deploy a cluster, or a standalone node.

Cluster

We will be installing redis into kubernetes using the redis operator. This will allow us to use CRDs to deploy redis clusters.

Installing the Operator

We will install the operator via helm.

kubectl create ns redis
helm repo add ot-helm <https://ot-container-kit.github.io/helm-charts/>
helm repo update
helm install redis-operator ot-helm/redis-operator --namespace redis

Installing the Cluster

We will use the operators helm chart to deploy the cluster. Before we do this, we need to create an external_service.yaml file to feed in as values to the helm chart which will expose redis as a ClusterIP sesrvice

external_service.yaml

externalService:
  enabled: true
  serviceType: ClusterIP
  port: 6379

We can then install the cluster and feed it the values file

helm upgrade redis-cluster ot-helm/redis-cluster -f external_service.yaml --set redisCluster.clusterSize=3 --install --namespace redis

It will take some time for all the pods to come online. When they are all reporting in a ready state, we can run this command to verify it’s running:

kubectl -n redis exec -it redis-cluster-leader-0 -- redis-cli cluster nodes

56f1aafa629a2812802ae962d5c34eab4481803f 10.244.1.43:6379@16379 slave 87635f9597bf2899020742e258c0a9cff0079955 0 1710801605000 3 connected
bea5d0f7a03b8752b09ee431b2d8e27a8b4fe537 10.244.0.38:6379@16379 master - 0 1710801606335 2 connected 5461-10922
a3d25fc9ef8a443e32887f331294408afc46ef64 10.244.0.170:6379@16379 slave 407f90075ab6de7fbe405ade297ac558b794692d 0 1710801606000 1 connected
407f90075ab6de7fbe405ade297ac558b794692d 10.244.0.169:6379@16379 myself,master - 0 1710801605000 1 connected 0-5460
b148b0bf369df79cfb8bc1d759aa1c8440342f4d 10.244.0.39:6379@16379 slave bea5d0f7a03b8752b09ee431b2d8e27a8b4fe537 0 1710801605331 2 connected
87635f9597bf2899020742e258c0a9cff0079955 10.244.1.42:6379@16379 master - 0 1710801605000 3 connected 10923-16383

If you see the above output, then all nodes are connected correctly.

Exposing redis via Traefik Ingress

We will need to extend the traefik helm chart to add the TCP entry point for redis. In the oci-k8s-devops repository, go into traefik/(environment) directory and modify the traefik_internal_values.yaml file

traefik_internal_values.yaml

---
...
additionalArguments:
  ...
  - "--entrypoints.redis.address=:6379/tcp"
ports:
  ...
  redis:
    port: 6379
    protocol: TCP
    expose: true
...

We will also need to modify the service definition to expose the redis port

traefik_internal_service.yaml

spec:
  ports:
  ...
  - name: redis
    port: 6379
    targetPort: redis
  ...

Once these files have been updated, we can apply the configurations

helm upgrade traefik-internal traefik/traefik --values traefik_internal_values.yaml
kubectl apply -f traefik_internal_service.yaml

Now that traefik has been configured, we will need to create the ingressrouteTCP entry point for redis

---
apiVersion: traefik.io/v1alpha1
kind: IngressRouteTCP
metadata:
  name: redis
  namespace: redis
  labels:
    ingressclass: traefik-internal
spec:
  entryPoints:
    - redis
  routes:
    - match: ClientIP(`10.0.0.0/8`)
      services:
        - name: redis-cluster-leader-external-service
          port: 6379

Standalone

We will be installing standalone redis into kubernetes using a raw deployment.

Deployment

We will use a standard redis deployment of latest image, with modest requests.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  labels:
    app: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:latest
          resources:
            limits:
              cpu: '1'
              memory: 2Gi
            requests:
              cpu: '0.5'
              memory: 1Gi

We can then install this with:

kubectl apply -f redis-deployment.yaml

Service

We will expose a ClusterIP service for Traefik to be able to route to

---
apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  selector:
    app: redis
  ports:
  - port: 6379
    protocol: TCP
    targetPort: 6379
  type: ClusterIP

We can then install this with:

kubectl apply -f redis-service.yaml

Ingress

Finally, we will use a TCP router ingress to route the raw TCP connection to the redis pod

---
apiVersion: traefik.io/v1alpha1
kind: IngressRouteTCP
metadata:
  name: redis
  namespace: default
  labels:
    ingressclass: traefik-internal
spec:
  entryPoints:
    - redis
  routes:
    - match: ClientIP(`10.0.0.0/8`)
      services:
        - name: redis-service
          port: 6379

We can then install this with:

kubectl apply -f redis-ingress.yaml