基于Kubernetes集群构建MongoDB

作者:行癫(盗版必究) ------ ## 一:基础环境 1.Kubernetes集群正常运行 ![image-20231122095145370](https://diandiange.oss-cn-beijing.aliyuncs.com/image-20231122095145370.png) 2.Harbor私有仓库正常运行 ![image-20231122095408602](https://diandiange.oss-cn-beijing.aliyuncs.com/image-20231122095408602.png) ## 二:MongoDB项目部署 ​ MongoDB项目对应Kubernetes的yaml文件: ```yaml --- apiVersion: v1 kind: Namespace metadata: name: mongodb --- apiVersion: apps/v1 kind: Deployment metadata: labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: mongodb name: mongodb namespace: mongodb spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: mongodb strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: mongodb spec: containers: - env: - name: MONGO_INITDB_ROOT_USERNAME value: root - name: MONGO_INITDB_ROOT_PASSWORD value: xingdian image: mongo #image: 10.9.12.201/xingdian/mongo imagePullPolicy: IfNotPresent name: mongodb ports: - containerPort: 27017 name: sss protocol: TCP resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /data/db name: volume-4ztti dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler terminationGracePeriodSeconds: 30 volumes: - name: volume-4ztti nfs: path: /data/mongodb server: 10.9.12.250 --- apiVersion: v1 kind: Service metadata: labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: mongodb name: mongodb namespace: mongodb spec: clusterIP: 10.108.206.134 clusterIPs: - 10.108.206.134 externalTrafficPolicy: Cluster internalTrafficPolicy: Cluster ipFamilies: - IPv4 ipFamilyPolicy: SingleStack ports: - name: 3cmrmd nodePort: 30150 port: 27017 protocol: TCP targetPort: 27017 selector: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: mongodb sessionAffinity: None type: NodePort ``` ​ MongoDB-Express项目对应Kubernetes的yaml文件: ``` --- apiVersion: apps/v1 kind: Deployment metadata: labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: mongodb-express name: mongodb-express namespace: mongodb spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: mongodb-express strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: mongodb-express spec: containers: - env: - name: ME_CONFIG_MONGODB_ADMINUSERNAME value: root - name: ME_CONFIG_MONGODB_ADMINPASSEORD value: xingdian - name: ME_CONFIG_MONGODB_URL value: 'mongodb://root:xingdian@10.9.12.206:30150/' #注意这里是连接mongodb的url,其中root是用户,xingdian是密码 image: 10.9.12.201/xingdian/mongo-express #image: mongodb-express imagePullPolicy: IfNotPresent name: mongodb-express ports: - containerPort: 8081 name: qq protocol: TCP terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler terminationGracePeriodSeconds: 30 --- apiVersion: v1 kind: Service metadata: labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: mongodb-express name: mongodb-express namespace: mongodb resourceVersion: '36330826' spec: clusterIP: 10.110.224.114 clusterIPs: - 10.110.224.114 externalTrafficPolicy: Cluster internalTrafficPolicy: Cluster ipFamilies: - IPv4 ipFamilyPolicy: SingleStack ports: - name: berrtc nodePort: 30151 port: 8081 protocol: TCP targetPort: 8081 selector: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: mongodb-express sessionAffinity: None type: NodePort ``` 1.浏览器访问MongoDB管理界面 ![image-20231122100003032](https://diandiange.oss-cn-beijing.aliyuncs.com/image-20231122100003032.png) ## 三:MongoDB使用