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

kubernetes使用statefulset部署mongoDB 单机版 自定义配置文件、密码等

上一篇:Oracle 12C 设置pdb随cdb一起启动的方法


下一篇:Java 几种常见数据库连接方式