Setting Maxmemory and Maxmemory-Policy in Kubernetes using kubectl apply yaml

3/4/2020

Is there a way to set maxmemory and maxmemory-policy for Redis on Kubernetes?

I am using the command kubectl apply -f redis-cache.yaml

The file redis-cache.yaml contains this content:

apiVersion: apps/v1
data:
  redis-config: |
    maxmemory 256
    maxmemory-policy allkeys-lru
kind: Deployment
metadata:
  name: redis-cache
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis-cache
  template:
    metadata:
      labels:
        app: redis-cache
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: redis-cache
        image: redis
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 150m
            memory: 256Mi
        ports:
        - containerPort: 6379
          name: redis
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cache
spec:
  ports:
  - port: 6379
  selector:
    app: redis-cache
-- cfbd
kubernetes
redis

1 Answer

3/4/2020

The Kubernetes deployment doesn't have field named data.

You can provide the configuration via ConfigMap or Secret. The Redis configuration file is located at installdir/redis/etc/redis.conf.

ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
data:
  redis.conf: |-
    maxmemory 256
    maxmemory-policy allkeys-lru

Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-cache
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis-cache
  template:
    metadata:
      labels:
        app: redis-cache
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      volumes:
      - name: redis-config
        configMap:
          name: redis-config
      containers:
      - name: redis-cache
        image: redis
        volumeMounts:
        - name: redis-config
          mountPath: /redis/etc
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 150m
            memory: 256Mi
        ports:
        - containerPort: 6379
          name: redis
-- Kamol Hasan
Source: StackOverflow