rancher helm chart 集成 mongodb

/ 2021-05-16 / 69人浏览 / 0人评论

前言

项目使用rancher应用商店部署服务,用到了mongoDB,这里记录下helm chart 集成mongoDB。

一、为什么需要集成mongoDB?

mongoDB为什么需要和项目服务一起部署?其实你也是可以单独部署服务,再单独部署mongoDB。本项目集成mongoDB是为了方便实施部署方便,简化难度。

二、使用步骤

1.引入mongodb依赖

从rancher chart,引入已经集成完善的mongodb chart 参考地址 : https://github.com/rancher/charts/tree/master/charts 引入的mongodb chart 放到 charts 目录

wordpress/
  Chart.yaml          # 包含了chart信息的YAML文件
  LICENSE             # 可选: 包含chart许可证的纯文本文件
  README.md           # 可选: 可读的README文件
  values.yaml         # chart 默认的配置值
  values.schema.json  # 可选: 一个使用JSON结构的values.yaml文件
  charts/             # 包含chart依赖的其他chart ,  引入的mongodb放在此处
  crds/               # 自定义资源的定义
  templates/          # 模板目录, 当和values 结合时,可生成有效的Kubernetes manifest文件
  requirements.yaml   # 依赖chart定义
  questions.yml       

2.配置使用

requirements.yaml 代码示例:

dependencies:
  - name: mongodb
    version: 7.2.6
    condition: config.mongo.install
    repository: file://./charts/mongodb

questions.yaml 关于mongodb代码示例: 代码来源于mongodb questions.yaml,只是variable对应的值增加 mongodb(mongodb.usePassword,为什么这个前缀是mongodb,是引入chart.yaml中name定义是mongodb)

# mongodb
- variable: config.mongo.install
  default: false
  required: true
  label: "部署mongodb"
  description: "如果否,则需配置外部mongodb"
  type: boolean
  group: "mongodb设置"
# 外部mongodb配置
- variable: db.url
  default:  "mongodb://localhost:27017/shorturl"
  required: true
  show_if: "config.mongo.install=false"
  label: "设置外部mongodb"
  description: "参数格式   mongodb://name:pwd@ip:端口/数据库  ,如果无密码则是  mongodb://ip:端口/数据库"
  type: string
  group: "外部mongodb设置"
  # mongodb 部署
- variable: mongodb.usePassword
  default: true
  description: "Enable Password Authentication"
  type: boolean
  label: Enable Password Authentication
  required: true
  show_if: "config.mongo.install=true"
  show_subquestion_if: true
  group: "MongoDB Settings"
  subquestions:
  - variable: mongodb.mongodbUsername
    default: "username"
    description: "MongoDB username"
    show_if: "config.mongo.install=true"
    type: string
    label: MongoDB Username
    required: true
  - variable: mongodb.mongodbPassword
    default: "password"
    description: "MongoDB Password"
    show_if: "config.mongo.install=true"
    type: password
    label: MongoDB Password
  - variable: mongodb.mongodbDatabase
    default: "database"
    description: "MongoDB Database"
    show_if: "config.mongo.install=true"
    type: string
    label: MongoDB Database
    required: true
- variable: mongodb.replicaSet.enabled
  default: true
  description: "Switch to standalone or replicaSet mongodb instance"
  show_if: "config.mongo.install=true"
  type: boolean
  label: Enable MongoDB ReplicaSet
  required: true
  group: "ReplicaSet Configuration"
  show_subquestion_if: true
  subquestions:
  - variable: mongodb.replicaSet.name
    default: "rs0"
    description: "Name of the replica set"
    show_if: "config.mongo.install=true"
    type: string
    label: ReplicaSet Name
    required: true
  - variable: mongodb.replicaSet.key
    default: ""
    description: "Key used for replica set authentication, will be auto-generated if not set"
    show_if: "config.mongo.install=true"
    type: string
    label: ReplicaSet Auth Key
- variable: mongodb.persistence.enabled
  default: true
  description: "Enable persistent volume for MongoDB"
  show_if: "config.mongo.install=true"
  type: boolean
  required: true
  label: MongoDB Persistent Volume Enabled
  show_subquestion_if: true
  group: "Persistent Volume"
  subquestions:
  - variable: mongodb.persistence.size
    default: "8Gi"
    description: "MongoDB Persistent Volume Size"
    show_if: "config.mongo.install=true"
    type: string
    label: MongoDB Volume Size
    required: true
  - variable: mongodb.persistence.storageClass
    default: ""
    description: "If undefined or set to null, using the default storageClass. Defaults to null."
    show_if: "config.mongo.install=true"
    type: storageclass
    label: Storage Class for MongoDB
  - variable: mongodb.persistence.existingClaim
    default: ""
    description: "If not empty, uses the specified existing PVC instead of creating new one"
    type: pvc
    label: Uses Existing Persistent Volume Claim for LocalStorage
    show_if: "mongodb.persistence.enabled=true&&mongodb.replicaSet.enabled=false&&config.mongo.install=true"
- variable: mongodb.service.type
  default: "ClusterIP"
  description: "MongoDB Kubernetes Service type"
  show_if: "config.mongo.install=true"
  type: enum
  group: "Service Settings"
  options:
    - "ClusterIP"
    - "NodePort"
  required: true
  label: MongoDB Service Type
  show_subquestion_if: "NodePort"
  subquestions:
  - variable: mongodb.service.nodePort
    default: ""
    description: "NodePort port number(to set explicitly, choose port between 30000-32767)"
    show_if: "config.mongo.install=true"
    type: int
    min: 30000
    max: 32767
    label: Service NodePort number
- variable: mongodb.metrics.enabled
  default: true
  description: "Start a side-car prometheus exporter"
  show_if: "config.mongo.install=true"
  type: boolean
  required: true
  label: Enable MongoDB Metrics
  show_subquestion_if: true
  group: "Metrics Settings"
  subquestions:
  - variable: mongodb.metrics.serviceMonitor.enabled
    default: false
    description: "If the operator is installed in your cluster, set to true to create a Service Monitor Entry"
    show_if: "config.mongo.install=true"
    type: boolean
    label: Create ServiceMonitor Resource for Scraping Metrics Using PrometheusOperator
    required: true

3.获取mongodb启动的变量

代码示例:

{{- define "xxx.dbUrl" -}}
{{- if .Values.config.mongo.install -}}
{{- if .Values.mongodb.usePassword -}}
mongodb://{{- .Values.mongodb.mongodbUsername -}}:{{- .Values.mongodb.mongodbPassword -}}@short-url-mongodb-headless:27017/{{- .Values.mongodb.mongodbDatabase -}}
{{- else -}}
mongodb://short-url-mongodb-headless:27017/shorturl
{{- end -}}
{{- else -}}
{{- .Values.db.url -}}
{{- end -}}
{{- end -}}

总结

以上就是今天要讲的内容,本文仅仅简单介绍了mongodb 集成的使用,有问题可以在下方评论中提出。

全部评论