apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
  namespace: chp10-set1510
spec:
  selector:
    matchLabels:
      app: mongo # match .spec.template.metadata.labels
  updateStrategy: 
    type: RollingUpdate
  serviceName: mongo
  replicas: 3
  template: 
    metadata:
      labels:
        app: mongo
    spec: 
      volumes:
      - name: config
        configMap:
          name: "mongo-init"

      containers:

      # this container inits the mongodb server, then sleeps
      - name: init-mongo
        image: mongo:4.2.10
        command:
        - bash
        - /config/init.sh
        volumeMounts:
        - name: config
          mountPath: /config

      - name: mongodb
        image: mongo:4.2.10
        command:
        - mongod
        - --replSet
        - rs0
        ports:
        - containerPort: 27017
          name: peer
        readinessProbe:
          exec:
            command: 
            - /usr/bin/mongo
            - --eval
            - db.serverStatus()
          initialDelaySeconds: 10
          timeoutSeconds: 10

        # new section
        volumeMounts: # pod container mounts path volume here
        - name: data # match volumeClaimTemplate.metadata.name
          mountPath: /var/data

  # provide stable storage using PersistentVolumes provisioned by a PersistentVolume Provisioner.
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      resources:
        requests:
          storage: 1Mi
      accessModes:
      - ReadWriteOnce


# Reference:
# Kubernetes Up and Running, page 207
# Version 2
# https://github.com/brendandburns/mongo-k8s/blob/master/mongo-cm.yaml
# https://github.com/kubernetes/kubernetes/blob/master/hack/testdata/rollingupdate-statefulset.yaml
