mongodb statefulset pod 在复制期间重新启动,没有任何有用的错误

问题描述 投票:0回答:1

我尝试在带有复制功能的 minikube 上运行 mongodb statefulset,但 Pod 不断重新启动,没有任何明显的原因。我已经到处搜索试图调试这个问题。以下是我的有状态集,我正在使用 NFS 进行 PVC(这仅用于测试)。

MongoDB Pod 日志:https://pastebin.com/Aq8LiKew

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb
spec:
  selector:
    matchLabels:
      app: mongodb
  serviceName: "mongo"
  replicas: 3
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: mongodb
        image: mongo:4.4.18
        #lifecycle:
        #  postStart:
        #    exec:
        #      command: ["/bin/sh","-c","mongo < /tmp/init.js"]
        args: ["--config","/etc/mongod.conf"]
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongodb-data
          mountPath: /data
        - name: init-scripts
          mountPath: /docker-entrypoint-initdb.d
        - name: keys
          mountPath: /keys
        - name: config
          mountPath: /etc/mongod.conf
          subPath: mongod.conf
        - name: repinit
          mountPath: /tmp/init.js
          subPath: init.js
        env:
          - name: MONGO_INITDB_ROOT_USERNAME
            valueFrom:
              secretKeyRef:
                name: mongoadmin
                key: MONGO_INITDB_ROOT_USERNAME
          - name: MONGO_INITDB_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mongoadmin
                key: MONGO_INITDB_ROOT_PASSWORD
      volumes:
      - name: init-scripts
        configMap:
          name: dbfiles
      - name: keys
        secret:
          secretName: dbkey
          defaultMode: 0400
      - name: config
        configMap:
          name: dbconfig
      - name: repinit
        configMap:
           name: repinit

  volumeClaimTemplates:
  - metadata:
      name: mongodb-data
    spec:
      accessModes: ["ReadWriteMany"]
      storageClassName: nfs-csi
      resources:
        requests:
          storage: 5Gi

k 获取豆荚

NAME        READY   STATUS             RESTARTS        AGE
mongodb-0   0/1     CrashLoopBackOff   3 (16s ago)     2m8s
mongodb-1   0/1     CrashLoopBackOff   7 (2m15s ago)   25m
mongodb-2   0/1     CrashLoopBackOff   7 (2m16s ago)   25m
nginx       1/1     Running            0               28h

k 描述 pod mongodb-0

Name:             mongodb-0
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.59.103
Start Time:       Sat, 17 Feb 2024 17:57:28 +0530
Labels:           app=mongodb
                  controller-revision-hash=mongodb-5dc867c4c7
                  statefulset.kubernetes.io/pod-name=mongodb-0
Annotations:      <none>
Status:           Running
IP:               10.244.0.76
IPs:
  IP:           10.244.0.76
Controlled By:  StatefulSet/mongodb
Containers:
  mongodb:
    Container ID:  docker://168537febead9f2c220e413174a7ab0710b4f1f5268a0ff23c74b19a5b98d3f3
    Image:         mongo:4.4.18
    Image ID:      docker-pullable://mongo@sha256:d23ec07162ca06646a6329c452643f37494af644d045c002a7b41873981c160d
    Port:          27017/TCP
    Host Port:     0/TCP
    Args:
      --config
      /etc/mongod.conf
    State:          Running
      Started:      Sat, 17 Feb 2024 18:01:51 +0530
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Sat, 17 Feb 2024 18:00:06 +0530
      Finished:     Sat, 17 Feb 2024 18:00:25 +0530
    Ready:          True
    Restart Count:  5
    Environment:
      MONGO_INITDB_ROOT_USERNAME:  <set to the key 'MONGO_INITDB_ROOT_USERNAME' in secret 'mongoadmin'>  Optional: false
      MONGO_INITDB_ROOT_PASSWORD:  <set to the key 'MONGO_INITDB_ROOT_PASSWORD' in secret 'mongoadmin'>  Optional: false
    Mounts:
      /data from mongodb-data (rw)
      /docker-entrypoint-initdb.d from init-scripts (rw)
      /etc/mongod.conf from config (rw,path="mongod.conf")
      /keys from keys (rw)
      /tmp/init.js from repinit (rw,path="init.js")
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-lpq4q (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  mongodb-data:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  mongodb-data-mongodb-0
    ReadOnly:   false
  init-scripts:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      dbfiles
    Optional:  false
  keys:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  dbkey
    Optional:    false
  config:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      dbconfig
    Optional:  false
  repinit:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      repinit
    Optional:  false
  kube-api-access-lpq4q:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age                   From               Message
  ----     ------     ----                  ----               -------
  Normal   Scheduled  4m32s                 default-scheduler  Successfully assigned default/mongodb-0 to minikube
  Normal   Pulled     119s (x5 over 4m27s)  kubelet            Container image "mongo:4.4.18" already present on machine
  Normal   Created    117s (x5 over 4m27s)  kubelet            Created container mongodb
  Normal   Started    114s (x5 over 4m26s)  kubelet            Started container mongodb
  Warning  BackOff    50s (x10 over 3m48s)  kubelet            Back-off restarting failed container mongodb in pod mongodb-0_default(14ff7d3c-e08a-47c4-828b-928cd017bd50)

此外,如果我删除配置选项并且初始化脚本仍然正常运行,则此设置可以正常工作

以下是初始化脚本供参考。我只希望它在有状态集的第一个 pod 上运行。

var host = _getEnv("HOSTNAME")
idx = host.split('-')[1]
if (idx!='0') {
        print("exiting early")
        quit(0);
}


const user =  _getEnv('MONGO_INITDB_ROOT_USERNAME')
const pass =  _getEnv('MONGO_INITDB_ROOT_PASSWORD')
db = connect('mongodb://'+user+':'+pass+'@localhost/admin');
let i = 0;
var file = cat('/docker-entrypoint-initdb.d/users.json');
var myusers = JSON.parse(file);
while (i < myusers.length) {
        if (db.getUser(myusers[i].user) == null) {
                db.createUser(myusers[i]);
        }
        i++;
}

db = db.getSiblingDB("testdb1");
if (db.test.countDocuments({}) == 0) {
        var datastr = cat('/docker-entrypoint-initdb.d/db1.json')
        var data = JSON.parse(datastr)
        db.test.insertMany(data)
}
db = db.getSiblingDB("testdb2")
if (db.test.countDocuments({}) == 0) {
        var datastr = cat('/docker-entrypoint-initdb.d/db2.json')
        var data = JSON.parse(datastr)
        db.test.insertMany(data)
}
mongodb kubernetes replication persistent-volumes kubernetes-statefulset
1个回答
0
投票

在你的脚本中我看到了一些可能有问题的点,但是我没有测试它们是否真的会导致任何错误:

为什么使用

connect(...)
?我假设,当您运行脚本时,您已经连接到数据库。

启用身份验证后,身份验证将在您创建第一个管理员用户时激活。这意味着在命令

{"createUser": "mongoadmin", ...}
之后,您必须使用 db.auth() 才能继续。否则,所有命令都将失败并出现“需要身份验证”错误。但是,我对 kubernetes 一无所知,因此我不知道第一个 admin/roor 用户是何时何地创建的。

我认为

db
是对您当前数据库的引用。你不应该覆盖它。更好用

testdb1 = db.getSiblingDB("testdb1");
testdb1.test.countDocuments({}) ...

或直接

db.getSiblingDB("testdb1").test.countDocuments({})

没有理由将脚本传递给 STDIN,即使用

mongo < /tmp/init.js
。 mongo shell 可以运行 shell 脚本。只需使用
mongo /tmp/init.js --username username --password ...
,那么您也不会对引号或转义序列感到头疼。

您的密码是否包含特殊字符?它大多数不包含

: / ? # [ ] @
。使用
encodeURI(pass)
转义这些字符。

© www.soinside.com 2019 - 2024. All rights reserved.