我正在尝试在 mongo 部署文件中运行如下所示的命令 -
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-deployment
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: mongo-pod
template:
metadata:
labels:
app: mongo-pod
spec:
containers:
- name: mongo-container
image: mongo
ports:
- containerPort: 27017
name: mongo
volumeMounts:
- name: mongo-persistent-storage
mountPath: /data/db
command:
- "bash"
- "-c"
- |
mongo --eval "db.getSiblingDB('admin').createUser({user : "$user123", pwd : "$pass", roles: [ { role: 'root', db: 'admin' } ]});"
volumes:
- name: mongo-persistent-storage
persistentVolumeClaim:
claimName: mongo-pv-claim
如您所见,我想在命令中为多个用户提供访问控制, 但是我在 pods 日志中收到以下错误,并且 pods 进入错误状态 -
MongoDB shell version v4.2.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2019-09-21T15:42:39.549+0000 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2019-09-21T15:42:39.550+0000 F - [main] exception: connect failed
2019-09-21T15:42:39.550+0000 E - [main] exiting with code 1
知道为什么我总是得到这个吗?
谢谢
试试这个:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-deployment
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: mongo-pod
template:
metadata:
labels:
app: mongo-pod
spec:
containers:
- name: mongo-container
image: mongo
ports:
- containerPort: 27017
name: mongo
volumeMounts:
- name: mongo-persistent-storage
mountPath: /data/db
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: user
- name: MONGO_INITDB_ROOT_PASSWORD
value: password
volumes:
- name: mongo-persistent-storage
persistentVolumeClaim:
claimName: mongo-pv-claim
它将为admin
数据库创建用户user
和密码password
,您可以在dockerhub上找到更多信息。