目录

一、单节点安装

二、主从安装

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")

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐