无法使用statefulset和sidecar为mongodb创建只读副本

时间:2019-04-05 09:51:14

标签: mongodb azure kubernetes azure-aks kubernetes-mongodb-sidecar

我试图通过创建副本使现有的mongodb具有高可用性。 为了执行它,我引用了enter link description here

以下是我的状态集

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: skeleton-mongodb
spec:
  serviceName: skeleton-mongodb
  replicas: 3
  selector:
    matchLabels:
      app: skeleton-mongodb
  template:
    metadata:
      labels:
        app: skeleton-mongodb
    spec:
      containers:
        - name: mongodb
          image: mongo:4
          resources:
            requests:
              memory: 250Mi
              cpu: 100m
            limits:
              memory: 500Mi
          ports:
            - containerPort: 27017
              name: mongo
          args:
            - "--bind_ip_all"
            - "--replSet"
            - rs0
          volumeMounts:
            - name: skeleton-mongodb-data
              mountPath: /data/db
            - name: skeleton-mongodb-init
              mountPath: /docker-entrypoint-initdb.d
          env:
            - name: MONGO_INITDB_ROOT_USERNAME
              valueFrom:
                configMapKeyRef:
                  name: skeleton
                  key: mongo_root-username
            - name: MONGO_INITDB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: skeleton
                  key: mongodb_root-password
            - name: MONGO_INITDB_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: skeleton
                  key: mongo_db
            - name: MONGO_USERNAME
              valueFrom:
                configMapKeyRef:
                  name: skeleton
                  key: mongo_username
            - name: MONGO_USER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: skeleton
                  key: mongodb_user-password
        - name: mongo-sidecar
          image: cvallance/mongo-k8s-sidecar
          env:
            - name: MONGO_SIDECAR_POD_LABELS
              value: "app=skeleton-mongodb"
            - name: MONGODB_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: skeleton
                  key: mongo_db
            - name: MONGODB_USERNAME
              valueFrom:
                configMapKeyRef:
                  name: skeleton
                  key: mongo_username
            - name: MONGODB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: skeleton
                  key: mongodb_user-password
      volumes:
      - name: skeleton-mongodb-init
        configMap:
          name: common-mongodb-init
  volumeClaimTemplates:
    - metadata:
        name: skeleton-mongodb-data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi
        storageClassName: managed-premium

当我尝试找到哪个吊舱是我的主人时,我找不到任何吊舱作为主人。

for ((i = 0; i < 3; ++i)); 
do 
   kubectl exec --namespace default skeleton-mongodb-$i -- sh -c 'mongo --eval="printjson(rs.isMaster())"'; 
done

我要得到所有三个豆荚

{
        "ismaster" : false,
        "secondary" : false,
        "info" : "Does not have a valid replica set config",
        "isreplicaset" : true,
        ....
        "readOnly" : false,
        "ok" : 1
}

不确定我是否想念东西

1 个答案:

答案 0 :(得分:0)

我对MongoDB的了解还不足以提供建议,但是使用StatefulSet,您将为所有副本使用完全相同的配置。