helm 安装mongo 复制集群

915 阅读5分钟
查询mongo的Chart

root@node1:~# helm search repo mongo
NAME                    CHART VERSION   APP VERSION     DESCRIPTION                                       
bitnami/mongodb         13.9.4          6.0.5           MongoDB(R) is a relational open source NoSQL da...
bitnami/mongodb-sharded 6.3.3           6.0.5           MongoDB(R) is an open source NoSQL database tha...
root@node1:~# 

创建一个部署mongo Chart的配置文件

# vim value-test.yaml

global:
  # 定义 storageClass 使用的类型
  storageClass: "rook-ceph-block"

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

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

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


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

使用helm启动mongo集群

helm install mongodb-cluster bitnami/mongodb -f values-test.yaml 

查看集群部署情况

root@node1:~# helm list
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
mongodb-cluster default         1               2023-04-11 02:48:33.026204208 +0000 UTC deployed        mongodb-13.9.4  6.0.5   

通过k8s查看集群情况

root@node1:~# kubectl get statefulset
NAME                      READY   AGE
mongodb-cluster           3/3     133m
mongodb-cluster-arbiter   1/1     133m
root@node1:~# kubectl get pods -o wide
NAME                        READY   STATUS                   RESTARTS       AGE     IP            NODE    NOMINATED NODE   READINESS GATES
busybox-6f4c6d997c-5cvbd    0/1     ContainerStatusUnknown   1              46m     10.244.3.45   node4   <none>           <none>
busybox-6f4c6d997c-8j4b4    1/1     Running                  1 (3m5s ago)   6m41s   10.244.1.89   node2   <none>           <none>
kali-59ff5d779-6wdfb        1/1     Running                  1 (3m5s ago)   7m14s   10.244.1.93   node2   <none>           <none>
kali-59ff5d779-zk9kf        0/1     ContainerStatusUnknown   1              51m     10.244.3.44   node4   <none>           <none>
mongo-0                     2/2     Running                  0              8m53s   10.244.1.95   node2   <none>           <none>
mongo-1                     2/2     Running                  0              102s    10.244.2.76   node3   <none>           <none>
mongo-2                     2/2     Running                  0              97s     10.244.3.66   node4   <none>           <none>
mongodb-cluster-0           1/1     Running                  0              2m26s   10.244.2.75   node3   <none>           <none>
mongodb-cluster-1           1/1     Running                  0              92s     10.244.1.97   node2   <none>           <none>
mongodb-cluster-2           1/1     Running                  0              35s     10.244.3.68   node4   <none>           <none>
mongodb-cluster-arbiter-0   1/1     Running                  1 (3m5s ago)   10m     10.244.1.84   node2   <none>           <none>
ubuntu-6489df7bcf-dzmrc     0/1     ContainerStatusUnknown   1              23m     10.244.3.53   node4   <none>           <none>
ubuntu-6489df7bcf-fnzpr     1/1     Running                  1 (3m5s ago)   8m19s   10.244.1.90   node2   <none>           <none>
#登录节点查看状态

root@node1:~# mongosh admin  --host "10.244.2.75:27017" --authenticationDatabase admin -u root -p root
Current Mongosh Log ID: 6434ea2fa6a772427bfa1285
Connecting to:          mongodb://<credentials>@10.244.2.75:27017/admin?directConnection=true&authSource=admin&appName=mongosh+1.8.0
Using MongoDB:          6.0.5
Using Mongosh:          1.8.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-04-11T05:00:37.840+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2023-04-11T05:00:38.920+00:00: vm.max_map_count is too low
------

------
   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()
------

rs0 [direct: primary] admin> rs.status()
{
  set: 'rs0',
  date: ISODate("2023-04-11T05:03:55.615Z"),
  myState: 1,
  term: Long("11"),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 3,
  writeMajorityCount: 3,
  votingMembersCount: 4,
  writableVotingMembersCount: 3,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1681189434, i: 1 }), t: Long("11") },
    lastCommittedWallTime: ISODate("2023-04-11T05:03:54.519Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1681189434, i: 1 }), t: Long("11") },
    appliedOpTime: { ts: Timestamp({ t: 1681189434, i: 1 }), t: Long("11") },
    durableOpTime: { ts: Timestamp({ t: 1681189434, i: 1 }), t: Long("11") },
    lastAppliedWallTime: ISODate("2023-04-11T05:03:54.519Z"),
    lastDurableWallTime: ISODate("2023-04-11T05:03:54.519Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1681189414, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate("2023-04-11T05:01:44.484Z"),
    electionTerm: Long("11"),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1681187530, i: 1 }), t: Long("9") },
    numVotesNeeded: 3,
    priorityAtElection: 5,
    electionTimeoutMillis: Long("10000"),
    numCatchUpOps: Long("0"),
    newTermStartDate: ISODate("2023-04-11T05:01:44.512Z"),
    wMajorityWriteAvailabilityDate: ISODate("2023-04-11T05:01:52.526Z")
  },
  members: [
    {
      _id: 0,
      name: 'mongodb-cluster-0.mongodb-cluster-headless.default.svc.cluster.local:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 198,
      optime: { ts: Timestamp({ t: 1681189434, i: 1 }), t: Long("11") },
      optimeDate: ISODate("2023-04-11T05:03:54.000Z"),
      lastAppliedWallTime: ISODate("2023-04-11T05:03:54.519Z"),
      lastDurableWallTime: ISODate("2023-04-11T05:03:54.519Z"),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1681189304, i: 1 }),
      electionDate: ISODate("2023-04-11T05:01:44.000Z"),
      configVersion: 8,
      configTerm: 11,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 1,
      name: 'mongodb-cluster-arbiter-0.mongodb-cluster-arbiter-headless.default.svc.cluster.local:27017',
      health: 1,
      state: 7,
      stateStr: 'ARBITER',
      uptime: 121,
      lastHeartbeat: ISODate("2023-04-11T05:03:54.907Z"),
      lastHeartbeatRecv: ISODate("2023-04-11T05:03:53.864Z"),
      pingMs: Long("5"),
      lastHeartbeatMessage: '',
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      configVersion: 8,
      configTerm: 11
    },
    {
      _id: 2,
      name: 'mongodb-cluster-1.mongodb-cluster-headless.default.svc.cluster.local:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 136,
      optime: { ts: Timestamp({ t: 1681189434, i: 1 }), t: Long("11") },
      optimeDurable: { ts: Timestamp({ t: 1681189434, i: 1 }), t: Long("11") },
      optimeDate: ISODate("2023-04-11T05:03:54.000Z"),
      optimeDurableDate: ISODate("2023-04-11T05:03:54.000Z"),
      lastAppliedWallTime: ISODate("2023-04-11T05:03:54.519Z"),
      lastDurableWallTime: ISODate("2023-04-11T05:03:54.519Z"),
      lastHeartbeat: ISODate("2023-04-11T05:03:54.906Z"),
      lastHeartbeatRecv: ISODate("2023-04-11T05:03:55.513Z"),
      pingMs: Long("13"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'mongodb-cluster-0.mongodb-cluster-headless.default.svc.cluster.local:27017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 8,
      configTerm: 11
    },
    {
      _id: 3,
      name: 'mongodb-cluster-2.mongodb-cluster-headless.default.svc.cluster.local:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 104,
      optime: { ts: Timestamp({ t: 1681189434, i: 1 }), t: Long("11") },
      optimeDurable: { ts: Timestamp({ t: 1681189434, i: 1 }), t: Long("11") },
      optimeDate: ISODate("2023-04-11T05:03:54.000Z"),
      optimeDurableDate: ISODate("2023-04-11T05:03:54.000Z"),
      lastAppliedWallTime: ISODate("2023-04-11T05:03:54.519Z"),
      lastDurableWallTime: ISODate("2023-04-11T05:03:54.519Z"),
      lastHeartbeat: ISODate("2023-04-11T05:03:54.906Z"),
      lastHeartbeatRecv: ISODate("2023-04-11T05:03:54.906Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'mongodb-cluster-1.mongodb-cluster-headless.default.svc.cluster.local:27017',
      syncSourceId: 2,
      infoMessage: '',
      configVersion: 8,
      configTerm: 11
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1681189434, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("57cc10e771bf71af93cd5ae9fbed474e94be7c21", "hex"), 0),
      keyId: Long("7220619024269312006")
    }
  },
  operationTime: Timestamp({ t: 1681189434, i: 1 })
}
rs0 [direct: primary] admin> 

可以看到mongo创建的是一个无头服务,pod内部可以通过DNS访问

root@node1:~# kubectl get service
NAME                               TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)     AGE
kubernetes                         ClusterIP   10.96.0.1    <none>        443/TCP     25h
mongodb-cluster-arbiter-headless   ClusterIP   None         <none>        27017/TCP   96m
mongodb-cluster-headless           ClusterIP   None         <none>        27017/TCP   96m
使用busybox验证

/ # ping mongodb-cluster-0.mongodb-cluster-headless.default.svc.cluster.local
PING mongodb-cluster-0.mongodb-cluster-headless.default.svc.cluster.local (10.244.2.43): 56 data bytes
64 bytes from 10.244.2.43: seq=0 ttl=62 time=0.585 ms
64 bytes from 10.244.2.43: seq=1 ttl=62 time=1.517 ms
64 bytes from 10.244.2.43: seq=2 ttl=62 time=1.336 ms
64 bytes from 10.244.2.43: seq=3 ttl=62 time=0.832 ms

未完待续... 使集群可以外部访问