kubernetes生產實踐之mongodb
- 2021 年 3 月 12 日
- 筆記
- docker;, 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複製集模式架構如下圖
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分片模式架構如下圖
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
和複製集相比,分片集需要更多的資源。以保證整個集群的健壯性,而且能後提供更大容量的存儲空間。
3、驗證集群狀態及讀寫
# 獲取帳號密碼
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 -d
123456
# 連接mongo
root@mongo-sharding-mongos-0:/# mongo admin -u root -p
MongoDB shell version v4.2.3
Enter password:
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("948eadef-87b6-4ab2-ba5a-c8f4e23689a7") }
MongoDB server version: 4.2.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
//docs.mongodb.org/
Questions? Try the support group
//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, which is not recommended.
2021-03-12T08:10:48.173+0000 I CONTROL [main]
mongos>
# 查看分片集群狀態
mongos> 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-sharding-shard0-pods.op.svc.cluster.local:27017,mongo-sharding-shard0-2.mongo-sharding-shard0-pods.op.svc.cluster.local:27017", "state" : 1 }
{ "_id" : "shard1", "host" : "shard1/mongo-sharding-shard1-0.mongo-sharding-shard1-pods.op.svc.cluster.local:27017,mongo-sharding-shard1-1.mongo-sharding-shard1-pods.op.svc.cluster.local:27017,mongo-sharding-shard1-2.mongo-sharding-shard1-pods.op.svc.cluster.local:27017", "state" : 1 }
active mongoses:
"4.2.3" : 2
autosplit:
Currently enabled: 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)
#創建分片
mongos> sh.enableSharding("test");
{
"ok" : 1,
"operationTime" : Timestamp(1615538870, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1615538870, 3),
"signature" : {
"hash" : BinData(0,"CTnuwtgOIn+ke0qqarLQIi+VXz8="),
"keyId" : NumberLong("6938674968410456068")
}
}
}
# 創建集合
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")
}
}
}
# 寫入數據
mongos> db.testcoll.insert({"myfield": "scofield", "agefield": "18"});
WriteResult({ "nInserted" : 1 })
mongos> db.testcoll.insert({"myfield": "amos", "otherfield": "d", "kube" : "db" });
WriteResult({ "nInserted" : 1 })
# 獲取數據
mongos> db.testcoll.find();
{ "_id" : ObjectId("604b2d099446ca80f20bab7f"), "myfield" : "scofield", "agefield" : "18" }
{ "_id" : ObjectId("604b2d4d9446ca80f20bab80"), "myfield" : "amos", "otherfield" : "d", "kube" : "db" }