You are looking at the documentation of a prior release. To read the documentation of the latest release, please visit here.

New to KubeDB? Please start here.

Using Custom Configuration File

KubeDB supports providing custom configuration for Memcached. This tutorial will show you how to use KubeDB to run Memcached with custom configuration.

Before You Begin

  • At first, you need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using kind.

  • Now, install KubeDB cli on your workstation and KubeDB operator in your cluster following the steps here.

  • To keep things isolated, this tutorial uses a separate namespace called demo throughout this tutorial.

    $ kubectl create ns demo
    namespace/demo created
    
    $ kubectl get ns demo
    NAME    STATUS  AGE
    demo    Active  5s
    

Note: YAML files used in this tutorial are stored in docs/examples/memcached folder in GitHub repository kubedb/docs.

Overview

Memcached does not allows to configuration via any file. However, configuration parameters can be set as arguments while starting the memcached docker image. To keep similarity with other KubeDB supported databases which support configuration through a config file, KubeDB has added an additional executable script on top of the official memcached docker image. This script parses the configuration file then set them as arguments of memcached binary.

To know more about configuring Memcached server see here.

At first, you have to create a config file named memcached.conf with your desired configuration. Then you have to put this file into a volume. You have to specify this volume in spec.configSecret section while creating Memcached crd. KubeDB will mount this volume into /usr/config directory of the database pod.

In this tutorial, we will configure max_connections and limit_maxbytes via secret.

Create a secret with custom configuration file:

apiVersion: v1
stringData:
  memcached.conf: |
    --conn-limit=500
    --memory-limit=128    
kind: Secret
metadata:
  name: mc-configuration
  namespace: demo
  resourceVersion: "4505"

Here, –con-limit means max simultaneous connections which is default value is 1024. and –memory-limit means item memory in megabytes which default value is 64.

 $ kubectl apply -f mc-configuration.yaml
secret/mc-configuration created

Let’s get the mc-configuration secret with custom configuration:

$ $ kubectl apply -f https://github.com/kubedb/docs/raw/v2024.11.18/docs/examples/memcached/custom-config/mc-configuration.yaml
apiVersion: v1
data:
  memcached.conf: LS1jb25uLWxpbWl0PTUwMAotLW1lbW9yeS1saW1pdD01MTIK
kind: Secret
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Secret","metadata":{"annotations":{},"name":"mc-configuration","namespace":"demo","resourceVersion":"4505"},"stringData":{"memcached.conf":"--conn-limit=500\n--memory-limit=512\n"}}      
  creationTimestamp: "2024-08-26T12:19:54Z"
  name: mc-configuration
  namespace: demo
  resourceVersion: "4580860"
  uid: 02d41fc0-590e-44d1-ae95-2ee8f9632d36
type: Opaque

Now, create Memcached crd specifying spec.configSecret field.

$ kubectl apply -f https://github.com/kubedb/docs/raw/v2024.11.18/docs/examples/memcached/configuration/mc-custom.yaml
memcached.kubedb.com/custom-memcached created

Below is the YAML for the Memcached crd we just created.

apiVersion: kubedb.com/v1
kind: Memcached
metadata:
  name: custom-memcached
  namespace: demo
spec:
  replicas: 1
  version: "1.6.22"
  configSecret:
    name: mc-configuration
  podTemplate:
    spec:
      containers:
        - name: memcached
          resources:
            limits:
              cpu: 500m
              memory: 128Mi
            requests:
              cpu: 250m
              memory: 64Mi
  deletionPolicy: WipeOut

Now, wait a few minutes. KubeDB operator will create necessary petset, services etc. If everything goes well, we will see that a pod with the name custom-memcached-0 has been created.

Check if the database is ready

$ kubectl get mc -n demo
NAME               VERSION   STATUS   AGE
custom-memcached   1.6.22    Ready    17m

Now, we will check if the database has started with the custom configuration we have provided. We will use stats command to check the configuration.

We will connect to custom-memcached-0 pod from local-machine using port-frowarding.

$ kubectl port-forward -n demo custom-memcached-0 11211
Forwarding from 127.0.0.1:11211 -> 11211
Forwarding from [::1]:11211 -> 11211

Now, connect to the memcached server from a different terminal through telnet.

$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
...
STAT max_connections 500
...
STAT limit_maxbytes 134217728
...
END

Here, limit_maxbytes is represented in bytes.

Cleaning up

To cleanup the Kubernetes resources created by this tutorial, run:

kubectl patch -n demo mc/custom-memcached -p '{"spec":{"deletionPolicy":"WipeOut"}}' --type="merge"
kubectl delete -n demo mc/custom-memcached

kubectl patch -n demo drmn/custom-memcached -p '{"spec":{"wipeOut":true}}' --type="merge"
kubectl delete -n demo drmn/custom-memcached

kubectl delete -n demo secret mc-configuration

kubectl delete ns demo

If you would like to uninstall KubeDB operator, please follow the steps here.

Next Steps