apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
data:
  redis-stack.conf: |
    port 6379
    daemonize no
    loadmodule /opt/redis-stack/lib/rediscompat.so
    loadmodule /opt/redis-stack/lib/redisearch.so
    loadmodule /opt/redis-stack/lib/redistimeseries.so
    loadmodule /opt/redis-stack/lib/rejson.so
    loadmodule /opt/redis-stack/lib/redisbloom.so
    loadmodule /opt/redis-stack/lib/redisgears.so v8-plugin-path /opt/redis-stack/lib/libredisgears_v8_plugin.so
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-service
  labels:
    app: redis-cluster
  namespace: bcx-opm
spec:
  ports:
    - port: 6379
      targetPort: 6379
      name: tcp-redis
    - port: 8001
      targetPort: 8001
      name: http-redis
  clusterIP: None
  #针对无头服务, 上面指定的ports是无效的. 其他pod访问这个service的时候，他们直接通过
  # pod的DNS名称进行通信，而不是通过service的IP地址，这对于StatefulSet的场景非常有用，因为它允许每个Pod有一个稳定的网络标识,
  # 比如:　my-statefulset-service-0.my-statefulset-service.default.svc.cluster.local
  # 无头Service不会提供负载均衡功能，因为他们不会将流量分发到多个PO, 相反，流量将直接路由到DNS名称关联的POD
  selector:
    app: redis-cluster
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-cluster
  namespace: bcx-opm
spec:
  serviceName: "redis-cluster"
  replicas: 3
  selector:
    matchLabels:
      app: redis-cluster
  template:
    metadata:
      labels:
        app: redis-cluster
    spec:
      containers:
        - name: redis
          image: redis/redis-stack:latest
          ports:
            - containerPort: 6379
              name: tcp-redis
            - containerPort: 8001
              name: http-redis
          env:
            - name: REDIS_CLUSTER_ENABLED
              value: "true"
            - name: REDIS_CLUSTER_NODES
              value: "redis-cluster-0.redis-cluster-service.bcx-opm.svc.cluster.local:6379,redis-cluster-1.redis-cluster-service.bcx-opm.svc.cluster.local:6379,redis-cluster-2.redis-cluster-service.bcx-opm.svc.cluster.local:6379"
            - name: REDIS_CLUSTER_REPLICAS
              value: "1"
            - name: REDISEARCH_ARGS
              value: "MAXSEARCHRESULTS 5000000 MAXAGGREGATERESULTS 5000000"
            - name: REDIS_ARGS
              value: "--appendfsync everysec"
          volumeMounts:
            - name: redis-storage
              mountPath: /data
      volumes:
        - name: redis-config
          configMap:
            name: redis-config
  volumeClaimTemplates:
    - metadata:
        name: redis-storage
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 1Gi