前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Kubernetes 集群部署 MongoDB+exporter (单节点)

Kubernetes 集群部署 MongoDB+exporter (单节点)

作者头像
高楼Zee
发布2021-10-27 11:14:24
1.5K0
发布2021-10-27 11:14:24
举报
文章被收录于专栏:7DGroup7DGroup
「系统环境:」
  • MongoDB 版本:4.2.5
  • Kubernetes 版本:1.19.5
  • 操作系统版本:CentOS 7.8

一、简介

MongoDB 是我们常用的 NoSQL 数据库,在项目开发、测试、部署到生成环境时,经常需要部署一套 MongoDB 来对文档数据进行存储。这里介绍下如何在 Kubernetes 环境中部署用于开发、测试的环境的 MongoDB 数据库,当然,部署的是单节点模式,并非用于生产环境的主从或集群模式。单节点的 MongoDB 部署简单,且配置存活探针,能保证快速检测 MongoDB 是否可用,当不可用时快速进行重启。

二、参数配置

在使用 Kubernetes 部署应用后,一般会习惯于将应用的配置文件外置,用 ConfigMap 存储,然后挂载进入镜像内部。这样,只要修改 ConfigMap 里面的配置,再重启应用就能很方便就能够使应用重新加载新的配置,很方便。

1、创建 ConfigMap 存储配置文件

创建 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

2、Kubectl 部署 ConfigMap

通过 kubectl 工具部署 Kubernetes ConfigMap 资源,命令如下:

$ kubectl create -f mongo-config.yaml

三、数据存储

Kubernetes 部署的应用一般都是无状态应用,部署后下次重启很可能会漂移到不同节点上,所以不能使用节点上的本地存储,而是网络存储对应用数据持久化,PV 和 PVC 是 Kubernetes 用于与储空关联的资源,可与不同的存储驱动建立连接,存储应用数据,所以接下来我们要创建 Kubernetes PV、PVC 资源。

1、创建 PV

PV 支持多种存储驱动,不同存储驱动的 PV 配置方式是不同的,需要根据你的存储系统来配置 PV 参数。这里用的是 NFS 存储(共享网络文件存储系统),直接使用前面创建的 StorageClass 即可 具体参考:

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

2、创建 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

3、通过 Kubectl 工具部署 PV、PVC

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

$ kubectl create -f mongo-storage.yaml

四、Kubernetes 部署 MongoDB

1、mongodb exporter

mongodb 没有自带 /metrics 接口供 Prometheus 使用,在这种情况下,我们就需要利用 exporter 服务来为 Prometheus 提供指标数据了,我们可以前往官方网站进行查看:

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

这里我们选择 mongodb_exporter:

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

2、创建 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 进行绑定。

2、通过 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    <none>        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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-10-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 7DGroup 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、简介
  • 二、参数配置
    • 1、创建 ConfigMap 存储配置文件
      • 2、Kubectl 部署 ConfigMap
      • 三、数据存储
        • 1、创建 PV
          • 2、创建 PVC 绑定存储空间
            • 3、通过 Kubectl 工具部署 PV、PVC
            • 四、Kubernetes 部署 MongoDB
              • 1、mongodb exporter
                • 2、创建 Deployment
                  • 2、通过 Kubectl 工具部署
                  • 五、测试是否能够正常使用
                  • 六、测试获取监控数据是否正常
                  相关产品与服务
                  容器服务
                  腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档