# helm 安装 MongoDB 集群

### 资源清单 <a href="#tid-hzeweb" id="tid-hzeweb"></a>

> 本文安装 `MongoDB` 依赖 `K8S集群` 和 `helm` ，本文不提供 `K8S集群` 和 `helm` 安装方式

> 使用此文档部署，需要自行解决 storageClass 问题 （ NFS, ceph, openebs等 ）

| 软件         | 版本      |
| ---------- | ------- |
| chart      | 12.1.11 |
| mongodb    | 5.0.8   |
| kubernetes | v1.19.3 |
| helm       | v3.8.1  |

### `helm` 安装 `MongoDB 3副本集` <a href="#tid-wdnf8q" id="tid-wdnf8q"></a>

#### 1. 添加 `bitnami` 的仓库 <a href="#tid-sh8wdj" id="tid-sh8wdj"></a>

```sh
$ helm repo add bitnami https://charts.bitnami.com/bitnami
```

#### 2. 查询 `MongoDB` 资源 <a href="#tid-ninzjy" id="tid-ninzjy"></a>

```sh
$ helm repo update
$ helm search repo mongodb
NAME                   	CHART VERSION	APP VERSION	DESCRIPTION                                       
bitnami/mongodb        	12.1.11      	5.0.8      	MongoDB(R) is a relational open source NoSQL da...
bitnami/mongodb-sharded	5.0.5        	5.0.8      	MongoDB(R) is an open source NoSQL database tha...
bitnami/mean           	6.1.2        	4.6.2      	DEPRECATED MEAN is a free and open-source JavaS...
```

#### 3. 拉取 `MongoDB chart` 到本地 <a href="#tid-pxpmhz" id="tid-pxpmhz"></a>

```sh
$ mkdir /root/mongodb && cd /root/mongodb

# 拉取 chart 到本地 /root/mongodb 目录
$ helm pull bitnami/mongodb --version 12.1.11


$ tar -xvf mongodb-12.1.11.tgz
$ cp mongodb/values.yaml ./values-test.yaml

# 查看当前目录层级
$ tree -L 2
.
├── mongodb
│   ├── Chart.lock
│   ├── charts
│   ├── Chart.yaml
│   ├── README.md
│   ├── templates
│   ├── values.schema.json
│   └── values.yaml
├── mongodb-12.1.11.tgz
└── values-test.yaml
```

#### 4. 对本地 `values-test.yaml` 修改 <a href="#tid-asn3rx" id="tid-asn3rx"></a>

* 查看集群 `storageclasses`

```sh
$ kubectl get storageclasses.storage.k8s.io 
NAME                   PROVISIONER           RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
openebs-device         openebs.io/local      Delete          WaitForFirstConsumer   false                  34d
openebs-hostpath       openebs.io/local      Delete          WaitForFirstConsumer   false                  34d
openebs-jiva-default   jiva.csi.openebs.io   Delete          Immediate              true                   33d
```

* 修改配置

```sh
$ cat values-test.yaml 

## 配置文件中定义 storageClass: ""，会使用集群配置的 openebs 提供的 storageClass，
## 使用此文档部署，需要自行解决 storageClass 问题 (ceph, nfs, 公有云提供的 nfs)

global:
  # 定义 storageClass 使用的类型
  storageClass: "openebs-jiva-default"

# 定义 mongodb 集群为副本集模式
architecture: replicaset

# 启动集群认证功能，设置超级管理员账户密码
auth:
  enabled: true
  rootUser: root
  rootPassword: "root"

# 设置集群数量，3个
replicaCount: 3

# 定义 pod 的 nodeSelector
nodeSelector: { "node": "middleware" }

# 启用持久化存储，使用 global.storageClass 自动创建 pvc 
persistence:
  enabled: true
  size: 20Gi
```

#### 5. 安装 `MongoDB 集群` <a href="#tid-zbis7n" id="tid-zbis7n"></a>

```sh
# 创建 test-middleware 名称空间
$ kubectl create ns test-middleware

# 安装 MongoDB 集群
$ helm -n test-middleware install mongodb-cluster mongodb -f value-test.yaml

## helm -n NAMESAPCE install SERVER_NAME FILE_NAME -f CONFIG_FILE
-n 指定 kubernetes 集群名称空间
-f 指定使用的配置文件，文件中定义的配置可以覆盖 mongodb/values.yaml 文件中配置


NAME: mongodb-cluster
LAST DEPLOYED: Thu May 26 10:46:14 2022
NAMESPACE: test-middleware
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: mongodb
CHART VERSION: 12.1.11
APP VERSION: 5.0.8

** Please be patient while the chart is being deployed **

MongoDB&reg; can be accessed on the following DNS name(s) and ports from within your cluster:

    mongodb-cluster-0.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017
    mongodb-cluster-1.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017
    mongodb-cluster-2.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017

To get the root password run:

    export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace test-middleware mongodb-cluster -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)

To connect to your database, create a MongoDB&reg; client container:

    kubectl run --namespace test-middleware mongodb-cluster-client --rm --tty -i --restart='Never' --env="MONGODB_ROOT_PASSWORD=$MONGODB_ROOT_PASSWORD" --image docker.io/bitnami/mongodb:5.0.8-debian-10-r24 --command -- bash

Then, run the following command:
    mongosh admin --host "mongodb-cluster-0.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017,mongodb-cluster-1.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017,mongodb-cluster-2.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017" --authenticationDatabase admin -u root -p $MONGODB_ROOT_PASSWORD
```

#### 6. 查看部署的 `MongoDB` 集群 <a href="#tid-sgpehw" id="tid-sgpehw"></a>

```sh
$ helm -n test-middleware list
NAME                   	NAMESPACE     	REVISION	UPDATED                                	STATUS  	CHART               	APP VERSION
mongodb-cluster        	test-middleware	1       	2022-05-26 10:46:14.388002385 +0800 CST	deployed	mongodb-12.1.11     	5.0.8

$ kubectl -n test-middleware get pods -l app.kubernetes.io/name=mongodb
NAME                        READY   STATUS    RESTARTS   AGE
mongodb-cluster-0           1/1     Running   0          77m
mongodb-cluster-1           1/1     Running   0          77m
mongodb-cluster-2           1/1     Running   0          76m
mongodb-cluster-arbiter-0   1/1     Running   0          77m
```

* 查看服务使用的 `storageclass`

```sh
# 查看 pvc
$ kubectl -n test-middleware get pvc
NAME                                          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS        AGE
datadir-mongodb-cluster-0                     Bound    pvc-d6a4c7d8-ede5-4545-8b8c-fd471025aea6   20Gi       RWO            nfs-dynamic-class   78m
datadir-mongodb-cluster-1                     Bound    pvc-0b9a6c9b-7a32-4aac-9684-21c633345537   20Gi       RWO            nfs-dynamic-class   77m
datadir-mongodb-cluster-2                     Bound    pvc-62614e7b-b20e-4ea7-973a-72213d868a6b   20Gi       RWO            nfs-dynamic-class   77m

# 查看 pv
$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                                        STORAGECLASS        REASON   AGE
pvc-0b9a6c9b-7a32-4aac-9684-21c633345537   20Gi       RWO            Delete           Bound    test-middleware/datadir-mongodb-cluster-1                     nfs-dynamic-class            78m
pvc-62614e7b-b20e-4ea7-973a-72213d868a6b   20Gi       RWO            Delete           Bound    test-middleware/datadir-mongodb-cluster-2                     nfs-dynamic-class            78m
pvc-d6a4c7d8-ede5-4545-8b8c-fd471025aea6   20Gi       RWO            Delete           Bound    test-middleware/datadir-mongodb-cluster-0                     nfs-dynamic-class            78m
```

#### 7. 连接 `MongoDB 集群` 验证服务 <a href="#tid-pphcbj" id="tid-pphcbj"></a>

```sh
mongosh admin --host "mongodb-cluster-0.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017,mongodb-cluster-1.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017,mongodb-cluster-2.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017" --authenticationDatabase admin -u root -p $MONGODB_ROOT_PASSWORD

# 获取 Mongodb 集群的密码
$ kubectl get secret --namespace test-middleware mongodb-cluster -o jsonpath="{.data.mongodb-root-password}" | base64 --decode
root

# 启动一个临时容器
$ export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace test-middleware mongodb-cluster -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)
$ kubectl run --namespace test-middleware mongodb-cluster-client --rm --tty -i --restart='Never' --env="MONGODB_ROOT_PASSWORD=$MONGODB_ROOT_PASSWORD" --image docker.io/bitnami/mongodb:5.0.8-debian-10-r24 --command -- bash

## 登陆 Mongodb Cluster
$ mongosh admin --host "mongodb-cluster-0.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017,mongodb-cluster-1.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017,mongodb-cluster-2.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017" --authenticationDatabase admin -u root -p $MONGODB_ROOT_PASSWORD

rs0 [primary] admin> rs.conf()
{
  _id: 'rs0',
  version: 8,
  term: 2,
  members: [
    {
      _id: 0,
      host: 'mongodb-cluster-0.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 5,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    },
    {
      _id: 1,
      host: 'mongodb-cluster-arbiter-0.mongodb-cluster-arbiter-headless.test-middleware.svc.cluster.local:27017',
      arbiterOnly: true,
      buildIndexes: true,
      hidden: false,
      priority: 0,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    },
    {
      _id: 2,
      host: 'mongodb-cluster-1.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    },
    {
      _id: 3,
      host: 'mongodb-cluster-2.mongodb-cluster-headless.test-middleware.svc.cluster.local:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    }
  ],
  protocolVersion: Long("1"),
  writeConcernMajorityJournalDefault: true,
  settings: {
    chainingAllowed: true,
    heartbeatIntervalMillis: 2000,
    heartbeatTimeoutSecs: 10,
    electionTimeoutMillis: 10000,
    catchUpTimeoutMillis: -1,
    catchUpTakeoverDelayMillis: 30000,
    getLastErrorModes: {},
    getLastErrorDefaults: { w: 1, wtimeout: 0 },
    replicaSetId: ObjectId("628eea0fdd0ee8dc91a88441")
  }
}
```

### 参考文档 <a href="#tid-37afdd" id="tid-37afdd"></a>

```
https://artifacthub.io/packages/helm/bitnami/mongodb
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/kubernetes/helm/helm-an-zhuang-mongodb-ji-qun.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
