1.1 前提准备
-
MongoDB
镜像 可从 Docker Hub 官网 下载镜像 - 外部ceph集群搭建好,(使用cephfs-csi 做数据持久卷,作为
MongoDB用来存放数据
)
1.2 编写 yaml
文件
编写 mongo.yaml
文件,提供了一个对外暴露的 NodePort 类型的 Service,用于外部访问。
---
apiVersion: v1
kind: Namespace
metadata:
name: mongodb
labels:
name: mongo
---
#创建fastdfs存储卷pvc分配1G使用
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongodb-pvc
namespace: mongodb
spec:
accessModes:
- ReadWriteMany
storageClassName: jyy-cephfs-sc
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
name: mongo
namespace: mongodb
labels:
app: mongo
spec:
ports:
- name: mongo
port: 27017
targetPort: 27017
clusterIP: None
---
apiVersion: v1
kind: Service
metadata:
name: mongo-service
namespace: mongodb
labels:
app: mongo
spec:
ports:
- name: mongo-service
port: 27017
nodePort: 27017
selector:
app: mongo
type: NodePort
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongo
namespace: mongodb
spec:
selector:
matchLabels:
app: mongo
serviceName: "mongo"
replicas: 1
template:
metadata:
labels:
app: mongo
spec:
terminationGracePeriodSeconds: 0
containers:
- name: mongo
image: mongo:latest
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: root
- name: MONGO_INITDB_ROOT_PASSWORD
value: 'uUN38#%pq'
command:
- mongod
- "--bind_ip_all"
- "--replSet"
- rs0
ports:
- containerPort: 27017
volumeMounts:
- name: mongo-data
mountPath: /data/db
volumes:
- name: mongo-data
persistentVolumeClaim:
claimName: mongodb-pvc