# Helm 安装 MongoDB-分片集群

```bash
helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
```

### 安装分片集群

> 具有默认存储，如果没有存储，设置 persistence=false

```bash
helm upgrade \
--install mongodb-sharded bitnami/mongodb-sharded \
--namespace mongodb-sharded \
--create-namespace \
--set architecture=sharded \
--set shards=3 \
--set shardsvr.dataNode.replicaCount=1 \
--set shardsvr.dataNode.resources.requests.cpu=100m \
--set shardsvr.dataNode.resources.requests.memory=256Mi \
--set shardsvr.dataNode.resources.limits.cpu=4 \
--set shardsvr.dataNode.resources.limits.memory=6G \
--set configsvr.replicaCount=3 \
--set mongos.replicaCount=3 \
--set auth.rootPassword="123456" \
--set service.externalIPs[0]=172.30.10.86
```

• shards：设置分片的数量。

&#x20;• shardsvr.dataNode.replicaCount：设置每个数据节点角色的副本数量。

&#x20;• configsvr.replicaCount：设置配置服务器的副本数量。&#x20;

• mongos.replicaCount：设置路由器的副本数量。

&#x20;• auth.rootPassword: 由于验证是默认开启的, 这里必须设置密码, 管理员账号是root。&#x20;

• service.externalIPs: 暴露节点给外部调用

```bash
## K8S 内部链接
mongodb-sharded.mongodb-sharded.svc.cluster.local


## 获取密码
export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace mongodb-sharded mongodb-sharded -o jsonpath="{.data.mongodb-root-password}" | base64 -d)


## 启动测试客户端
kubectl run --namespace mongodb-sharded mongodb-sharded-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mongodb-sharded:8.0.4-debian-12-r2 --command -- mongosh admin --host mongodb-sharded --authenticationDatabase admin -u root -p $MONGODB_ROOT_PASSWORD

## K8S node 机器上本地转发测试
kubectl port-forward --namespace mongodb-sharded svc/mongodb-sharded 27017:27017 &
    mongosh --host 127.0.0.1 --authenticationDatabase admin -p $MONGODB_ROOT_PASSWORD

```


---

# 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-fen-pian-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.
