apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-configuration
  namespace: redis
  labels:
    app: redis
data:
  master.conf: |
    maxmemory 400mb
    maxmemory-policy allkeys-lru
    maxclients 20000
    timeout 300
    appendonly no
    dbfilename dump.rdb
    dir /data
  slave.conf: |
    replicaof redis-0.redis.default.svc.cluster.local 6379
    maxmemory 400mb
    maxmemory-policy allkeys-lru
    maxclients 20000
    timeout 300
    dir /data
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: redis
spec:
  serviceName: "redis"
  replicas: 3
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      initContainers:
      - name: init-redis
        image: redis:latest
        command:
        - bash
        - "-c"
        - |
          set -ex
          # Generate redis server-id from pod ordinal index.
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          # Copy appropriate redis config files from config-map to respective directories.
          if [[ $ordinal -eq 0 ]]; then
            cp /mnt/master.conf /etc/redis-config.conf
          else
            cp /mnt/slave.conf /etc/redis-config.conf
          fi
        volumeMounts:
        - name: redis-pvc
          mountPath: /etc
          subPath: redis-claim
        - name: config-map
          mountPath: /mnt/
      containers:
      - name: redis
        image: redis:latest
        ports:
        - containerPort: 6379
          name: redis
        command:
          - redis-server
          - "/etc/redis-config.conf"
        volumeMounts:
        - name: redis-pvc
          mountPath: /data
          subPath: redis-data
        - name: redis-pvc
          mountPath: /etc
          subPath: redis-claim
      volumes:
      - name: config-map
        configMap:
          name: redis-configuration
  volumeClaimTemplates:
  - metadata:
      name: redis-pvc
    spec:
      storageClassName: ""
      accessModes: [ "ReadWriteMany" ]
      resources:
        requests:
          storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: redis
  labels:
    app: redis
spec:
  ports:
    - port: 6379
      targetPort: redis
  clusterIP: None
  selector:
    app: redis
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-pv-0
spec:
  storageClassName: ""
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    server: 192.168.2.110
    path: /mnt/nfs_share/redis-0
    readOnly: false
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-pv-1
spec:
  storageClassName: ""
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    server: 192.168.2.110
    path: /mnt/nfs_share/redis-1
    readOnly: false
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-pv-2
spec:
  storageClassName: ""
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    server: 192.168.2.110
    path: /mnt/nfs_share/redis-2
    readOnly: false
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-pvc-redis-0
  namespace: redis
spec:
  storageClassName: ""
  volumeName: redis-pv-0
  accessModes:
    - ReadWriteMany
  volumeMode: Filesystem
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-pvc-redis-1
  namespace: redis
spec:
  storageClassName: ""
  volumeName: redis-pv-1
  accessModes:
    - ReadWriteMany
  volumeMode: Filesystem
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-pvc-redis-2
  namespace: redis
spec:
  storageClassName: ""
  volumeName: redis-pv-2
  accessModes:
    - ReadWriteMany
  volumeMode: Filesystem
  resources:
    requests:
      storage: 1Gi

