kubernetes生产实践之mongodb

先看下生命周期图

kubernetes生产实践之mongodb


kubedb支持的mongodb版本

[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 4.0.10 percona/percona-server-mongodb:4.0.10 46h 4.0.11-v1 4.0.11 kubedb/mongo:4.0.11-v1 46h 4.0.3-v1 4.0.3 kubedb/mongo:4.0.3-v1 46h 4.0.5-v3 4.0.5 kubedb/mongo:4.0.5-v3 46h 4.1.13-v1 4.1.13 kubedb/mongo:4.1.13-v1 46h 4.1.4-v1 4.1.4 kubedb/mongo:4.1.4-v1 46h 4.1.7-v3 4.1.7 kubedb/mongo:4.1.7-v3 46h 4.2.3 4.2.3 kubedb/mongo:4.2.3 46h 4.2.7-percona 4.2.7 percona/percona-server-mongodb:4.2.7-7 46h MongoDB ReplicaSet

mongodb复制集模式架构如下图

kubernetes生产实践之mongodb

1、定义配置文件,创建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 20s

2、定义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

3、执行安装并查看部署结果

[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

4、验证集群

# 获取用户名和密码 [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-replicaset-auth -o jsonpath='{.data.\password}' | base64 -d 123456 # 登录mongodb-replicaset-0查看集群状态,并测试读写 [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: mongodb://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> # 查看当前PRIMARY节点 rs0:PRIMARY> rs.isMaster().primary mongodb-replicaset-0.mongodb-replicaset-pods.op.svc.cluster.local:27017 # 读写数据 rs0:PRIMARY> use testdb switched to 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" } MongoDB Sharding

mongodb分片模式架构如下图

kubernetes生产实践之mongodb

shard :每个分片包含分片数据的子集。 从MongoDB 3.6开始,分片必须作为副本集部署。 mongos:mongos充当查询路由器,在客户端应用程序和分片群集之间提供接口。 config servers:配置服务器存储群集的元数据和配置设置。 从MongoDB 3.4开始,配置服务器必须部署为副本集(CSRS)。

1、编写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

2、执行部署,并查看部署状态

[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 22m

和复制集相比,分片集需要更多的资源。以保证整个集群的健壮性,而且能后提供更大容量的存储空间。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspfgz.html