helm命令部署mongodb

部署在ns-demo命名空间下

helm配置 Chart 仓库

helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add azure http://mirror.azure.cn/kubernetes/charts
helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo update   # 类似 yum update

查看配置的存储库

helm repo list

helm repo remove aliyun   # 移除指定的存储库aliyun

部署mongodb

1、查询 MongoDB 资源

$ helm repo update
$ helm search repo mongodb

2、下载安装包至本地,chart的版本14.10.0 (7.0.5)

$ helm pull bitnami/mongodb --version=14.10.0


# 解压缩安装包
$ tar xf mongodb-14.10.0.tgz

# 修改values.yaml文件
$ cd mongodb
$ cp values.yaml values.yaml.bak 
$ vim values.yaml

# 查看配置文件
$ grep -Ev "$^|#" values.yaml

# 查看集群 storageclasses
$ kubectl get sc

3、修改配置文件

$ vim values.yaml 
global:
  # 定义 storageClass 使用的类型
  storageClass: "managed-nfs-storage"

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

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

# 设置集群数量,2个
replicaCount: 2

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

4、安装mongodb

# 进入mongodb的上级目录执行
helm install mongodb-helm mongodb/ -n ns-demo

查看部署的 MongoDB 集群

$ kubectl describe pod -n ns-demo
$ kubectl get pod -n ns-demo
$ helm -n ns-demo
 list
$ kubectl -n ns-demo get pods -l app.kubernetes.io/name=mongodb

查看服务使用的 storageclass

# 查看 pvc
$ kubectl -n ns-demo get pvc

# 查看 pv
$ kubectl get pv

连接 MongoDB 集群 验证服务

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")
  }
}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐