Skip to content

Instantly share code, notes, and snippets.

@jithin-scaria
Created May 19, 2022 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jithin-scaria/191cd170e2bfd00b361111c4c519a651 to your computer and use it in GitHub Desktop.
Save jithin-scaria/191cd170e2bfd00b361111c4c519a651 to your computer and use it in GitHub Desktop.
StatefulSet for Redis High Availability with sentinel
# StatefulSet for Redis High Availability with sentinel
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: config
image: redis:7.0.0-alpine
command: [ "sh", "-c" ]
args:
- |
echo "Copying configuration file"
cp /tmp/redis/redis.conf /etc/redis/redis.conf
if [ "$(redis-cli -h sentinel -p 5000 ping)" != "PONG" ]; then
echo "Sentinel not found to get the master info, defaulting to redis-0"
if [ "$(hostname)" == "redis-0" ]; then
echo "This is redis-0, No need to update config."
else
echo "This is not redis-0, Updating redis.conf. finding master FQDN"
MASTER_FDQN=`hostname -f | sed -e 's/redis-[0-9]\./redis-0./'`
echo "REPLICAOF $MASTER_FDQN 6379" >> /etc/redis/redis.conf
fi
else
echo "Sentinel found, finding master"
MASTER="$(redis-cli -h sentinel -p 5000 sentinel get-master-addr-by-name mymaster | grep -E '(^redis-\d{1,})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})')"
echo "Master got: $MASTER, updating this in redis.conf"
echo "REPLICAOF $MASTER 6379" >> /etc/redis/redis.conf
fi
volumeMounts:
- name: redis-config
mountPath: /etc/redis/
- name: config
mountPath: /tmp/redis/
containers:
- name: redis
image: redis:7.0.0-alpine
command: ["redis-server"]
args: ["/etc/redis/redis.conf"]
ports:
- containerPort: 6379
name: redis
volumeMounts:
- name: data
mountPath: /data
- name: redis-config
mountPath: /etc/redis/
- name: config-acl
mountPath: /conf/acl/
volumes:
- name: redis-config
emptyDir: {}
- name: config
configMap:
name: redis-config
- name: config-acl
configMap:
name: redis-acl
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 50Mi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment