kubernetes使用statefulset部署mongoDB 單機版 自定義配置文件、密碼等


注:

  • 官方鏡像地址: https://hub.docker.com/_/mongo?tab=description

  • docker版的mongo移除了默認的/etc/mongo.conf, 修改了db數據存儲路徑為 /data/db.

  • 創建configmap配置,注意不能加fork=true,否則Pod會變成Completed。

  • 存儲:aliyun nas

  • svc: ClusterIP ? Headless Service ?

資源清單

configmap.yaml

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

storageclass.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: mongodb
  namespace: zisefeizhu
mountOptions:
  - nolock,tcp,noresvport
  - vers=3
parameters:
  volumeAs: subpath
  server: "7131dxxxxxxxxxxxxxxxxxxxxxxcs.com:/mongodb/"
provisioner: nasplugin.csi.alibabacloud.com
reclaimPolicy: Retain

svc.yaml

kind: Service
apiVersion: v1
metadata:
  labels:
    name: mongodb
  name: mongodb
  namespace: zisefeizhu
spec:
  type: ClusterIP
  ports:
    - name: mongodb
      port: 27017
      targetPort: 27017
  selector:
    name: mongodb

statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb
  namespace: zisefeizhu
spec:
  replicas: 1
  podManagementPolicy: OrderedReady  #按照順序啟動或者終止Pod
  serviceName: mongodb
  selector:
    matchLabels:
      name: mongodb
  template:
    metadata:
      labels:
        name: mongodb
    spec:
      containers:
        - name: mongodb
          image: mongo:4.2.1
          command:
            - sh
            - -c
            - "exec mongod -f /data/zisefeizhu/mongodb/conf/mongodb.conf"
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 27017
              name: mongodb
              protocol: TCP
          volumeMounts:
            - name: mongodb-config
              mountPath: /data/zisefeizhu/mongodb/conf/
            - name: data
              mountPath: /data/zisefeizhu/mongodb/
      volumes:
        - name: mongodb-config
          configMap:
            name: mongodb-conf
  volumeClaimTemplates: #定義創建PVC使用的模板
    - metadata:
        name: data
        annotations: #這是指定storageclass
          volume.beta.kubernetes.io/storage-class: mongodb
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 100Gi

測試

# ctl get pods -n zisefeizhu | grep mongodb
mongodb-0   1/1     Running   0          25m

連接

 ctl exec -it mongodb-0 -n zisefeizhu -- mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("de51ecc8-ce78-4e4d-8107-662f3adb0e77") }
MongoDB server version: 4.2.1
Server has startup warnings: 
2021-03-29T07:18:24.047+0000 I  CONTROL  [initandlisten] 
2021-03-29T07:18:24.047+0000 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-03-29T07:18:24.047+0000 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2021-03-29T07:18:24.047+0000 I  CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-03-29T07:18:24.047+0000 I  CONTROL  [initandlisten] 
2021-03-29T07:18:24.047+0000 I  CONTROL  [initandlisten] 
2021-03-29T07:18:24.047+0000 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-03-29T07:18:24.047+0000 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-03-29T07:18:24.047+0000 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM