Kubernetes部署MongoDB
本文为K8s集群部署单节点mongodb,用于业务服务测试。
创建动态持久卷存储
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongodb-disk
namespace: default
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: alicloud-disk-available
volumeMode: Filesystem
创建MongoDB工作负载
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb
namespace: default
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: registry.cn-beijing.cr.aliyuncs.com/mongo:6.0.3
imagePullPolicy: IfNotPresent
ports:
- containerPort: 27017
volumeMounts:
- name: mongodb-disk
mountPath: /data/db
volumes:
- name: mongodb-disk
persistentVolumeClaim:
claimName: mongodb-disk
imagePullSecrets:
- name: aliyun-docker-registry
创建服务
ClusterIP用于K8s集群内部连接
apiVersion: v1
kind: Service
metadata:
labels:
app: mongodb
name: mongodb-svc
namespace: default
spec:
ports:
- name: mongo
port: 27017
protocol: TCP
targetPort: 27017
selector:
app: mongodb
type: ClusterIP
Nodeport用于本地连接
apiVersion: v1
kind: Service
metadata:
labels:
app: mongodb
name: mongodb-nodeport
namespace: default
spec:
ports:
- name: mongo
nodePort: 32717
port: 27017
protocol: TCP
targetPort: 27017
selector:
app: mongodb
sessionAffinity: None
type: NodePort
通过上面步骤部署好的MongoDB数据库无需用户名、密码即可连接,如果想要开启权限验证,只需要在YAML文件中入环境变量MONGO_INITDB_ROOT_USERNAME和MONGO_INITDB_ROOT_PASSWORD即可。