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 MongoDB. This tutorial will show you how to use KubeDB to run a MongoDB database 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. Run the following command to prepare your cluster for this tutorial:

    $ kubectl create ns demo
    namespace/demo created

Note: The yaml files used in this tutorial are stored in docs/examples/mongodb folder in GitHub repository kubedb/docs.


MongoDB allows configuring database via configuration file. The default configuration file for MongoDB deployed by KubeDB can be found in /data/configdb/mongod.conf. When MongoDB starts, it will look for custom configuration file in /configdb-readonly/mongod.conf. If configuration file exist, this custom configuration will overwrite the existing default one.

To learn available configuration option of MongoDB see Configuration File Options.

At first, you have to create a secret with your configuration file contents as the value of this key mongod.conf. Then, you have to specify the name of this secret in section while creating MongoDB crd. KubeDB will mount this secret into /configdb-readonly/ directory of the database pod.

Here one important thing to note that, will be used for standard replicaset members & standalone mongodb only. If you want to configure a specific type of mongo nodes, you have to set the name in respective fields. For example, to configure shard topology node, set spec.shardTopology.<shard / configServer / mongos> field. Similarly, To configure arbiter node, set field.

In this tutorial, we will configure net.maxIncomingConnections (default value: 65536) via a custom config file.

Custom Configuration

At first, create mongod.conf file containing required configuration settings.

$ cat mongod.conf
   maxIncomingConnections: 10000

Here, maxIncomingConnections is set to 10000, whereas the default value is 65536.

Now, create the secret with this configuration file.

$ kubectl create secret generic -n demo mg-configuration --from-file=./mongod.conf
secret/mg-configuration created

Verify the secret has the configuration file.

$  kubectl get secret -n demo mg-configuration -o yaml
apiVersion: v1
  mongod.conf: bmV0OgogIG1heEluY29taW5nQ29ubmVjdGlvbnM6IDEwMDAwMA==
kind: Secret
  creationTimestamp: "2021-02-09T12:59:50Z"
  name: mg-configuration
  namespace: demo
  resourceVersion: "52495"
  uid: 92ca4191-eb97-4274-980c-9430ab7cc5d1
type: Opaque

$ echo bmV0OgogIG1heEluY29taW5nQ29ubmVjdGlvbnM6IDEwMDAwMA== | base64 -d
  maxIncomingConnections: 100000

Now, create MongoDB crd specifying spec.configSecret field.

kind: MongoDB
  name: mgo-custom-config
  namespace: demo
  version: "4.2.3"
  storageType: Durable
    storageClassName: "standard"
    - ReadWriteOnce
        storage: 1Gi
    name: mg-configuration
$ kubectl create -f created

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

Check that the statefulset’s pod is running

$ kubectl get pod -n demo mgo-custom-config-0
NAME                  READY     STATUS    RESTARTS   AGE
mgo-custom-config-0   1/1       Running   0          1m

Now, we will check if the database has started with the custom configuration we have provided.

Now, you can connect to this database through mongo-shell. In this tutorial, we are connecting to the MongoDB server from inside the pod.

$ kubectl get secrets -n demo mgo-custom-config-auth -o jsonpath='{.data.\username}' | base64 -d

$ kubectl get secrets -n demo mgo-custom-config-auth -o jsonpath='{.data.\password}' | base64 -d

$ kubectl exec -it mgo-custom-config-0 -n demo sh

> mongo admin

> db.auth("root","ErialNojWParBFoP")

> db._adminCommand( {getCmdLineOpts: 1})
	"argv" : [
	"parsed" : {
		"config" : "/data/configdb/mongod.conf",
		"net" : {
			"bindIp" : "*",
			"ipv6" : true,
			"maxIncomingConnections" : 10000,
			"port" : 27017,
			"tls" : {
				"mode" : "disabled"
		"security" : {
			"authorization" : "enabled"
		"storage" : {
			"dbPath" : "/data/db"
	"ok" : 1

> exit

As we can see from the configuration of running mongodb, the value of maxIncomingConnections has been set to 10000 successfully.

Cleaning up

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

kubectl patch -n demo mg/mgo-custom-config -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"
kubectl delete -n demo mg/mgo-custom-config

kubectl delete -n demo secret mg-configuration

kubectl delete ns demo

Next Steps