尝试在Kubernetes上的MongoDB中创建新用户时,连接被拒绝
我正在使用一个带有只读文件系统的自定义MongoDB映像,并尝试使用Kind将其部署到Mac上本地的Kubernetes。下面是我的statefolset.yaml。我无法运行名为尝试在Kubernetes上的MongoDB中创建新用户时,连接被拒绝,mongodb,kubernetes,permissions,Mongodb,Kubernetes,Permissions,我正在使用一个带有只读文件系统的自定义MongoDB映像,并尝试使用Kind将其部署到Mac上本地的Kubernetes。下面是我的statefolset.yaml。我无法运行名为mongo init.sh的自定义脚本,该脚本将创建我的db用户。Kubernetes只允许一个入口点,因此我使用它来运行我的db启动命令:mongod-f/docker entrypoint initdb.d/mongod.conf。它不允许我在此之后运行另一个命令或脚本,即使我在末尾附加了&。我尝试使用“/bin/
mongo init.sh
的自定义脚本,该脚本将创建我的db用户。Kubernetes只允许一个入口点,因此我使用它来运行我的db启动命令:mongod-f/docker entrypoint initdb.d/mongod.conf
。它不允许我在此之后运行另一个命令或脚本,即使我在末尾附加了&
。我尝试使用“/bin/bash/”、“-c”、“command1”、“command2”
,但如果我的第一个命令是mongod
初始化命令,则不会执行command2
。最后,如果跳过mongod初始化命令,则数据库未启动并运行,无法连接到它,因此连接被拒绝。当我kubectl exec
进入容器时,我可以使用带有mongo
的mongo shell进入数据库,但我无法查看任何内容或真正执行任何命令,因为我遇到了未经授权的错误。完全迷失在这一点上,需要一些帮助
apiVersion: apps/v1
kind: StatefulSet
metadata:
annotations:
creationTimestamp: null
labels:
app: mydb
name: mongodb
namespace: mongodb
spec:
serviceName: "dbservice"
replicas: 1
selector:
matchLabels:
app: mydb
template:
metadata:
annotations:
creationTimestamp: null
labels:
app: mydb
spec:
# terminationGracePeriodSeconds: 10
imagePullSecrets:
- name: regcred
containers:
- env:
- name: MONGODB_APPLICATION_USER_PWD
valueFrom:
configMapKeyRef:
key: MONGODB_APPLICATION_USER_PWD
name: myconfigmap
- name: MONGO_INITDB_DATABASE
valueFrom:
configMapKeyRef:
key: MONGO_INITDB_DATABASE
name: myconfigmap
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
configMapKeyRef:
key: MONGO_INITDB_ROOT_PASSWORD
name: myconfigmap
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
configMapKeyRef:
key: MONGO_INITDB_ROOT_USERNAME
name: myconfigmap
image: 'localhost:5000/mongo-db:custom'
command: ["/docker-entrypoint-initdb.d/mongo-init.sh"]
name: mongodb
ports:
- containerPort: 27017
resources: {}
volumeMounts:
- mountPath: /data/db
name: mongodata
- mountPath: /etc/mongod.conf
subPath: mongod.conf
name: mongodb-conf
readOnly: true
- mountPath: /docker-entrypoint-initdb.d/mongo-init.sh
subPath: mongo-init.sh
name: mongodb-conf
initContainers:
- name: init-mydb
image: busybox:1.28
command: ["chown"]
args: ["-R", "998:998", "/data"]
volumeMounts:
- name: mongodata
mountPath: /data/db
volumes:
- name: mongodata
persistentVolumeClaim:
claimName: mongo-volume-claim
- name: mongodb-conf
configMap:
name: myconfigmap
defaultMode: 0777
执行kubectl日志时看到的错误消息:
MongoDB shell version v4.4.1
connecting to: mongodb://localhost:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server localhost:27017, connection attempt failed: SocketException: Error connecting to localhost:27017 (127.0.0.1:27017) :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1