Using variables in Kubernetes via secret file

7/29/2021

I'm trying to use variables from a secret in a line in confMap. The deployment is deploying an app which needs to connect to a MongoDB server via a url. The url is in a confMap and the username and the password are variables stored in the secret. I'm stuck too long in it and can't solve it. Would appreciate your help.Here are the files:

deployent.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo-deployment
  labels:
    app: echo-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: echo-app
  template:
    metadata:
      labels:
        app: echo-app
    spec:
      containers:
      - name: echo-app-container
        image: gcr.io/echo-project-320612/echo:v1.1
        ports:
          - containerPort: 3000
        volumeMounts:
          - mountPath: /app/config 
            name: echo-confmap123
        env:
          - name: username
            valueFrom:
              secretKeyRef:
                name: echo-secret
                key: username
          - name: password
            valueFrom:
              secretKeyRef:
                name: echo-secret
                key: password
      volumes:
        - name: echo-confmap123
          configMap:
            name: echo-confmap

secret.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: echo-secret
type: Opaque
stringData:
  username: usr123
  password: 123

confMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: echo-confmap
data:
  default.yaml: |
    port: 3000
    loglevel: info
    persist: true
    dbname: my-database
    mongodburl: mongodb://$(username):$(password)@my-database-mongodb-0.my-database-mongodb-headless.default.svc.cluster.local,my-database-mongodb-1.my-database-mongodb-headless.default.svc.cluster.local,my-database-mongodb-2.my-database-mongodb-headless.default.svc.cluster.local:27017/my-database

the error:

[2021-07-29T17:54:41.949] [INFO] default - Starting echo app!
[2021-07-29T17:54:41.967] [INFO] default - Echo listening on port 3000!
[2021-07-29T17:54:41.989] [FATAL] default - Could not connect to MongoDB! MongoError: Authentication failed.
    at Function._getError (/app/node_modules/mongodb/lib/core/auth/scram.js:125:14)
    at /app/node_modules/mongodb/lib/core/auth/scram.js:175:31
    at Connection.messageHandler (/app/node_modules/mongodb/lib/core/connection/connect.js:334:5)
    at Connection.emit (events.js:314:20)
    at processMessage (/app/node_modules/mongodb/lib/core/connection/connection.js:364:10)
    at Socket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:533:15)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at Socket.Readable.push (_stream_readable.js:213:10) {
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {}
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
-- DlekaShelHaHaim
configmap
deployment
environment-variables
kubernetes
kubernetes-secrets

1 Answer

7/29/2021

Please check your MongoDB URL connection string however if auth failing which means it cloud be an issue of username password.

mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl

ideally we pass the port number at the end of each service URL and also pass the default admin database if any.

https://docs.mongodb.com/manual/reference/connection-string/

-- Harsh Manvar
Source: StackOverflow