Running Redis on Multi-Node Kubernetes Cluster

Ajeet Singh Raina - Dec 23 '20 - - Dev Community

Prerequisite:

Ensure that Kubernetes(atleast 2-node cluster) is installed in your system

Verify Kubernetes components:

$ kubectl get componentstatus
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok
scheduler            Healthy   ok
etcd-0               Healthy   {"health":"true"}
[node1 kubelabs]$
Enter fullscreen mode Exit fullscreen mode

Configuring Redis using a ConfigMap:

You can follow the steps below to configure a Redis cache using data stored in a ConfigMap.

[node1 kubelabs]$ curl -OL https://k8s.io/examples/pods/config/redis-config
Enter fullscreen mode Exit fullscreen mode

First create a kustomization.yaml containing a ConfigMap from the redis-config file:

[node1 kubelabs]$ cat <<EOF >./kustomization.yaml
> configMapGenerator:
> - name: example-redis-config
>   files:
>   - redis-config
> EOF
Enter fullscreen mode Exit fullscreen mode

Add the pod resource config to the kustomization.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis:5.0.4
    command:
      - redis-server
      - "/redis-master/redis.conf"
    env:
    - name: MASTER
      value: "true"
    ports:
    - containerPort: 6379
    resources:
      limits:
        cpu: "0.1"
    volumeMounts:
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
  volumes:
    - name: data
      emptyDir: {}
    - name: config
      configMap:
        name: example-redis-config
        items:
        - key: redis-config
          path: redis.conf
Enter fullscreen mode Exit fullscreen mode

Apply the kustomization directory to create both the ConfigMap and Pod objects:

[node1 kubelabs]$ kubectl apply -k .
configmap/example-redis-config-dgh9dg555m created
pod/redis created
Enter fullscreen mode Exit fullscreen mode

Examine the created objects by

kubectl get -k .
NAME                                        DATA   AGE
configmap/example-redis-config-dgh9dg555m   1      33s

NAME        READY   STATUS    RESTARTS   AGE
pod/redis   1/1     Running   0          33s
[node1 kubelabs]$
Enter fullscreen mode Exit fullscreen mode

In the example, the config volume is mounted at /redis-master. It uses path to add the redis-config key to a file named redis.conf. The file path for the redis config, therefore, is /redis-master/redis.conf. This is where the image will look for the config file for the redis master.
Use kubectl exec to enter the pod and run the redis-cli tool to verify that the configuration was correctly applied:

[node1 kubelabs]$ kubectl exec -it redis -- redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "2097152"
127.0.0.1:6379> CONFIG GET maxmemory-policy
1) "maxmemory-policy"
2) "allkeys-lru"
127.0.0.1:6379>
Enter fullscreen mode Exit fullscreen mode

Delete the created pod:

$ kubectl delete pod redis
pod "redis" deleted
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . .