apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: solr-statefulset
  namespace: solr
  labels:
    app: solr
spec:
  replicas: 3 # set this to the number of Solr pod instances you want
  selector:
    matchLabels:
      app: solr
  serviceName: solr-service
  template:
    metadata:
      labels:
        app: solr
    spec:
      securityContext:
        runAsUser: 1001
        fsGroup: 1001
      nodeSelector:
        kubernetes.io/os: linux
        agentpool: solr
      affinity:
        # this causes K8s to only schedule only one Solr pod per node
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                      - solr
              topologyKey: "kubernetes.io/hostname"
      containers:
        - name: solr
          image: solr:8.4
          env:
            # ZK_HOST lists all of the hostnames for all of the Zookeeper instances - this should correspond to however many ZK instances you have running.
            - name: ZK_HOST
              value: zookeeper-statefulset-0.zookeeper-service:2181,zookeeper-statefulset-1.zookeeper-service:2181,zookeeper-statefulset-2.zookeeper-service:2181, etc
            - name: SOLR_JAVA_MEM
              value: "-Xms4g -Xmx4g" # set the JVM memory usage and limit
          ports:
            - name: solr
              containerPort: 8983
          volumeMounts:
            - name: solr-pvc
              mountPath: /var/solr
          livenessProbe:
            # runs a built-in script to check for Solr readiness/liveness
            exec:
              command:
                - /bin/bash
                - -c
                - "/opt/docker-solr/scripts/wait-for-solr.sh"
            initialDelaySeconds: 20
            timeoutSeconds: 5
          readinessProbe:
            # runs a built-in script to check for Solr readiness/liveness
            exec:
              command:
                - /bin/bash
                - -c
                - "/opt/docker-solr/scripts/wait-for-solr.sh"
            initialDelaySeconds: 20
            timeoutSeconds: 5
      initContainers:
        # runs a built-script to wait until all Zookeeper instances are up and running
        - name: solr-zk-waiter
          image: solr:8.4
          command:
            - /bin/bash
            - "-c"
            - "/opt/docker-solr/scripts/wait-for-zookeeper.sh"
          env:
            - name: ZK_HOST
              value: zookeeper-statefulset-0.zookeeper-service:2181,zookeeper-statefulset-1.zookeeper-service:2181,zookeeper-statefulset-2.zookeeper-service:2181
        # runs a built-in script to initialize Solr instance if necessary
        - name: solr-init
          image: solr:8.4
          command:
            - /bin/bash
            - "-c"
            - "/opt/docker-solr/scripts/init-var-solr"
          volumeMounts:
            - name: solr-pv
              mountPath: /var/solr
  volumeClaimTemplates:
    - metadata:
        name: solr-pvc
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: managed-premium
        resources:
          requests:
            storage: 1Gi
