apiVersion: v1
kind: Service
metadata:
  name: redis-svc
spec:
  clusterIP: None
  selector:
    app: commerce-redis
  ports:
    - name: redis
      port: 6379
      targetPort: 6379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: commerce-redis
spec:
  serviceName: redis-svc
  replicas: 1
  selector:
    matchLabels:
      app: commerce-redis
  template:
    metadata:
      labels:
        app: commerce-redis
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - name: redis
          image: redis:7.4-alpine
          imagePullPolicy: IfNotPresent
          command:
            - sh
            - -c
          args:
            - >
              exec redis-server
              --appendonly yes
              --appendfsync everysec
              --requirepass "$REDIS_PASSWORD"
          env:
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: commerce-secrets
                  key: REDIS_PASSWORD
          ports:
            - name: redis
              containerPort: 6379
          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - redis-cli -a "$REDIS_PASSWORD" ping | grep PONG
            initialDelaySeconds: 5
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 6
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - redis-cli -a "$REDIS_PASSWORD" ping | grep PONG
            initialDelaySeconds: 20
            periodSeconds: 20
            timeoutSeconds: 5
            failureThreshold: 6
          resources:
            requests:
              cpu: 50m
              memory: 64Mi
            limits:
              cpu: 250m
              memory: 256Mi
          volumeMounts:
            - name: redis-data
              mountPath: /data
  volumeClaimTemplates:
    - metadata:
        name: redis-data
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: local-path
        resources:
          requests:
            storage: 1Gi
