Kubernetes 集群部署 MongoDB+exporter (單節點)

      網友投稿 925 2025-03-31

      簡介

      參數配置

      創建 ConfigMap 存儲配置文件

      Kubectl 部署 ConfigMap

      簡介

      參數配置

      創建 ConfigMap 存儲配置文件

      Kubectl 部署 ConfigMap

      數據存儲

      創建 PV

      創建 PVC 綁定存儲空間

      通過 Kubectl 工具部署 PV、PVC

      Kubernetes 部署 MongoDB

      mongodb exporter

      創建 Deployment

      通過 Kubectl 工具部署

      測試是否能夠正常使用

      測試獲取監控數據是否正常

      系統環境:

      MongoDB 版本:4.2.5

      Kubernetes 版本:1.19.5

      操作系統版本:CentOS 7.8

      簡介

      MongoDB 是我們常用的 NoSQL 數據庫,在項目開發、測試、部署到生成環境時,經常需要部署一套 MongoDB 來對文檔數據進行存儲。這里介紹下如何在 Kubernetes 環境中部署用于開發、測試的環境的 MongoDB 數據庫,當然,部署的是單節點模式,并非用于生產環境的主從或集群模式。單節點的 MongoDB 部署簡單,且配置存活探針,能保證快速檢測 MongoDB 是否可用,當不可用時快速進行重啟。

      參數配置

      在使用 Kubernetes 部署應用后,一般會習慣與將應用的配置文件外置,用 ConfigMap 存儲,然后掛載進入鏡像內部。這樣,只要修改 ConfigMap 里面的配置,再重啟應用就能很方便就能夠使應用重新加載新的配置,很方便。

      創建 ConfigMap 存儲配置文件

      Kubernetes 集群部署 MongoDB+exporter (單節點)

      創建 Kubernetes 的 ConfigMap 資源,用于存儲 MongoDB 的配置文件 mongodb.conf 內容:

      mongo-conf.yaml:

      apiVersion: v1 kind: ConfigMap metadata: name: mongo-config-produce labels: app: mongo-produce 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

      注意:

      修改 db 數據存儲路徑

      注意不能加fork=true,否則 Pod 會變成 Completed

      Kubectl 部署 ConfigMap

      通過 kubectl 工具部署 Kubernetes ConfigMap 資源,命令如下:

      $ kubectl create -f mongo-config.yaml

      數據存儲

      Kubernetes 部署的應用一般都是無狀態應用,部署后下次重啟很可能會漂移到不同節點上,所以不能使用節點上的本地存儲,而是網絡存儲對應用數據持久化,PV 和 PVC 是 Kubernetes 用于與儲空關聯的資源,可與不同的存儲驅動建立連接,存儲應用數據,所以接下來我們要創建 Kubernetes PV、PVC 資源。

      創建 PV

      PV 支持多種存儲驅動,不同存儲驅動的 PV 配置方式是不同的,需要根據你的存儲系統來配置 PV 參數。這里用的是 NFS 存儲(共享網絡文件存儲系統),直接使用前面創建的 StorageClass 即可

      具體參考:

      Kubernetes 集群部署 NFS-Subdir-External-Provisioner 存儲插件

      創建 PVC 綁定存儲空間

      mongo-storage.yaml:

      ## PVC kind: PersistentVolumeClaim apiVersion: v1 metadata: name: mongo-produce spec: storageClassName: nfs-storage #---指定StorageClass resources: requests: storage: 5Gi #設置 pvc 存儲資源大小 accessModes: - ReadWriteOnce

      通過 Kubectl 工具部署 PV、PVC

      通過 kubectl 工具部署 Kubernetes PV、PVC 資源,命令如下:

      $ kubectl create -f mongo-storage.yaml

      Kubernetes 部署 MongoDB

      mongodb exporter

      mongodb 沒有自帶 /metrics 接口供 Prometheus 使用,在這種情況下,我們就需要利用 exporter 服務來為 Prometheus 提供指標數據了,我們可以前往官方網站進行查看:

      https://prometheus.io/docs/instrumenting/exporters/

      這里我們選擇 mongodb_exporter:

      https://hub.docker.com/r/noenv/mongo-exporter

      創建 Deployment

      這里通過 mongodb_exporter 的服務來監控 mongodb 服務,我們以 sidecar 的形式和主應用部署在同一個 Pod 中,比如我們這里來部署一個 mongodb ,并用 mongodb _exporter 的方式來采集監控數據供 Prometheus 使用。

      創建用于 Kubernetes Deployment 來配置部署 MongoDB 的參數:

      配置 MongoDB 的鏡像地址、名稱、版本號;

      配置其 CPU 與 Memory 資源的占用;

      配置探針監測應用可用性;

      配置 Volume 掛載之前創建的 PV、PVC、ConfigMap 資源等等;

      構建 sidecar 掛載 mongodb _exporter。

      mongo-deploy.yaml:

      ## Service apiVersion: v1 kind: Service metadata: name: db-mongo-produce labels: app: mongo-produce spec: type: NodePort ports: - name: mongo port: 27017 targetPort: 27017 nodePort: 30017 - name: prom port: 9104 targetPort: 9104 selector: app: mongo-produce --- ## Deployment apiVersion: apps/v1 kind: Deployment metadata: name: db-mongo-produce labels: app: mongo-produce spec: replicas: 1 selector: matchLabels: app: mongo-produce template: metadata: annotations: prometheus.io/scrape: "true" prometheus.io/port: "9104" labels: app: mongo-produce spec: containers: - name: mongo-produce image: mongo:4.2.5 command: - sh - -c - "exec mongod -f /etc/mongod.conf" ports: - containerPort: 27017 resources: limits: cpu: 1000m memory: 512Mi requests: cpu: 1000m memory: 512Mi livenessProbe: initialDelaySeconds: 30 periodSeconds: 10 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 3 tcpSocket: port: 27017 readinessProbe: initialDelaySeconds: 10 periodSeconds: 10 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 3 tcpSocket: port: 27017 volumeMounts: - name: data mountPath: /data/middleware-data/mongodb/ - name: config mountPath: /etc/mongod.conf subPath: mongodb.conf - name: localtime readOnly: true mountPath: /etc/localtime - name: mongo-exporter image: noenv/mongo-exporter:latest args: [ "--web.listen-address=:9104", "--mongodb.uri", "mongodb://db-mongo-produce:27017", ] resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 9104 volumes: - name: data persistentVolumeClaim: claimName: mongo-produce - name: config configMap: name: mongo-config-produce - name: localtime hostPath: type: File path: /etc/localtime

      參數簡介:

      ports: 配置鏡像映射端口。

      resources: 配置 CPU、Memory 資源限制,可以通過配置該值來配置 Pod 的 QoS 級別。

      livenessProbe: 配置存活探針,定時檢測 MongoDB 應用運行狀態,如果檢測到 MongoDB 掛掉將進行重啟操作。

      readinessProbe: 配置就緒探針,定時檢測 MongoDB 應用啟動狀態,如果啟動成功將允許流量涌入,啟動失敗將進行重啟操作。

      command: 探針執行探測時執行的探測命令。

      volumeMounts: 存儲卷掛載配置,用于鏡像內存儲的掛載配置,與 volumes 中對于的 name 進行綁定。

      volumes: 存儲卷配置,可配置使用 pvc、hostPath、emptyDir、nfs 等存儲,需要配置 name 值與 -VolumeMounts 進行綁定。

      通過 Kubectl 工具部署

      通過 kubectl 工具部署 Deployment 來創建 MongoDB,命令如下:

      $ kubectl create -f mongo-deploy.yaml

      測試是否能夠正常使用

      進入 MongoDB 使用命令進行連接:

      $ kubectl exec -ti db-mongo-produce-5596947577-7bspt -- /bin/bash Defaulting container name to mongo. Use 'kubectl describe pod/db-mongo-produce-5596947577-7bspt ' to see all of the containers in this pod. root@db-mongo-produce-5596947577-7bspt:/# mongo MongoDB shell version v4.2.5 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("a6cf4d5f-cbc4-41e2-b2dd-417bbf967787") } MongoDB server version: 4.2.5 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2020-09-07T01:37:33.136+0800 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- > db test >

      可以看到,已經成功連接數據庫,說明數據庫能正常使用。

      測試獲取監控數據是否正常

      創建完成后,我們可以看到 mongodb 的 Pod 里面包含有兩個容器:

      $ kubectl get pods NAME READY STATUS RESTARTS AGE db-mongo-produce-5596947577-7bspt 2/2 Running 4 4d5h $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE db-mongo-produce ClusterIP 10.96.239.99 27017/TCP,9104/TCP 4d5h

      我們可以通過 9104 端口來校驗是否能夠采集到數據:

      $ curl 10.96.239.99:9104/metrics # HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 7.2981e-05 go_gc_duration_seconds{quantile="0.25"} 0.000132539 go_gc_duration_seconds{quantile="0.5"} 0.000185705 go_gc_duration_seconds{quantile="0.75"} 0.000327893 go_gc_duration_seconds{quantile="1"} 0.002464757 go_gc_duration_seconds_sum 1.023320185 go_gc_duration_seconds_count 3033 # HELP go_goroutines Number of goroutines that currently exist. # TYPE go_goroutines gauge go_goroutines 12 ......

      源碼地址:

      https://github.com/zuozewei/blog-example/tree/master/Kubernetes/k8s-mongodb-exporter

      Kubernetes MongoDB

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:Excel中都有哪些文本連接函數?
      下一篇:wps如何從兩列變為一列?
      相關文章
      亚洲精品mv在线观看| 中文字幕精品无码亚洲字| 亚洲国产综合无码一区| 亚洲国产天堂久久综合| 亚洲aⅴ天堂av天堂无码麻豆| 国产成人亚洲综合网站不卡| 亚洲婷婷在线视频| 亚洲第一香蕉视频| 亚洲国产成人久久99精品| 亚洲毛片免费视频| 亚洲高清视频免费| 亚洲精品国产啊女成拍色拍| 亚洲综合视频在线| 亚洲精品视频专区| 亚洲日本国产乱码va在线观看| 亚洲欧洲尹人香蕉综合| 亚洲性无码av在线| 亚洲va在线va天堂成人| 日韩亚洲不卡在线视频中文字幕在线观看| 亚洲乱码卡一卡二卡三| 国产精品亚洲综合五月天| 亚洲一区二区三区久久久久| 国产成人精品日本亚洲直接| 国产亚洲玖玖玖在线观看| 亚洲成a人片在线观看天堂无码 | 国产精品高清视亚洲精品| 亚洲AV一二三区成人影片| 亚洲大成色www永久网址| 亚洲人成色777777老人头| 亚洲av片在线观看| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲 综合 国产 欧洲 丝袜| 亚洲国产综合精品一区在线播放| 亚洲精品无码久久久久AV麻豆| 亚洲综合无码AV一区二区| 久久久久亚洲精品无码系列| 亚洲精品午夜在线观看| 亚洲粉嫩美白在线| 亚洲AⅤ无码一区二区三区在线 | 亚洲欧洲中文日产| 亚洲中文字幕无码久久|