美文网首页K8sk8s
k8s部署mongo集群

k8s部署mongo集群

作者: 渡边Hok | 来源:发表于2021-09-27 14:38 被阅读0次

    1、环境

    主机列表:
    node1 10.10.10.25
    node2 10.10.10.26
    node3 10.10.10.27

    node1 k8s的管理节点 nfs服务器
    node2 k8s的工作节点
    node3 k8s的工作节点

    2、创建数据目录

    在node1基于nfs创建mongo数据目录:

    mkdir /k8s/
    chmod 777 k8s
    cd k8s
    mkdir /k8s/mongo-1
    mkdir /k8s/mongo-1/data
    mkdir /k8s/mongo-1/key
    mkdir /k8s/mongo-2
    mkdir /k8s/mongo-2/data
    mkdir /k8s/mongo-2/key
    vi /etc/exports
    /k8s/ *(insecure,rw,sync,no_root_squash)
    systemctl restart nfs
    

    3、检查nfs目录是否创建成功

    登陆node2 或者node3执行以下命令

    showmount -e 10.10.10.25
    

    可以看到以下结果


    image.png

    4、编写ConfigMap配置文件

    mongod.conf 配置

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

    5、配置pv

    mongo-1挂载卷配置

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mongo-pv-1
    spec:
      accessModes:
        - ReadWriteOnce
      capacity:
        storage: 3Gi
      nfs:
        path: /k8s/mongo-1
        readOnly: false
        server: 10.10.10.25
    

    mongo-2挂载卷配置

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mongo-pv-2
    spec:
      accessModes:
        - ReadWriteOnce
      capacity:
        storage: 3Gi
      nfs:
        path: /k8s/mongo-2
        readOnly: false
        server: 10.10.10.25
    

    6、创建有状态副本集StatefulSet

    mongo-1有状态副本集

    apiVersion: apps/v1beta1
    kind: StatefulSet
    metadata: 
      namespace: default
      name: mongo-1
      app: mongo-cluster
    spec: 
      selector: 
        matchLabels: 
          name: mongo-1
      serviceName: "mongo-1"
      replicas: 1
      podManagementPolicy: Parallel
      template: 
        metadata: 
          labels: 
            name: mongo-1
            app: mongo-cluster
        spec: 
          terminationGracePeriodSeconds: 10
          affinity: 
             podAntiAffinity: 
               requiredDuringSchedulingIgnoredDuringExecution: 
               - labelSelector: 
                   matchExpressions: 
                   - key: "name"
                     operator: In
                     values: 
                     - mongo
                 topologyKey: "kubernetes.io/hostname"
          containers: 
          - name: mongo
            image: mongo:4.2
            imagePullPolicy: IfNotPresent
            command:  
            - mongod 
            - "-f"
            - "/etc/mongod.conf"
            - "--bind_ip_all"
            - "--replSet"
            - rs0
            ports: 
            - containerPort: 27017
            volumeMounts: 
            - name: mongo-cnf-volume
              mountPath: /etc/mongod.conf/
              subPath: mongod.conf
            - name: mongo-dir 
              mountPath: /mongo
          volumes:
            - name: mongo-cnf-volume     #映射configMap信息
              configMap:
                name: mongodb-conf
                items:
                  - key: mongod.conf
                    path: mongod.conf
    
      volumeClaimTemplates:
      - metadata:
          name: mongo-dir
        spec:
          accessModes: [ "ReadWriteOnce" ]
          resources:
            requests:
              storage: 3Gi
    

    mongo-2有状态副本集

    apiVersion: apps/v1beta1
    kind: StatefulSet
    metadata: 
      namespace: default
      name: mongo-2
      app: mongo-cluster
    spec: 
      selector: 
        matchLabels: 
          name: mongo-2
      serviceName: "mongo-2"
      replicas: 1
      podManagementPolicy: Parallel
      template: 
        metadata: 
          labels: 
            name: mongo-2
            app: mongo-cluster
        spec: 
          terminationGracePeriodSeconds: 10
          affinity: 
             podAntiAffinity: 
               requiredDuringSchedulingIgnoredDuringExecution: 
               - labelSelector: 
                   matchExpressions: 
                   - key: "name"
                     operator: In
                     values: 
                     - mongo
                 topologyKey: "kubernetes.io/hostname"
          containers: 
          - name: mongo
            image: mongo:4.2
            imagePullPolicy: IfNotPresent
            command:  
            - mongod 
            - "-f"
            - "/etc/mongod.conf"
            - "--bind_ip_all"
            - "--replSet"
            - rs0
            ports: 
            - containerPort: 27017
            volumeMounts: 
            - name: mongo-cnf-volume
              mountPath: /etc/mongod.conf/
              subPath: mongod.conf
            - name: mongo-dir
              mountPath: /mongo
          volumes:
            - name: mongo-cnf-volume     #映射configMap信息
              configMap:
                name: mongodb-conf
                items:
                  - key: mongod.conf
                    path: mongod.conf
    
      volumeClaimTemplates:
      - metadata:
          name: mongo-dir
        spec:
          accessModes: [ "ReadWriteOnce" ]
          resources:
            requests:
              storage: 3Gi
    

    7、创建无头服务HeadlessService

    apiVersion: v1
    kind: Service
    metadata:
      namespace: default
      name: mongo-1 #需要与podname同名
      labels:
        name: mongo
    spec:
      ports:
      - name: mongo-port
        port: 27017
      clusterIP: None
      selector:
        name: mongo-1
    
    
    apiVersion: v1
    kind: Service
    metadata:
      namespace: default
      name: mongo-2 #需要与podname同名
      labels:
        name: mongo
    spec:
      ports:
      - name: mongo-port
        port: 27017
      clusterIP: None
      selector:
        name: mongo-2
    
    

    8、创建一个busybox,执行以下命令查看dns信息

    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox
      namespace: default
    spec:
      containers:
      - name: busybox
        image: busybox:1.32
        command:
          - sleep
          - "3600"
        imagePullPolicy: IfNotPresent
      restartPolicy: Always
    

    进入busybox容器内,执行以下命令

    nslookup mongo-1
    
    image.png
    nslookup mongo-2
    
    image.png

    9、初始化集群

    进入mongo-1内部、执行一下命令:

    #连接mongo数据库
    mongo    
    #初始化集群
    rs.initiate({  _id:"rs0", // replSet指定的名称
      members:[{    _id:0,    host:"mongo-1.default.svc.cluster.local:27017" // 主节点ip与端口,
      }]
    })
    
    image.png
    #mongo-2加入集群
    rs.add("mongo-2.default.svc.cluster.local:27017")// 将mongo-2加入集群
    
    image.png

    ok为1为成功。

    10、创建外部访问的NodePort Service

    master NodePort Service

    apiVersion: v1
    kind: Service
    metadata:
      name: mongo-1-front-service
      labels:
        name: mongo-1
      namespace: default
    spec:
      selector:
        name: mongo-1
      type: NodePort
      externalTrafficPolicy: Cluster
      ports:
        - name: mongo-http
          nodePort: 30882
          port: 27017
          protocol: TCP
          targetPort: 27017
    

    slave NodePort Service

    apiVersion: v1
    kind: Service
    metadata:
      name: mongo-2-front-service
      labels:
        name: mongo-2
      namespace: default
    spec:
      selector:
        name: mongo-2
      type: NodePort
      externalTrafficPolicy: Cluster
      ports:
        - name: mongo-http
          nodePort: 30883
          port: 27017
          protocol: TCP
          targetPort: 27017
    

    此时,mongo集群的部署已经完成,可以使用外部客户端访问了。

    相关文章

      网友评论

        本文标题:k8s部署mongo集群

        本文链接:https://www.haomeiwen.com/subject/loqwwltx.html