<p>We have a situation where Kubernetes is killing Mongo containers when it reaches max memory limit of the container. Even though its expected to K8s to work in that way but feel like Mongo is not reusing its memory as its keep on growing the memory usage day by day even though the user load &amp; transactions are the same so what to check how we can limit the Mongo in reaching max memory of the container or flush Mongo memory at regular intervals.</p>

<p>I have tried to increase the memory which helped the pods to keep running a couple of more days before K8 killing it</p>

<pre><code>     "containers": [
          {
            "name": "mongo",
            "image": "dockercentral.com:5870/com.public/mongodb:3.6",
            "ports": [
              {
                "containerPort": 27017,
                "protocol": "TCP"
              }
            ]      
"resources": {
              "limits": {
                "cpu": "1",
                "memory": "24Gi"
              },
              "requests": {
                "cpu": "250m",
                "memory": "24Gi"
              }
            }
   "name": "MONGO_SECURITY",
                "value": "true"
              }
            ],
            "resources": {
              "limits": {
                "cpu": "500m",
                "memory": "1Gi"
              },
              "requests": {
                "cpu": "150m",
                "memory": "256Mi"
              }
            },
</code></pre>

Panda
<p>Based on what <strong>Stennie from MongoDB, Inc.</strong> wrote in comment to question.</p>

<p>this command in kube .yaml works for me:</p>

<pre><code>      command:
        - "sh"
        - "-c"
        - &gt;
          echo "storage:" &gt;&gt;  /etc/mongod.conf;
          echo "    wiredTiger:" &gt;&gt;  /etc/mongod.conf;
          echo "       engineConfig:" &gt;&gt;  /etc/mongod.conf;
          echo "          cacheSizeGB: 2" &gt;&gt;  /etc/mongod.conf;
          echo "replication:" &gt;&gt; /etc/mongod.conf;
          echo "  replSetName: YOUR_REPL_NAME" &gt;&gt; /etc/mongod.conf;
          mongod --config /etc/mongod.conf;
</code></pre>

<p>also there is a way to set it in runtime:</p>

<pre><code>db.adminCommand( { "setParameter": 1, "wiredTigerEngineRuntimeConfig":"cache_size=2G"})
</code></pre>

<p>which also works just fine, but it looks easier through kuberneties yaml file because to issue a command you have to wait until mongo is up and running.</p>

<p>NOTE:
make sure that your:</p>

<pre><code>      resources:
        limits:
          memory:
</code></pre>

<p>is allowing <strong>extra 1G</strong> for system.</p>
