# Redis StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: ez-api-test-dev
  labels:
    app.kubernetes.io/name: ez-api-test-dev
    app.kubernetes.io/component: redis
spec:
  serviceName: redis
  replicas: 1
  selector:
    matchLabels:
      app: redis
  # 有状态服务使用 OrderedReady 策略
  podManagementPolicy: OrderedReady
  # Redis 更新策略：使用 RollingUpdate，但设置 partition=0 表示全部更新
  # 如果需要金丝雀发布，可以将 partition 设置为大于 0 的值
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      partition: 0
  template:
    metadata:
      labels:
        app: redis
        app.kubernetes.io/name: ez-api-test-dev
        app.kubernetes.io/component: redis
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - name: redis
          image: redis:7-alpine
          command:
            - redis-server
            - --appendonly
            - "yes"
            - --save
            - "60"
            - "1"
          ports:
            - containerPort: 6379
              name: redis
          volumeMounts:
            - name: redis-data
              mountPath: /data
          resources:
            requests:
              memory: "128Mi"
              cpu: "50m"
            limits:
              memory: "512Mi"
              cpu: "250m"
          livenessProbe:
            exec:
              command:
                - redis-cli
                - ping
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 6
          readinessProbe:
            exec:
              command:
                - redis-cli
                - ping
            initialDelaySeconds: 5
            periodSeconds: 5
            timeoutSeconds: 5
            failureThreshold: 3
  volumeClaimTemplates:
    - metadata:
        name: redis-data
      spec:
        accessModes: ["ReadWriteOnce"]
        # 根据集群配置修改 storageClassName
        # storageClassName: "standard"
        resources:
          requests:
            storage: 10Gi
---
# Redis Service (Headless for StatefulSet)
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: ez-api-test-dev
  labels:
    app.kubernetes.io/name: ez-api-test-dev
    app.kubernetes.io/component: redis
spec:
  ports:
    - port: 6379
      targetPort: 6379
      name: redis
  clusterIP: None  # Headless service for StatefulSet
  selector:
    app: redis
