Redis & Sentinel init containers Statefulset examples - Part 3

Following are examples of full StatefulSet YAMLs for Redis and Sentinel to show you how our previously discussed init container sections appear:

Redis StatefulSet

apiVersion: apps/v1 kind: StatefulSet metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | name: redis namespace: redis spec: persistentVolumeClaimRetentionPolicy: whenDeleted: Retain whenScaled: Retain podManagementPolicy: OrderedReady replicas: 3 revisionHistoryLimit: 10 selector: matchLabels: app: redis serviceName: redis template: metadata: labels: app: redis spec: containers: - args: - /etc/redis/redis.conf command: - redis-server image: redis:7.0.10-alpine imagePullPolicy: IfNotPresent name: redis ports: - containerPort: 6379 name: redis protocol: TCP resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /data name: data - mountPath: /etc/redis/ name: redis-config dnsPolicy: ClusterFirst initContainers: - args: - | cp /tmp/redis/redis.conf /etc/redis/redis.conf MY_FQDN="$(hostname -f)" SEED_MASTER="$(hostname -f | sed -e 's/redis-[0-9]\./redis-0./')" # announce by stable FQDN so Sentinel never tracks an ephemeral pod IP echo "replica-announce-ip $MY_FQDN" >> /etc/redis/redis.conf echo "finding master..." if [ "$(redis-cli -h sentinel -p 5000 ping 2>/dev/null)" != "PONG" ]; then echo "sentinel unreachable -> cold-start default" if [ "$(hostname)" = "redis-0" ]; then echo "this is redis-0: starting as master" else echo "replicaof $SEED_MASTER 6379" >> /etc/redis/redis.conf fi else MASTER="$(redis-cli -h sentinel -p 5000 sentinel get-master-addr-by-name mymaster | head -n1)" echo "sentinel reports master: '$MASTER'" if [ -z "$MASTER" ]; then [ "$(hostname)" = "redis-0" ] || echo "replicaof $SEED_MASTER 6379" >> /etc/redis/redis.conf elif [ "$MASTER" = "$MY_FQDN" ]; then echo "this is the current master: starting clean (no replicaof)" else echo "replicaof $MASTER 6379" >> /etc/redis/redis.conf fi fi command: - sh - -c image: redis:7.0.10-alpine imagePullPolicy: IfNotPresent name: config resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /etc/redis/ name: redis-config - mountPath: /tmp/redis/ name: config restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30 volumes: - emptyDir: {} name: redis-config - configMap: defaultMode: 420 name: redis-config name: config updateStrategy: rollingUpdate: partition: 0 type: RollingUpdate volumeClaimTemplates: - apiVersion: v1 kind: PersistentVolumeClaim metadata: name: data spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi storageClassName: standard volumeMode: Filesystem

Sentinel StatefulSet

apiVersion: apps/v1 kind: StatefulSet metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | name: sentinel namespace: redis spec: persistentVolumeClaimRetentionPolicy: whenDeleted: Retain whenScaled: Retain podManagementPolicy: OrderedReady replicas: 3 revisionHistoryLimit: 10 selector: matchLabels: app: sentinel serviceName: sentinel template: metadata: labels: app: sentinel spec: containers: - args: - /etc/redis/sentinel.conf command: - redis-sentinel image: redis:7.0.10-alpine imagePullPolicy: IfNotPresent name: sentinel ports: - containerPort: 5000 name: sentinel protocol: TCP resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /etc/redis/ name: redis-config - mountPath: /data name: data dnsPolicy: ClusterFirst initContainers: - args: - | cat > /etc/redis/sentinel.conf <<EOF port 5000 sentinel resolve-hostnames yes sentinel announce-hostnames yes sentinel monitor mymaster redis-0.redis.redis.svc.cluster.local 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 60000 sentinel parallel-syncs mymaster 1 sentinel auth-pass mymaster "$REDIS_PASSWORD" EOF command: - sh - -c env: - name: REDIS_PASSWORD valueFrom: secretKeyRef: key: auth name: redis-secret image: redis:7.0.10-alpine imagePullPolicy: IfNotPresent name: config resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /etc/redis/ name: redis-config restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30 volumes: - emptyDir: {} name: redis-config updateStrategy: rollingUpdate: partition: 0 type: RollingUpdate volumeClaimTemplates: - apiVersion: v1 kind: PersistentVolumeClaim metadata: name: data spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi storageClassName: standard volumeMode: Filesystem

#k8s #redis #sentinel

0 Comments