美文网首页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部署DOClever

    开发组采用doclever进行开发文档规范管理,将服务运行在k8s集群中1.先部署mongodbvim mongo...

  • 部署k8s 1.22.2 集群 && Euler部署k8s 1

    部署k8s 1.22.2 集群 Euler部署k8s 1.22.2 集群 一、基础环境 主机名IP地址角色系统ma...

  • k8s部署mongo集群

    1、环境 主机列表:node1 10.10.10.25node2 10.10.10.26node3 10.1...

  • 一文学会 K8S故障处理

    1 集群故障概述 在k8s集群的部署过程中,大家可能会遇到很多问题。这也是本地部署k8s集群遇到的最大挑战,因此本...

  • k8s-访问外网服务的两种方式

    需求 k8s集群内的pod需要访问mysql,由于mysql的性质,不适合部署在k8s集群内,故k8s集群内的应用...

  • k8s部署redis集群

    一、部署方式 k8s 以statefulset方式部署redis集群 二、statefulset简介 Statef...

  • k8s部署zookeeper集群

    一、部署方式 k8s 以statefulset方式部署zookeeper集群 二、statefulset简介 St...

  • Gitlab添加K8S集群

    本文介绍如何在Gitlab项目中添加K8S集群,以便使用K8S集群部署gitlab-runner帮我们运行gitl...

  • K8S Fluentd Mongo日志采集

    K8S Fluentd Mongo日志采集 项目最近需要对K8S集群的容器日志进行统计采集,再汇聚起来持久化。最近...

  • kubeadm部署kubernetes集群

    使用kubeadm部署kubernetes集群 kubeadm是k8s官方推出的一个快速部署集群的工具,通过两条命...

网友评论

    本文标题:k8s部署mongo集群

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