Kubernetes: redis-master on GKE (Google Kubernetes Engine)

This will create a stateful redis-master with a persistent drive on GKE (no slaves or sentinels) with a headless service.

Create redis-master.yaml

vi redis-master.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-primary
  labels:
    app: redis-primary
spec:
  ports:
  - port: 6379
    name: redis-primary
  selector:
    app: redis-primary
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: redis-primary
spec:
  serviceName: redis-primary
  replicas: 1
  template:
    metadata:
      labels:
        app: redis-primary
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: redis-primary
        image: redis:4.0.2-alpine
        imagePullPolicy: Always
        ports:
        - containerPort: 6379
          name: redis-primary
        volumeMounts:
        - name: redis-primary
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: redis-primary
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 5Gi

Create

kubectl create -f redis-master.yaml

Example

kubectl create -f redis-master.yaml
service "redis-primary" created
statefulset.apps "redis-primary" created

Connect

kubectl exec redis-primary-0 -it redis-cli

Check version

kubectl exec redis-primary-0 -it redis-cli info server

Clean up

kubectl delete -f redis-master.yaml

Example

kubectl delete -f redis-master.yaml
service "redis-primary" deleted
statefulset.apps "redis-primary" deleted

Delete Volume

kubectl delete pvc -l app=redis-primary

Example

kubectl delete persistentVolumeClaim -l app=redis-primary
persistentvolumeclaim "redis-primary-volume-redis-primary-0" deleted