close

 

1. 下載 centos7 的 image

 

2. 在裡面安裝 mongodb 3.6

 

3. 編輯設定檔和產生 keyfile

 

# mongod.conf

# for documentation of all options, see:

#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.

systemLog:

  destination: file

  logAppend: true

  #path: /var/log/mongodb/mongod.log

  path: /data/db/mongod.log

# Where and how to store data.

storage:

  #dbPath: /var/lib/mongo

  dbPath: /data/db

  journal:

    enabled: true

#  engine:

#  mmapv1:

#  wiredTiger:

# how the process runs

processManagement:

  fork: false  # fork and run in background

  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

  timeZoneInfo: /usr/share/zoneinfo

# network interfaces

net:

  port: 27017

  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

security:

  authorization: enabled

  keyFile: /data/db/mongo-keyfile

#operationProfiling:

replication:

  replSetName: stag

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

 

 

4.產生 keyfile

openssl rand -base64 741 > mongo-keyfile
chmod 600 mongo-keyfile

 

5. 把變更過的 docker 存成 image

docker commit -m "xxx" -a "xxxx" 12345678 mongodb:stag

 

6. 在 kubernetes 上運行新的 image

 

apiVersion: apps/v1beta1

kind: StatefulSet

metadata:

  name: mongo-stag-s0

  namespace: stag

spec:

  serviceName: mongo-stag-0

  replicas: 1

  template:

    metadata:

      labels:

        app: mongo-stag-p0

    spec:

      containers:

      - name: mongo-container0

        image: mongodb:stag

        command: ["/bin/bash","-c"]

        args: ["/opt/mongodb/bin/mongod -f /data/db/mongod.conf"]

        #imagePullPolicy: Always

        ports:

        - name: mongo-port0

          containerPort: 27017

        volumeMounts:

          - name: mongo-stag-v0

            mountPath: /data/db

        livenessProbe:

          tcpSocket:

            port: 27017

          initialDelaySeconds: 15

          timeoutSeconds: 30

      nodeSelector:

        role: db

        env: stag

      #imagePullSecrets:

      #  - name: regsecret

  volumeClaimTemplates:

  - metadata:

      name: mongo-stag-v0

    spec:

      accessModes: [ "ReadWriteOnce" ]

      storageClassName: glusterfs-sc

      resources:

        requests:

          storage: 20Gi

 

7. 把 mongod.conf 和 mongo-keyfile 搬到 /daba/db

 

 

8. 登入 mongo db ,建立管理帳號

 

use admin

db.createUser({
  user: "root",
  pwd: "123456",
    roles: [{
    "role": "root",
    "db": "admin"
  }]
})

 

9. 重啟 mongo db 的 pod

 

10.登入 mongo db ,執行以下指令

rs.initiate()

rs.add("node2")

rs.add("node3)"

rs.status()

 

11. 測試 replica 是否成功

 

Primary node:

use test

db.test.insert({"name" : "test", "value" : 2222})

db.test.find()

 

Slave node:

db.getMongo().setSlaveOk()

use test

db.test.find()

 

arrow
arrow
    文章標籤
    mongodb kubernetes
    全站熱搜

    johnchen6927 發表在 痞客邦 留言(0) 人氣()