Created
May 19, 2022 13:25
-
-
Save jithin-scaria/191cd170e2bfd00b361111c4c519a651 to your computer and use it in GitHub Desktop.
StatefulSet for Redis High Availability with sentinel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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