Application inside container of a Kubernetes Deployment fails with error localhost:port can not be accessed?

12/13/2019

I have an applications Docker image which starts a mongodb instance on a random port. When I create a kubernetes Pod with application image; application gets successfully initialized and a mongodb instance gets up on a random port as localhost:port without any error.

However, when I create a Kubernetes Deployment; the same application initialization fails inside the container with error "mongodb can not be started as localhost:port can not be accessed".

If anyone can explain, why application initialization is failing with K8-Deployment but not with K-8Pod? And, How can I resolve this problem?

apiVersion: apps/v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: my-app
    image: my-app:v1
    ports:
    - containerPort: 8888 # Apps exposed port

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-dep
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app:v1
        ports:
        - containerPort: 8888 # Apps exposed port

Thanks

-- Jaraws
docker
kubernetes
kubernetes-deployment
kubernetes-pod
mongodb

1 Answer

12/14/2019

Instead of listening on localhost:port, you should try configure MongoDB to listening on 0.0.0.0:port. This has helped me when having a similar issue with another app.

Configure MongoDB to listen to all interfaces

Your mongod.conf

# /etc/mongod.conf

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1

change to this

# /etc/mongod.conf

# Listen to local interface only. Comment out to listen on all interfaces.
# bind_ip = 127.0.0.1
-- Jonas
Source: StackOverflow