---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: elasticsearch-single
spec:
  serviceName: "elasticsearch"
  replicas: 1
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      # Asegura permisos adecuados para lectura/escritura en volumen
      securityContext:
        fsGroup: 1000
      containers:
        - name: elasticsearch
          image: docker.elastic.co/elasticsearch/elasticsearch:8.9.0
          securityContext:
            runAsUser: 1000
          resources:
            requests:
              memory: "2Gi"
              cpu: "1"
            limits:
              memory: "4Gi"
              cpu: "2"
          env:
            # Configuración para un solo nodo
            - name: discovery.type
              value: "single-node"
            # Desactiva la seguridad interna (si no la necesitas)
            - name: xpack.security.enabled
              value: "false"
            # Ajusta la memoria de la JVM
            - name: ES_JAVA_OPTS
              value: "-Xms2g -Xmx2g"
          volumeMounts:
            - name: esdata
              mountPath: /usr/share/elasticsearch/data
          
          # 🔹 Liveness Probe: Evita reinicios prematuros
          livenessProbe:
            httpGet:
              path: /_cluster/health
              port: 9200
            initialDelaySeconds: 120  # Tiempo extra para arranque
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 6

          # 🔹 Readiness Probe: Asegura que el pod esté listo antes de recibir tráfico
          readinessProbe:
            httpGet:
              path: /_cluster/health
              port: 9200
            initialDelaySeconds: 90
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 6

  volumeClaimTemplates:
    - metadata:
        name: esdata
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: "longhorn"  # Ajusta si necesitas otro StorageClass
        resources:
          requests:
            storage: 10Gi

---
apiVersion: v1
kind: Service
metadata:
  name: elasticsearch
spec:
  selector:
    app: elasticsearch
  ports:
    - name: http
      port: 9200
      targetPort: 9200
    - name: transport
      port: 9300
      targetPort: 9300

