美文网首页
k8s 搭建mongodb

k8s 搭建mongodb

作者: 繁华丶凋零 | 来源:发表于2022-11-02 19:31 被阅读0次

    1.共享盘创建

    mkdir -p /data/mongodb/pv
    cat /etc/exports
    
    
    #/etc/exports内容如下
    /data/mongodb/pv *(rw,sync,no_root_squash)
    /data/mysql/pv *(rw,sync,no_root_squash)
    /data/redis/pv *(rw,sync,no_root_squash)
    
    #重启服务
    systemctl restart rpcbind
    systemctl restart nfs
    

    2.yaml文件

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mongodb-pv
      namespace: default
    spec:
      capacity:
        storage: 1Gi
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Retain
      storageClassName: nfs-mongodb
      nfs:
        path: /data/mongodb/pv
        server: 1.1.1.1
    
    ---
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: mongodb-pvc
      namespace: default
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 1Gi
      storageClassName: nfs-mongodb
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: mongodb-svc
      namespace: default
    spec:
      type: NodePort
      ports:
        - name: mongo
          port: 27017
          targetPort: 27017
          nodePort: 27017
          protocol: TCP
      selector:
        app: mongodb
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mongo-deploy
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mongodb
      template:
        metadata:
          labels:
            app: mongodb
        spec:
          containers:
            - name: mongodb
              image: 1.1.1.1:5000/mongo:5.0.9
              imagePullPolicy: IfNotPresent
              ports:
                - containerPort: 27017
              volumeMounts:
                - name: mongo-pvc
                  mountPath: /data/db
          volumes:
            - name: mongo-pvc
              persistentVolumeClaim:
                claimName: mongodb-pvc
    
    

    3.创建

    kubectl apply -f mongodb.yaml
    

    4.mongo初始化

    #登录 pod
    #登录mongo
    
    #禁止免认证登录
    db.createUser(
      {
        user: "root",
        pwd: "xxxxxxx",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
      }
    )
    
    
    #创建库 用户
    use devops
    db.createUser(
        {
          user: "zhangsan",
          pwd: "yyyyyyy",
          roles: ["readWrite","dbAdmin"]
        }
     )
    
    

    相关文章

      网友评论

          本文标题:k8s 搭建mongodb

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