Skip to content

Commit

Permalink
Redis Sentinel - handle SIGTERM in kubectl-shared container
Browse files Browse the repository at this point in the history
When Redis Sentinel has the master service option enabled, the `kubectl-shared`
container runs a script with an infinite `while+sleep` loop.

This script does not handle SIGTERM and causes the pod to hang upon termination
until the termination grace period elapses.

This change introduces a `preStop` hack to create a `terminate` file, which is used
as a singal to the script that it should exit the infinite loop

Signed-off-by: Kevin Pullin <[email protected]>
  • Loading branch information
kppullin committed Feb 20, 2025
1 parent 9d61546 commit a327f3c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bitnami/redis/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ maintainers:
name: redis
sources:
- https://github.com/bitnami/charts/tree/main/bitnami/redis
version: 20.8.0
version: 20.8.1
22 changes: 15 additions & 7 deletions bitnami/redis/templates/scripts-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -807,15 +807,23 @@ data:
update-master-label.sh: |
#!/bin/bash
while true; do
while [ ! -f "/etc/shared/current" ]; do
while [ ! -f "/etc/shared/current" ] && [ ! -f "/etc/shared/terminate" ]; do
sleep 1
done
echo "new master elected, updating label(s)..."
kubectl label pod --field-selector metadata.name="$(< "/etc/shared/current")" isMaster="true" --overwrite
kubectl label pod --field-selector metadata.name="$(< "/etc/shared/current")" app.kubernetes.io/role-
if [ -f /etc/shared/previous ]; then
kubectl label pod --field-selector metadata.name="$(< "/etc/shared/previous")" isMaster="false" --overwrite
if [ -f "/etc/shared/current" ]; then
echo "new master elected, updating label(s)..."
kubectl label pod --field-selector metadata.name="$(< "/etc/shared/current")" isMaster="true" --overwrite
kubectl label pod --field-selector metadata.name="$(< "/etc/shared/current")" app.kubernetes.io/role-
if [ -f /etc/shared/previous ]; then
kubectl label pod --field-selector metadata.name="$(< "/etc/shared/previous")" isMaster="false" --overwrite
fi
rm "/etc/shared/current" "/etc/shared/previous"
fi
if [ -f "/etc/shared/terminate" ]; then
echo "received signal to terminate"
exit
fi
rm "/etc/shared/current" "/etc/shared/previous"
done
{{- end }}
6 changes: 6 additions & 0 deletions bitnami/redis/templates/sentinel/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,12 @@ spec:
image: {{ template "redis.kubectl.image" . }}
imagePullPolicy: {{ .Values.kubectl.image.pullPolicy | quote }}
command: {{- toYaml .Values.kubectl.command | nindent 12 }}
lifecycle:
preStop:
exec:
command:
- touch
- /etc/shared/terminate
{{- if .Values.kubectl.containerSecurityContext.enabled }}
securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.kubectl.containerSecurityContext "context" $) | nindent 12 }}
{{- end }}
Expand Down

0 comments on commit a327f3c

Please sign in to comment.