kubernetes部署mongoDB 單機版 自定義配置檔案、密碼、日誌路徑等

GaoYanbing發表於2024-05-23

官方映象地址: https://hub.docker.com/_/mongo?tab=description

docker版的mongo移除了預設的/etc/mongo.conf, 修改了db資料儲存路徑為 /data/db.

建立configmap配置,注意不能加fork=true,否則Pod會變成Completed。

apiVersion: v1
kind: ConfigMap
metadata:
name: mongodb-conf
data:
mongodb.conf: |
dbpath=/data/middleware-data/mongodb
logpath=/data/middleware-data/mongodb/mongodb.log
pidfilepath=/data/middleware-data/mongodb/master.pid
directoryperdb=true
logappend=true
bind_ip=0.0.0.0
port=27017
1
2
3
4
5
6
7
8
9
10
11
12
13
建立StatefulSet:

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongodb
spec:
replicas: 1
serviceName: mongodb
selector:
matchLabels:
name: mongodb
template:
metadata:
labels:
name: mongodb
spec:
containers:
- name: mongodb
image: mongo:4.2.1
command:
- sh
- -c
- "exec mongod -f /data/middleware-data/mongodb/conf/mongodb.conf"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 27017
name: mongodb
protocol: TCP
volumeMounts:
- name: mongodb-config
mountPath: /data/middleware-data/mongodb/conf/
- name: data
mountPath: /data/middleware-data/mongodb/
volumes:
- name: mongodb-config
configMap:
name: mongodb-conf
- name: data
hostPath:
path: /data/middleware-data/mongodb/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
建立Service暴露埠:

kind: Service
apiVersion: v1
metadata:
labels:
name: mongodb
name: mongodb
spec:
type: NodePort
ports:
- name: mongodb
port: 27017
targetPort: 27017
nodePort: 30002
selector:
name: mongodb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
接著使用以下命令新增使用者和設定密碼,並且嘗試連線,以及修改密碼:

[root@liabio mongodb]# kubectl exec -ti mongodb-0 -- mongo admin
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6d31af3f-3749-3111-87d2-7c8745pob66c") }
MongoDB server version: 4.2.1
Welcome to the MongoDB shell.
For interactive help, type "help".
————————————————

本文為碼農實戰原創作品,歡迎關注「我的小碗湯」公眾號,QQ群:643418444

原文連結:https://blog.csdn.net/ll837448792/article/details/103313898

相關文章