Introduction to the

Let’s look at the life cycle diagramMongodb version supported by Kubedb

[root@qd01-stop-k8s-master001 mysql]# kubectl get mongodbversions NAME VERSION DB_IMAGE DEPRECATED AGE 3.4.17-v1 3.4.17 Kubedb /mongo:3.4.17-v1 46h 3.4.22-v1 3.4.22 kubedb/mongo:3.4.22-v1 46h 3.6.13-v1 3.6.13 kubedb/mongo:3.6.13-v1 46h 3.6.18-percona 3.6.18 percona/percona-server-mongodb:3.6.18 46h 3.6.8-v1 3.6.8 kubedb/mongo:3.6.8-v1 46h 4.0.10-percona Percona /percona-server-mongodb: 4.0.1046h 4.0.11-v1 4.0.11 kubedb/mongo:4.0.11-v1 4.0.3-v1 4.0.3 Kubedb /mongo:4.0.3-v1 46h 4.0.5-v3 46h 4.0.5 kubedb/mongo:4.0.5-v3 46h 4.1.13-v1 4.1.13 kubedb/mongo: 4.13-v1 46h 4.1.4-v1 Kubedb /mongo:4.1.4-v1 46h 4.1.7-v3 4.1.7 kubedb/mongo:4.1.7-v3 46h 4.2.3 kubedb/mongo: 4.2.346h 4.2.7 - percona 4.2.7 percona/percona server - mongo: 4.2.7 46 h - 7Copy the code

MongoDB ReplicaSet

The mongodb replication set schema is shown below

1. Define the configuration file and create secret

cat mongod.conf net: maxIncomingConnections: 10000 [root@qd01-stop-k8s-master001 Replication]# kubectl create secret generic -n op mg-configuration --from-file=./mongod.conf secret/mg-configuration created [root@qd01-stop-k8s-master001 Replication]# kubectl get secret  -n op NAME TYPE DATA AGE mg-configuration Opaque 1 20sCopy the code

Mongod -replicaset.yaml

apiVersion: kubedb.com/v1alpha2
kind: MongoDB
metadata:
  name: mongodb-replicaset
  namespace: op
spec:
  version: "4.2.3"
  replicas: 3
  replicaSet:
    name: rs0
  configSecret:
    name: mg-configuration
  storage:
    storageClassName: "rbd"
    accessModes:
    - ReadWriteOnce
    resources:
      requests:
        storage: 1Gi
Copy the code

3. Perform the installation and view the deployment result

[root@qd01-stop-k8s-master001 Replication]# kubectl apply -f mongod-replicaset.yaml
mongodb.kubedb.com/mongodb-replicaset created

[root@qd01-stop-k8s-master001 Replication]# kubectl get po -n op
NAME                   READY   STATUS    RESTARTS   AGE
mongodb-replicaset-0   2/2     Running   0          24m
mongodb-replicaset-1   2/2     Running   0          22m
mongodb-replicaset-2   2/2     Running   0          20m
Copy the code

4. Verify the cluster

[root@qd01-stop-k8s-master001 Replication]# kubectl get secrets -n demo mgo-replicaset-auth-o jsonpath='{.data.\username}' | base64 -d root [root@qd01-stop-k8s-master001 Replication]# kubectl get secrets -n demo Mgo style - replicaset - auth - o jsonpath = '{) data. \ password}' | base64-123456 # d login mongo - replicaset - 0 check the state of the cluster, [root@qd01-stop-k8s-master001 Replication]# kubectl -n op exec-ti mongodb-replicaset-0 -- /bin/bash root@mongodb-replicaset-0:/# mongo admin-uroot -p123456 MongoDB shell version v4.2.3 Connecting to: Mongo: / / 127.0.0.1:27017 / admin? compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID(" cc1b6a09-F407-44d8-be3B-8b32db4a10B0 ")} MongoDB server version: 4.2.3 Welcome to the MongoDB shell....... Rs0 :PRIMARY> rs.ismaster ().primary Mongo replicaset - 0. Directing - replicaset - pods. Op. SVC. Cluster. The local: 27017 #, speaking, reading and writing data rs0: PRIMARY > use testdb switched to the db Testdb rs0:PRIMARY> show DBS admin 0.000GB config 0.000GB local 0.000GB rs0:PRIMARY> db.movie.insert({"name":"scofield"}); WriteResult({ "nInserted" : 1 }) rs0:PRIMARY> db.movie.find().pretty() { "_id" : ObjectId("604b17469d22a18817a5927e"), "name" : "scofield" }Copy the code

MongoDB Sharding

The mongodb sharding mode architecture is shown in the following figure

Shard: Each shard contains a subset of the shard data. Starting with MongoDB 3.6, shards must be deployed as replica sets. Mongos: Mongos acts as a query router, providing an interface between the client application and the sharded cluster. Config Servers: Configures metadata and configuration Settings for the server storage cluster. Starting with MongoDB 3.4, configuration servers must be deployed as replica sets (CSRS).Copy the code

1. Write mongodb-sharding.yaml

apiVersion: kubedb.com/v1alpha2
kind: MongoDB
metadata:
  name: mongo-sharding
  namespace: op
spec:
  version: 4.2.3
  shardTopology:
    configServer:
      replicas: 3
      storage:
        resources:
          requests:
            storage: 1Gi
        storageClassName: rbd
    mongos:
      replicas: 2
    shard:
      replicas: 3
      shards: 2
      storage:
        resources:
          requests:
            storage: 1Gi
        storageClassName: rbd
Copy the code

2. Perform the deployment and check the deployment status

[root@qd01-stop-k8s-master001 Sharded]# kubectl apply -f mongodb-sharding.yaml mongodb.kubedb.com/mongo-sharding created  [root@qd01-stop-k8s-master001 Sharded]# kubectl get po -n op NAME READY STATUS RESTARTS AGE mongo-sharding-configsvr-0 1/1 Running 0 27m mongo-sharding-configsvr-1 1/1 Running 0 12m mongo-sharding-configsvr-2 1/1 Running 0 9m54s mongo-sharding-mongos-0 1/1 Running 0 7m27s mongo-sharding-mongos-1 1/1 Running 0 4m23s mongo-sharding-shard0-0 1/1 Running 0 27m mongo-sharding-shard0-1 1/1 Running 0 25m mongo-sharding-shard0-2 1/1 Running 0 24m mongo-sharding-shard1-0 1/1 Running 0 27m mongo-sharding-shard1-1 1/1 Running 0 25m mongo-sharding-shard1-2 1/1 Running 0 22mCopy the code

A sharded set requires more resources than a replicated set. This ensures the robustness of the entire cluster and provides larger storage space.

3. Verify the cluster status and read/write

# access password kubectl get secrets - n demo mongo - sh - auth - o jsonpath = '{) data. \ username}' | base64 - d root kubectl get secrets - n demo mongo - sh - auth - o jsonpath = '{) data. \ password}' | base64-123456 # d connected mongo root @ mongo - sharding - mongos - 0: / # mongo The admin -u root -p mongo shell version v4.2.3 Enter password: connecting to: mongo: / / 127.0.0.1:27017 / admin? compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("948eadef-87b6-4ab2-ba5a-c8f4e23689a7") } MongoDB server version: Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has Startup Warnings: 2021-03-12T08:10:48.173+0000 I CONTROL [main] ** WARNING: You are running this process as the root user, 2021-03-12T08:10:48.173+0000 I CONTROL [main] mongos> # check sh.status() -- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("604b201f7fb058e04bb03ef0") } shards: { "_id" : "shard0", "host" : "shard0/mongo-sharding-shard0-0.mongo-sharding-shard0-pods.op.svc.cluster.local:27017,mongo-sharding-shard0-1.mongo-shar ding-shard0-pods.op.svc.cluster.local:27017,mongo-sharding-shard0-2.mongo-sharding-shard0-pods.op.svc.cluster.local:2701 7", "state" : 1 } { "_id" : "shard1", "host" : "shard1/mongo-sharding-shard1-0.mongo-sharding-shard1-pods.op.svc.cluster.local:27017,mongo-sharding-shard1-1.mongo-shar ding-shard1-pods.op.svc.cluster.local:27017,mongo-sharding-shard1-2.mongo-sharding-shard1-pods.op.svc.cluster.local:2701 7", "state" : 1} active mongoses: "4.2.3" : 2 autosplit: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "config", "primary" : "config", "partitioned" : true } config.system.sessions shard key: { "_id" : 1 } unique: false balancing: true chunks: shard0 1 { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : Shard0 Timestamp(1, 0) # sh.enablesharding ("test"); { "ok" : 1, "operationTime" : Timestamp(1615538870, 3), "$clusterTime" : { "clusterTime" : Timestamp(1615538870, 3), "signature" : { "hash" : BinData(0,"CTnuwtgOIn+ke0qqarLQIi+VXz8="), "keyId" : Mongos > sh.shardCollection("test.testcoll", {"myfield": 1}); { "collectionsharded" : "test.testcoll", "collectionUUID" : UUID("313cacbe-014c-4e0a-9112-427de2351bdd"), "ok" : 1, "operationTime" : Timestamp(1615539067, 9), "$clusterTime" : { "clusterTime" : Timestamp(1615539067, 9), "signature" : { "hash" : BinData(0,"1RQN94R6yHvUa0SqBHkiV2hUXNM="), "keyId" : NumberLong("6938674968410456068")}} # write data mongos> db.testcoll. Insert ({" myField ": "scofield", "ageField ": "18"}); WriteResult({ "nInserted" : 1 }) mongos> db.testcoll.insert({"myfield": "amos", "otherfield": "d", "kube" : "db" }); WriteResult({"nInserted" : 1}) # Insert data mongos> db.testcol.find (); { "_id" : ObjectId("604b2d099446ca80f20bab7f"), "myfield" : "scofield", "agefield" : "18" } { "_id" : ObjectId("604b2d4d9446ca80f20bab80"), "myfield" : "amos", "otherfield" : "d", "kube" : "db" }Copy the code