在Kubernetes中通过秘密文件使用变量



我试图在confMap的一行中使用secret的变量。该部署正在部署一个需要通过url连接到MongoDB服务器的应用程序。url在confMap中,usernamepassword是存储在secret中的变量。我陷在里面太久了,解决不了。谢谢你的帮助。以下是文件:

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

错误:

[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.

请检查您的MongoDB URL连接字符串,但是如果认证失败,这意味着它可能是用户名密码的问题。

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

理想情况下,我们在每个服务URL的末尾传递端口号,如果有的话,也传递默认的管理数据库。

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

相关内容

  • 没有找到相关文章

最新更新