MongoDB副本集

前提 nfs及class已经创建ok

cluster-mongo.yaml

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:3.6.22
        command:
          - mongod
          - "--replSet"
          - rs0
          - "--bind_ip"
          - 0.0.0.0
          - "--smallfiles"
          - "--noprealloc"
        ports:
          - containerPort: 27017
        volumeMounts:
          - name: mongo-persistent-storage
            mountPath: /data/db
      - name: mongo-sidecar
        image: cvallance/mongo-k8s-sidecar:latest
        env:
          - name: MONGO_SIDECAR_POD_LABELS
            value: "role=mongo,environment=test"
  volumeClaimTemplates:
    - metadata:
        name: mongo-persistent-storage
      spec:
        storageClassName: "cluster-mongo"
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 20Gi
---
apiVersion: v1
kind: Service
metadata:
  name: mongo
  labels:
    name: mongo
spec:
  ports:
    - port: 27017
      targetPort: 27017
  clusterIP: None
  selector:
    role: mongo
---
apiVersion: v1
kind: Service
metadata:
  name: mongo-cs
  labels:
    name: mongo
spec:
  ports:
    - port: 27017
      targetPort: 27017
#nodePort访问
      nodePort: 30717
  selector:
    role: mongo
  type: NodePort
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: mongo-default-view 
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: view
subjects:
  - kind: ServiceAccount
    name: default
    namespace: default

验证

 kubectl exec -it mongo-0 -- mongo
rs0:SECONDARY>  rs.status()
{
    "set": "rs0",
    "date": ISODate("2022-10-06T07:58:25.67Z"),
    "myState": NumberInt("2"),
    "term": NumberLong("1"),
    "syncingTo": "10.244.2.3:27017",
    "syncSourceHost": "10.244.2.3:27017",
    "syncSourceId": NumberInt("0"),
    "heartbeatIntervalMillis": NumberLong("2000"),
    "optimes": {
        "lastCommittedOpTime": {
            "ts": Timestamp(1665043104, 1),
            "t": NumberLong("1")
        },
        "readConcernMajorityOpTime": {
            "ts": Timestamp(1665043104, 1),
            "t": NumberLong("1")
        },
        "appliedOpTime": {
            "ts": Timestamp(1665043104, 1),
            "t": NumberLong("1")
        },
        "durableOpTime": {
            "ts": Timestamp(1665043104, 1),
            "t": NumberLong("1")
        }
    },
    "members": [
        {
            "_id": NumberInt("0"),
            "name": "10.244.2.3:27017",
            "health": 1,
            "state": NumberInt("1"),
            "stateStr": "PRIMARY",
            "uptime": NumberInt("1770"),
            "optime": {
                "ts": Timestamp(1665043104, 1),
                "t": NumberLong("1")
            },
            "optimeDurable": {
                "ts": Timestamp(1665043104, 1),
                "t": NumberLong("1")
            },
            "optimeDate": ISODate("2022-10-06T07:58:24.000Z"),
            "optimeDurableDate": ISODate("2022-10-06T07:58:24.000Z"),
            "lastHeartbeat": ISODate("2022-10-06T07:58:24.576Z"),
            "lastHeartbeatRecv": ISODate("2022-10-06T07:58:25.52Z"),
            "pingMs": NumberLong("0"),
            "lastHeartbeatMessage": "",
            "syncingTo": "",
            "syncSourceHost": "",
            "syncSourceId": NumberInt("-1"),
            "infoMessage": "",
            "electionTime": Timestamp(1665041212, 2),
            "electionDate": ISODate("2022-10-06T07:26:52.000Z"),
            "configVersion": NumberInt("8")
        },
        {
            "_id": NumberInt("1"),
            "name": "10.244.1.3:27017",
            "health": 1,
            "state": NumberInt("2"),
            "stateStr": "SECONDARY",
            "uptime": NumberInt("1795"),
            "optime": {
                "ts": Timestamp(1665043104, 1),
                "t": NumberLong("1")
            },
            "optimeDate": ISODate("2022-10-06T07:58:24.000Z"),
            "syncingTo": "10.244.2.3:27017",
            "syncSourceHost": "10.244.2.3:27017",
            "syncSourceId": NumberInt("0"),
            "infoMessage": "",
            "configVersion": NumberInt("8"),
            "self": true,
            "lastHeartbeatMessage": ""
        },
        {
            "_id": NumberInt("2"),
            "name": "10.244.3.5:27017",
            "health": 1,
            "state": NumberInt("2"),
            "stateStr": "SECONDARY",
            "uptime": NumberInt("1679"),
            "optime": {
                "ts": Timestamp(1665043104, 1),
                "t": NumberLong("1")
            },
            "optimeDurable": {
                "ts": Timestamp(1665043104, 1),
                "t": NumberLong("1")
            },
            "optimeDate": ISODate("2022-10-06T07:58:24.000Z"),
            "optimeDurableDate": ISODate("2022-10-06T07:58:24.000Z"),
            "lastHeartbeat": ISODate("2022-10-06T07:58:24.575Z"),
            "lastHeartbeatRecv": ISODate("2022-10-06T07:58:24.657Z"),
            "pingMs": NumberLong("0"),
            "lastHeartbeatMessage": "",
            "syncingTo": "10.244.2.3:27017",
            "syncSourceHost": "10.244.2.3:27017",
            "syncSourceId": NumberInt("0"),
            "infoMessage": "",
            "configVersion": NumberInt("8")
        }
    ],
    "ok": 1,
    "operationTime": Timestamp(1665043104, 1),
    "$clusterTime": {
        "clusterTime": Timestamp(1665043104, 1),
        "signature": {
            "hash": BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId": NumberLong("0")
        }
    }
}


用户

rs0:PRIMARY> use admin  #切换到admin数据库
switched to db admin
rs0:PRIMARY> db.createUser({user:"admin",pwd:"12300",roles:["root"]})  #创建用户名为admin、密码为password、角色为root的用户;
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
rs0:PRIMARY> db.getCollectionNames()   #新增的用户在system.users;
[ "system.users", "system.version" ]
rs0:PRIMARY> db.auth("admin", "12300")  #第一个用户添加完成后,便需要认证才能继续添加其他用户;
1


rs0:PRIMARY> use test; #创建test数据库;
switched to db test
rs0:PRIMARY> db.users.insertMany( [{ name: "bob", age: 42, status: "A", },{ name: "ahn", age: 22, status: "A", },{ name: "xi", age: 34, status: "D", }])
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("61040bba1ce3378f75980aba"),
        ObjectId("61040bba1ce3378f75980abb"),
        ObjectId("61040bba1ce3378f75980abc")
    ]
}
rs0:PRIMARY> show collections; #查询集合
users
rs0:PRIMARY>  db.users.find() #查询users集合中的数据;
{ "_id" : ObjectId("61040bba1ce3378f75980aba"), "name" : "bob", "age" : 42, "status" : "A" }
{ "_id" : ObjectId("61040bba1ce3378f75980abb"), "name" : "ahn", "age" : 22, "status" : "A" }
{ "_id" : ObjectId("61040bba1ce3378f75980abc"), "name" : "xi", "age" : 34, "status" : "D" }
# mongodb 创建普通用户
rs0:PRIMARY> use test
switched to db test
rs0:PRIMARY> db.createUser({user: "test", pwd: "123456", roles: [{ role: "dbOwner", db: "test" }]})
Successfully added user: {
    "user" : "test",
    "roles" : [
        {
            "role" : "dbOwner",
            "db" : "test"
        }
    ]
}


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,639评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,277评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,221评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,474评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,570评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,816评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,957评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,718评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,176评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,511评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,646评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,322评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,934评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,755评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,987评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,358评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,514评论 2 348

推荐阅读更多精彩内容