tee mongodb.yaml<<-'EOF'
apiVersion: v1
kind: Service
metadata:
name: mongodb-headless
labels:
name: mongo
spec:
ports:
- port: 27017
targetPort: 27017
clusterIP: None
selector:
role: mongo
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongo
spec:
selector:
matchLabels:
role: mongo
environment: test
serviceName: "mongo"
replicas: 3
template:
metadata:
labels:
role: mongo
environment: test
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mongo
image: mongo
command:
- mongod
- "--replSet"
- rs0
- "--bind_ip"
- 0.0.0.0
ports:
- containerPort: 27017
volumeMounts:
- name: mongo-persistent-storage
mountPath: /data/db
- name: mongo-sidecar
image: cvallance/mongo-k8s-sidecar
env:
- name: MONGO_SIDECAR_POD_LABELS
value: "role=mongo,environment=test"
volumeClaimTemplates:
- metadata:
name: mongo-persistent-storage
spec:
storageClassName: "rook-ceph-block"
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 2Gi
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-cs
labels:
name: mongodb
spec:
ports:
- port: 27017
targetPort: 27017
nodePort: 30717
selector:
app: mongodb
type: NodePort
# kubectl get svc | grep mon
mongo ClusterIP None <none> 27017/TCP 3h16m
mongodb-headless ClusterIP None <none> 27017/TCP 3h15m
# nslookup mongodb-headless
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: mongodb-headless
Address 1: 10.244.0.246 mongo-2.mongo.default.svc.cluster.local
Address 2: 10.244.1.236 mongo-1.mongo.default.svc.cluster.local
Address 3: 10.244.2.213 mongo-0.mongo.default.svc.cluster.local
获取到地址开始创建集群
cfg = {_id: "rs0",members:[{_id: 0,host: 'mongo-0.default.svc.cluster.local:27017',priority: 3},{_id: 1,host: 'mongo-1.default.svc.cluster.local:27017',priority: 2},{_id: 2,host: 'mongo-2.default.svc.cluster.local:27017',priority: 1}]};
初始化
rs.initiate(cfg)
查看副本集状态
rs.status()
{
"set" : "rs0",
"date" : ISODate("2022-05-14T16:08:05.875Z"),
"myState" : 2,
"term" : NumberLong(3),
"syncSourceHost" : "10.42.2.72:27017",
"syncSourceId" : 1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1652544480, 1),
"t" : NumberLong(3)
},
"lastCommittedWallTime" : ISODate("2022-05-14T16:08:00.983Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1652544480, 1),
"t" : NumberLong(3)
},
"readConcernMajorityWallTime" : ISODate("2022-05-14T16:08:00.983Z"),
"appliedOpTime" : {
"ts" : Timestamp(1652544480, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1652544480, 1),
"t" : NumberLong(3)
},
"lastAppliedWallTime" : ISODate("2022-05-14T16:08:00.983Z"),
"lastDurableWallTime" : ISODate("2022-05-14T16:08:00.983Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1652544430, 1),
"members" : [
{
"_id" : 0,
"name" : "10.42.1.5:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3993,
"optime" : {
"ts" : Timestamp(1652544480, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1652544480, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2022-05-14T16:08:00Z"),
"optimeDurableDate" : ISODate("2022-05-14T16:08:00Z"),
"lastAppliedWallTime" : ISODate("2022-05-14T16:08:00.983Z"),
"lastDurableWallTime" : ISODate("2022-05-14T16:08:00.983Z"),
"lastHeartbeat" : ISODate("2022-05-14T16:08:05.546Z"),
"lastHeartbeatRecv" : ISODate("2022-05-14T16:08:05.546Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1652540469, 1),
"electionDate" : ISODate("2022-05-14T15:01:09Z"),
"configVersion" : 370424,
"configTerm" : 3
},
{
"_id" : 1,
"name" : "10.42.2.72:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 3993,
"optime" : {
"ts" : Timestamp(1652544480, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1652544480, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2022-05-14T16:08:00Z"),
"optimeDurableDate" : ISODate("2022-05-14T16:08:00Z"),
"lastAppliedWallTime" : ISODate("2022-05-14T16:08:00.983Z"),
"lastDurableWallTime" : ISODate("2022-05-14T16:08:00.983Z"),
"lastHeartbeat" : ISODate("2022-05-14T16:08:05.546Z"),
"lastHeartbeatRecv" : ISODate("2022-05-14T16:08:05.580Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "10.42.1.5:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 370424,
"configTerm" : 3
},
{
"_id" : 2,
"name" : "10.42.0.215:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 4006,
"optime" : {
"ts" : Timestamp(1652544480, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2022-05-14T16:08:00Z"),
"lastAppliedWallTime" : ISODate("2022-05-14T16:08:00.983Z"),
"lastDurableWallTime" : ISODate("2022-05-14T16:08:00.983Z"),
"syncSourceHost" : "10.42.2.72:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 370424,
"configTerm" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
}
到主库写入一些数据
#切换baidu库
rs0:PRIMARY> use baidu;
switched to db baidu
#造
rs0:PRIMARY> for(let i=0;i<30;i++){ db.user.insert({_id:i,name:'公路车_'+i,age:10+i});}
WriteResult({ "nInserted" : 1 })
use baidu;
for(let i=0;i<30;i++){ db.user.insert({_id:i,name:'公路车_'+i,age:10+i});}
rs0:SECONDARY> show tables;
user
rs0:SECONDARY> db.user.find();
{ "_id" : 0, "name" : "公路车_0", "age" : 10 }
{ "_id" : 1, "name" : "公路车_1", "age" : 11 }
{ "_id" : 3, "name" : "公路车_3", "age" : 13 }
{ "_id" : 23, "name" : "公路车_23", "age" : 33 }
{ "_id" : 7, "name" : "公路车_7", "age" : 17 }
从库写需要执行(rs.slaveOk)
rs0:SECONDARY> rs.slaveOk();
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
rs0:SECONDARY> db.user.find();
{ "_id" : 0, "name" : "公路车_0", "age" : 10 }
{ "_id" : 1, "name" : "公路车_1", "age" : 11 }
{ "_id" : 5, "name" : "公路车_5", "age" : 15 }
# 查看有30条数据
rs0:SECONDARY> db.user.count();
30
代码连接
mongodb://ip:port,IP2:port,IP3:port/库名?/reslicaSet=rs0