Kubernetes how to pass conection string for MongoDb?

3/27/2020

This is my appsettings.json

{
  "ConnectionStrings": {
    "PackingService": "mongodb://127.0.0.1:27017/test"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

This is my Deployment for app

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: test
  name: test
spec:
  selector:
    matchLabels:
      app: test
  strategy: {}
  template:
    metadata:
      labels:
        io.kompose.network/dbnet: "true"
        app: test
    spec:
      containers:
      - env:
        - name: ConnectionStrings
          value: mongodb://localhost/test
        - name: MONGO_PORT
          valueFrom:
            configMapKeyRef:
              key: MONGO_PORT
              name: env
        - name: NODE_ENV
          value: development
        image: mlrd/smartboxes
        imagePullPolicy: ""
        name: test-api
        ports:
        - containerPort: 80
        resources: {}
      restartPolicy: Always
      serviceAccountName: ""
      volumes: null
status: {}

This is my Service for Mongodb

apiVersion: v1
kind: Service
metadata:
  name: mongodb
  labels:
    app: mongodb
spec:
  ports:
    - port: 27017
  selector:
    app: mongodb

This doesn't work throws an error time out when I want to connect to database, what is proper way to sent connection string to application from .yaml file? Should I set aspnetcore env variable also? Should I change something in .json config file? Sorry for lack of knowledge

-- Biljana Glumac
.net
asp.net-core
kubernetes
mongodb

2 Answers

3/28/2020

In your case MongoDB is being created with a Deployment and you are exposing it with a Service.

In your Service, you do not declare a Type. This leads me to assume you're attempting to connect to MongoDB from within Kubernetes using the ClusterIP Service type.

In your appsettings.json that is shown, you reference 127.0.0.1 in your connection string. As you're probably aware, this is a moniker for the local server reading the file.

In your case, your client server will be separate from MongoDB. Meaning your connection attempt would not be to 127.0.0.1 but rather to the name of your Kubernetes Service resource. In this case, mongodb.

Here are two further points of consideration.

1) If your client server that is initiating the request is in another Kubernetes namespace, then you will need to configure your connection string to properly reference the FQDN used on Kubernetes.

Based on your configuration above, this will probably look like: mongodb.default.svc.cluster.local

2) If your client server is external to your cluster. For example, your laptop or a development server. Then you will need to modify your Service type from ClusterIP to something load a LoadBalancer. Or use an Ingress to expose it.

I hope this answer helps you with your issue. If you need any further clarification or information on anything feel free to ask!

-- TJ Zimmerman
Source: StackOverflow

3/27/2020

Since mongodb is deployed as a separate pod and exposed by a service you can't use localhost or 127.0.0.1 If you have created the mongodb service in the same namespace as the app then you should use mongodb.svc.cluster.local. If mongodb service is in different namespace then use mongodb.namespacename.svc.cluster.local

-- Arghya Sadhu
Source: StackOverflow