Project: KubeDB by AppsCode (App Definition and Development / Database)
File: using-podtemplate.md

title: Run MongoDB with Custom Configuration menu: docs_{{ .version }}: identifier: using-podtemplate-configuration name: Customize PodTemplate parent: mg-configuration weight: 15 menuname: docs{{ .version }} sectionmenuid: guides New to KubeDB? Please start . KubeDB supports providing custom configuration for MongoDB via . This tutorial will show you how to use KubeDB to run a MongoDB database with custom configuration using PodTemplate. 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 . Now, install KubeDB cli on your workstation and KubeDB operator in your cluster following the steps . To keep things isolated, this tutorial uses a separate namespace called `demo` throughout this tutorial. ```bash $ kubectl create ns demo namespace/demo created ``` Note: YAML files used in this tutorial are stored in folder in GitHub repository . KubeDB allows providing a template for database pod through `spec.podTemplate`. KubeDB operator will pass the information provided in `spec.podTemplate` to the StatefulSet created for MongoDB database. KubeDB accept following fields to set in `spec.podTemplate:` metadata: annotations (pod's annotation) labels (pod's labels) controller: annotations (statefulset's annotation) labels (statefulset's labels) spec: args env resources initContainers imagePullSecrets nodeSelector affinity serviceAccountName schedulerName tolerations priorityClassName priority securityContext livenessProbe readinessProbe lifecycle Read about the fields in details in , Below is the YAML for the MongoDB created in this example. Here, specifies environment variables and provides extra arguments for . In this tutorial, `maxIncomingConnections` is set to `100` (default, 65536) through args `--maxConns=100`. ```yaml apiVersion: kubedb.com/v1alpha2 kind: MongoDB metadata: name: mgo-misc-config namespace: demo spec: version: "4.4.26" storageType: "Durable" storage: storageClassName: "standard" accessModes: ReadWriteOnce resources: requests: storage: 1Gi podTemplate: spec: args: --maxConns=100 resources: requests: memory: "1Gi" cpu: "250m" terminationPolicy: Halt ``` ```bash $ kubectl create -f https://github.com/kubedb/docs/raw/{{< param "info.version" >}}/docs/examples/mongodb/configuration/mgo-misc-config.yaml

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-misc-config-0` has been created. Check that the statefulset's pod is running ```bash $ kubectl get pod -n demo NAME READY STATUS RESTARTS AGE mgo-misc-config-0 1/1 Running 0 14m ``` Now, check if the database has started with the custom configuration we have provided. Now, you can connect to this database through . In this tutorial, we are connecting to the MongoDB server from inside the pod. ```bash $ kubectl get secrets -n demo mgo-misc-config-auth -o jsonpath='{.data.\username}' | base64 -d root $ kubectl get secrets -n demo mgo-misc-config-auth -o jsonpath='{.data.\password}' | base64 -d zyp5hDfRlVOWOyk9 $ kubectl exec -it mgo-misc-config-0 -n demo sh mongo admin db.auth("root","zyp5hDfRlVOWOyk9") 1 db._adminCommand( {getCmdLineOpts: 1}) { "argv" : [ "mongod", "--dbpath=/data/db", "--auth", "--ipv6", "--bindipall", "--port=27017", "--tlsMode=disabled", "--config=/data/configdb/mongod.conf" ], "parsed" : { "config" : "/data/configdb/mongod.conf", "net" : { "bindIp" : "*", "ipv6" : true, "maxIncomingConnections" : 100, "port" : 27017, "tls" : { "mode" : "disabled" } }, "security" : { "authorization" : "enabled" }, "storage" : { "dbPath" : "/data/db" } }, "ok" : 1 } exit bye ``` You can see the maximum connection is set to `100` in `parsed.net.maxIncomingConnections`. To cleanup the Kubernetes resources created by this tutorial, run: ```bash kubectl patch -n demo mg/mgo-misc-config -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge" kubectl delete -n demo mg/mgo-misc-config kubectl delete ns demo ``` If you would like to uninstall KubeDB operator, please follow the steps . with KubeDB Operator. MongoDB databases using Stash. Initialize . Monitor your MongoDB database with KubeDB using . Monitor your MongoDB database with KubeDB using . Use to deploy MongoDB with KubeDB. Use to manage databases like kubectl for Kubernetes. Detail concepts of . Want to hack on KubeDB? Check our .