k8s-8:mongodb主从搭建及持久化

该博客详细介绍了如何在Kubernetes环境中设置MongoDB的单节点安装和主从复制。首先,展示了单节点MongoDB的Deployment配置,然后通过ConfigMap和StatefulSet配置文件创建主从复制集群,包括初始化集群和添加从节点的步骤。这为在Kubernetes上实现MongoDB高可用性提供了参考。
摘要由CSDN通过智能技术生成

目录

一、单节点安装

二、主从安装

1.mongodb-master.yaml

2.mongodb-slave.yaml

 配置主从:


一、单节点安装

apiVersion: v1
kind: Service
metadata:
  name: mongodb-nfs-svc
  namespace: default
spec:
  type: NodePort
  ports:
  - name: mongo
    port: 27017
    targetPort: 27017
    nodePort: 30017
    protocol: TCP
  selector:
    app: mongodb

---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: default
  name: mongodb
  labels:
    app: mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      nodeName: node32    # 固定在 k8s-node1 节点
      containers:
      - name: mongodb
        image: mongo:4.2.9

       # resources:
       #   limits:            # 限定资源
       #     cpu: 2000m
       #     memory: 8Gi
       #   requests:
       #     cpu: 100m
       #     memory: 8Gi
 #       env:
 #         - name: MONGO_INITDB_ROOT_USERNAME  # 设置用户名
 #           value: root
 #         - name: MONGO_INITDB_ROOT_PASSWORD  # 设置密码
 #           value: 'klvchen123'
        volumeMounts:
          - mountPath: /bitnami/mongodb
            name: mongodb-volume
      volumes:
        - name: mongodb-volume
          hostPath:
            path: /data/test         # 映射的宿主机目录
            type: DirectoryOrCreate

二、主从安装

1.mongodb-master.yaml

---
kind: ConfigMap
apiVersion: v1
metadata:
  name: mongodb-conf
data:
  mongodb.conf: |
    dbpath=/data/mongodb
    logpath=/data/mongodb/mongodb.log
    pidfilepath=/data/mongodb/master.pid
    directoryperdb=true
    logappend=true
    bind_ip=0.0.0.0
    port=27017


---
kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: mongodb-1
spec:
  replicas: 1
  serviceName: mongodb-1
  selector:
    matchLabels:
      name: mongodb-1
  template:
    metadata:
      labels:
        name: mongodb-1
        role: mongo
        environment: prod
    spec:
      terminationGracePeriodSeconds: 10
      nodeName: pord-k8s-node007
      #      nodeSelector:
      #        kubernetes.io/hostname: pord-k8s-node007
      #   volumes:
      #     - name: mongodb-conf
      #       configMap:
      #         name: mongodb-conf
      containers:
        - name: mongodb-1
          image: mongo:latest
          #    nodeName: pord-k8s-node007
          securityContext:
            runAsUser: 0
          command:
            - mongod
            - "-f"
            - "/data/mongodb/conf/mongodb.conf"
            - "--bind_ip_all"
            - "--replSet"
            - rs0
                  #            - sh
                  #            - '-c'
                  #            - exec mongod -f /data/mongodb/conf/mongodb.conf
          ports:
            - name: mongodb
              containerPort: 27017
              protocol: TCP
          volumeMounts:
            - name: mongodb-conf
              mountPath: /data/mongodb/conf/
            - name: mongodb-volume
              mountPath: /data/mongodb
              #        - name: mongo-sidecar
              #          image: docker.io/bitnami/mongodb-sharded
              #          env:
              #            - name: MONGO_SIDECAR_POD_LABELS
              #              value: "role=mongo,environment=prod"
      volumes:
        - name: mongodb-conf
          configMap:
            name: mongodb-conf
        - name: mongodb-volume
          hostPath:
            path: /data/mongodb
            type: DirectoryOrCreate

2.mongodb-slave.yaml

---
kind: ConfigMap
apiVersion: v1
metadata:
  name: mongodb-conf
data:
  mongodb.conf: |
    dbpath=/data/mongodb
    logpath=/data/mongodb/mongodb.log
    pidfilepath=/data/mongodb/master.pid
    directoryperdb=true
    logappend=true
    bind_ip=0.0.0.0
    port=27017


---
kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: mongodb-2
spec:
  replicas: 1
  serviceName: mongodb-2
  selector:
    matchLabels:
      name: mongodb-2
  template:
    metadata:
      labels:
        name: mongodb-2
    spec:
      terminationGracePeriodSeconds: 10
      nodeName: pord-k8s-node004
      #      nodeSelector:
      #        kubernetes.io/hostname: pord-k8s-node007
      #   volumes:
      #     - name: mongodb-conf
      #       configMap:
      #         name: mongodb-conf
      containers:
        - name: mongodb
          image: mongo:latest
          #    nodeName: pord-k8s-node007
          securityContext:
            runAsUser: 0
          command:
            - mongod
            - "-f"
            - "/data/mongodb/conf/mongodb.conf"
            - "--bind_ip_all"
            - "--replSet"
            - rs0
                  #            - sh
                  #            - '-c'
                  #            - exec mongod -f /data/mongodb/conf/mongodb.conf
          ports:
            - name: mongodb
              containerPort: 27017
              protocol: TCP
          volumeMounts:
            - name: mongodb-conf
              mountPath: /data/mongodb/conf/
            - name: mongodb-volume
              mountPath: /data/mongodb
              #        - name: mongo-sidecar
              #          image: docker.io/bitnami/mongodb-sharded
              #          env:
              #            - name: MONGO_SIDECAR_POD_LABELS
              #              value: "role=mongo,environment=prod"
      volumes:
        - name: mongodb-conf
          configMap:
            name: mongodb-conf
        - name: mongodb-volume
          hostPath:
            path: /data/mongodb
            type: DirectoryOrCreate

 配置主从:

# 进入mongo-1内部、执行一下命令:
#连接mongo数据库
mongo
#初始化集群
rs.initiate({  _id:"rs0", // replSet指定的名称
  members:[{    _id:0,    host:"mongodb-1.prod-zmj-wms.svc.cluster.local:27017" // 主节点ip与端口,
  }]
})

# 在mongodb-1 中把mongodb-2加入集群
rs.add("mongodb-2.prod-zmj-wms.svc.cluster.local:27017")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值