<p>guys, I am new to k8s and I am trying to deploy a microservice using mongodb on Azure Kubernetes Service, using ACR. I have created an AKS cluster and the deployment for the microservice runs fine, but the problem is with the deployment of the mongo db. For some reason, it keeps crashing, and I don't know why.</p>
<blockquote>
<p>I get the error : MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017 ; when looking into logs</p>
</blockquote>
<p>The mongo.yml file :</p>
<pre><code>kind: Deployment
metadata:
  name: mongo-deployment
  labels:
    app: mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo
        ports:
          - containerPort: 27017
        resources:
          requests:
            memory: &quot;64Mi&quot;
            cpu: &quot;250m&quot;
          limits:
            memory: &quot;128Mi&quot;
            cpu: &quot;500m&quot;
        env:
          - name: MONGO_INITDB_ROOT_USERNAME
            valueFrom: 
              secretKeyRef:
                name: mongo-secret
                key: mongo-root-username
          - name: MONGO_INITDB_ROOT_PASSWORD
            valueFrom: 
              secretKeyRef:
                name: mongo-secret
                key: mongo-root-password
---
apiVersion: v1
kind: Service
metadata:
  name: mongo-service
spec:
  selector:
    app: mongodb
  ports:
  - protocol: TCP
    port: 27017
    targetPort: 27017
</code></pre>
<p>mongo-configmap.yml file :</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: mongo-configmap
data:
  connection_string: mongodb://username:password@mongo-service:27017
</code></pre>
<p>mongo-secret.yml file :</p>
<pre><code>kind: Secret
metadata:
  name: mongo-secret
type: Opaque
data:
  mongo-root-username: dXNlcm5hbWU=
  mongo-root-password: cGFzc3dvcmQ=
</code></pre>
<p>Logs when trying to run &quot;kubectl describe pod&quot; :</p>
<pre><code>Namespace:        default
Priority:         0
Service Account:  default
Node:             aks-nodepool1-85690495-vmss000000/10.224.0.4
Start Time:       Tue, 25 Apr 2023 11:34:41 +0300
Labels:           app=mongodb
                  pod-template-hash=5d456c788f
Annotations:      &lt;none&gt;
Status:           Running
IP:               10.244.0.37
IPs:
  IP:           10.244.0.37
Controlled By:  ReplicaSet/mongo-deployment-5d456c788f
Containers:
  mongodb:
    Container ID:   containerd://6862b0a5291f7a4a6d1a7f81ba5b5fe2286f04a7d8aa7817cad2b48142190352
    Image:          mongo
    Image ID:       docker.io/library/mongo@sha256:9c8a0a019671ed7d402768d4df6dddcc898828e21e9f7b90a34b55fe8ca676ac
    Port:           27017/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       OOMKilled
      Exit Code:    1
      Started:      Tue, 25 Apr 2023 12:03:17 +0300
      Finished:     Tue, 25 Apr 2023 12:03:21 +0300
    Ready:          False
    Restart Count:  10
    Limits:
      cpu:     500m
      memory:  128Mi
    Requests:
      cpu:     250m
      memory:  64Mi
    Environment:
      MONGO_INITDB_ROOT_USERNAME:  &lt;set to the key 'mongo-root-username' in secret 'mongo-secret'&gt;  Optional: false
      MONGO_INITDB_ROOT_PASSWORD:  &lt;set to the key 'mongo-root-password' in secret 'mongo-secret'&gt;  Optional: false
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-l6l84 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  kube-api-access-l6l84:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       &lt;nil&gt;
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              &lt;none&gt;
Tolerations:                 node.kubernetes.io/memory-pressure:NoSchedule op=Exists
                             node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age                    From               Message
  ----     ------     ----                   ----               -------
  Normal   Scheduled  30m                    default-scheduler  Successfully assigned default/mongo-deployment-5d456c788f-587r6 to aks-nodepool1-85690495-vmss000000   
  Normal   Pulled     30m                    kubelet            Successfully pulled image &quot;mongo&quot; in 735.583384ms (735.618884ms including waiting)
  Normal   Pulled     30m                    kubelet            Successfully pulled image &quot;mongo&quot; in 788.963634ms (788.974334ms including waiting)
  Normal   Pulled     30m                    kubelet            Successfully pulled image &quot;mongo&quot; in 725.867064ms (725.879864ms including waiting)
  Normal   Created    29m (x4 over 30m)      kubelet            Created container mongodb
  Normal   Started    29m (x4 over 30m)      kubelet            Started container mongodb
  Normal   Pulled     29m                    kubelet            Successfully pulled image &quot;mongo&quot; in 783.028263ms (783.042864ms including waiting)
  Normal   Pulling    27m (x5 over 30m)      kubelet            Pulling image &quot;mongo&quot;
  Normal   Pulled     27m                    kubelet            Successfully pulled image &quot;mongo&quot; in 738.599919ms (738.611919ms including waiting)
  Warning  BackOff    5m57s (x107 over 30m)  kubelet            Back-off restarting failed container
</code></pre>
<p>I understand that this issue is caused by the fact that a mongodb instance is not running, but I can't find an answer for this issue. I kept trying to delete the pod and to recreate it, but it doesn't work.</p>
<p>Can anyone help me please? I would be grateful to found a solution. Thank you!</p>

Andrei Popescu
<p>Looking at this output:</p>
<pre><code>State:          Waiting
  Reason:       CrashLoopBackOff
Last State:     Terminated
  Reason:       OOMKilled
</code></pre>
<p>It seems the <code>pod</code> has terminated due to an OOM (Out of Memory) error, and is now in a <code>CrashLoopBackOff state</code>.</p>
<p>The <code>OOM</code> error occurs when a container consumes more memory than the limit set for it.</p>
<p>In your case, increasing the memory limit should solve this problem.</p>
<pre><code>limits:
  memory: &quot;128Mi&quot;
  cpu: &quot;500m&quot;
</code></pre>
<p>Hope it helps!</p>
