Upd #63: Gracefully scaledown Postfix

Make sure the queue is flushed when the Postfix container is
shutdown through Kubernetes lifecycle hooks.
This commit is contained in:
Bojan Čekrlić 2021-06-05 21:07:54 +02:00
parent aed5624d36
commit 956caf3fa4
3 changed files with 22 additions and 2 deletions

View file

@ -56,6 +56,8 @@ spec:
{{- tpl .Values.extraInitContainers . | nindent 6 }} {{- tpl .Values.extraInitContainers . | nindent 6 }}
{{- end }} {{- end }}
# Allow up to 2 minutes for Postfix to flush / empty the queue before shutting down the container
terminationGracePeriodSeconds: 120
containers: containers:
- name: {{ .Chart.Name }} - name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
@ -68,6 +70,17 @@ spec:
readinessProbe: {{- toYaml .Values.readinessProbe | nindent 12 }} readinessProbe: {{- toYaml .Values.readinessProbe | nindent 12 }}
livenessProbe: {{- toYaml .Values.livenessProbe | nindent 12 }} livenessProbe: {{- toYaml .Values.livenessProbe | nindent 12 }}
startupProbe: {{- toYaml .Values.startupProbe | nindent 12 }} startupProbe: {{- toYaml .Values.startupProbe | nindent 12 }}
lifecycle:
# If a container has a preStop hook configured, that runs before the container enters the Terminated state.
preStop:
exec:
command:
- bash
- -c
- touch /tmp/container_is_terminating && while ! [[ "`mailq`" == *empty* ]]; do echo "Flushing queue..." && postfix flush; sleep 1; done
{{- if .Values.lifecycle.postStart }}
postStart: {{- toYaml .Values.lifecycle.postStart | nindent 14 }}
{{- end }}
envFrom: envFrom:
- configMapRef: - configMapRef:
name: {{ $fullName | quote }} name: {{ $fullName | quote }}

View file

@ -131,7 +131,9 @@ readinessProbe:
command: command:
- sh - sh
- -c - -c
- 'printf "EHLO healthcheck\n" | nc 127.0.0.1 587 | grep -qE "^220.*ESMTP Postfix"' - >-
[ ! -f /tmp/container_is_terminating ] &&
printf "EHLO healthcheck\n" | nc 127.0.0.1 587 | grep -qE "^220.*ESMTP Postfix"
livenessProbe: livenessProbe:
initialDelaySeconds: 5 initialDelaySeconds: 5
periodSeconds: 5 periodSeconds: 5
@ -156,3 +158,6 @@ startupProbe:
ps axf | fgrep -v grep | egrep -q '\{supervisord\}|/usr/bin/supervisord' && ps axf | fgrep -v grep | egrep -q '\{supervisord\}|/usr/bin/supervisord' &&
ps axf | fgrep -v grep | fgrep -q "postfix-script" && ps axf | fgrep -v grep | fgrep -q "postfix-script" &&
ps axf | fgrep -v grep | fgrep -q 'opendkim.sh' ps axf | fgrep -v grep | fgrep -q 'opendkim.sh'
lifecycle:
postStart: {}

View file

@ -55,7 +55,9 @@ reown_folders() {
chown root: /var/spool/postfix/pid chown root: /var/spool/postfix/pid
do_postconf -e "manpage_directory=/usr/share/man" do_postconf -e "manpage_directory=/usr/share/man"
postfix -c /etc/postfix/ set-permissions || true
# postfix set-permissions complains if documentation files do not exist
postfix -c /etc/postfix/ set-permissions > /dev/null 2>&1 || true
} }
postfix_upgrade_conf() { postfix_upgrade_conf() {