allow a dedicated deployment for cron

This commit is contained in:
Jonas Leder 2026-08-08 21:19:51 +02:00
commit ce45c03e44
6 changed files with 124 additions and 2 deletions

View file

@ -42,7 +42,8 @@ ENV PHP_MEMORY_LIMIT=2048M \
NEXTCLOUD_OVERWRITE_PROTOCOL=https \
NEXTCLOUD_DBPERSISTENCE=true \
NOTIFY_PUSH_BINPATH=/data/apps/notify_push/bin/x86_64/notify_push \
EXIT_AFTER_INSTALL=0
EXIT_AFTER_INSTALL=0 \
RUN_CRON=1
RUN phpaddmod ftp imap gmp imagick sysvsem ldap && \
apk add --no-cache ffmpeg imagemagick-svg supercronic netcat-openbsd && \
@ -69,6 +70,7 @@ COPY --chown=root:$SYSTEM_USERNAME --chmod=0640 common.sh /usr/local/bin/common.
COPY --chown=root:$SYSTEM_USERNAME --chmod=0750 start.sh /usr/local/bin/start.sh
COPY --chown=root:$SYSTEM_USERNAME --chmod=0750 setup.sh /usr/local/bin/setup.sh
COPY --chown=root:$SYSTEM_USERNAME --chmod=0750 serve.sh /usr/local/bin/serve.sh
COPY --chown=root:$SYSTEM_USERNAME --chmod=0750 cron.sh /usr/local/bin/cron.sh
RUN rm -rf /var/www/html/config && \
ln -s /var/www/html/occ /usr/local/bin/occ && \

View file

@ -105,7 +105,9 @@ protect_secrets() {
run_server() {
echo "Init done, starting FrankenPHP"
supercronic -prometheus-listen-address 0.0.0.0:8081 /etc/crontabs/nextcloud &
if [ "${RUN_CRON:-1}" -eq 1 ]; then
supercronic -prometheus-listen-address 0.0.0.0:8081 /etc/crontabs/nextcloud &
fi
if [ "${NOTIFY_PUSH_DAEMON_ENABLED:-0}" -eq 1 ]; then
"$NOTIFY_PUSH_BINPATH" --port 7867 /data/config/config.php &
setup_notify_push &
@ -113,6 +115,11 @@ run_server() {
exec /usr/local/bin/frankenphp run --config /etc/caddy/Caddyfile
}
run_cron() {
echo "Init done, starting cron"
exec supercronic -prometheus-listen-address 0.0.0.0:8081 /etc/crontabs/nextcloud
}
run_sidecar_if_requested() {
if [ "${SIDECAR_NOTIFY_PUSH:-0}" -eq 1 ]; then
"$NOTIFY_PUSH_BINPATH" --port 7867 /data/config/config.php

18
cron.sh Normal file
View file

@ -0,0 +1,18 @@
#!/bin/ash
# Runs nextcloud cron jobs only. Used as a dedicated k8s cron pod so cron
# runs exactly once regardless of how many web replicas are running.
set -eu
. /usr/local/bin/common.sh
write_php_ini
ensure_data_dirs
while [ ! -f "/data/config/config.php" ]; do
echo "Waiting for nextcloud setup job to finish installing..."
sleep 2
done
protect_secrets
run_cron

View file

@ -0,0 +1,78 @@
{{- if .Values.cron.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "nextcloud.fullname" . }}-cron
namespace: {{ .Release.Namespace }}
labels:
{{- include "nextcloud.labels" . | nindent 4 }}
app.kubernetes.io/component: cron
spec:
# Cron must run on exactly one pod at a time; Recreate avoids two cron
# pods briefly overlapping during a rollout the way RollingUpdate would.
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
{{- include "nextcloud.selectorLabels" . | nindent 6 }}
app.kubernetes.io/component: cron
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "nextcloud.labels" . | nindent 8 }}
app.kubernetes.io/component: cron
{{- with .Values.podLabels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "nextcloud.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}-cron
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ include "nextcloud.image" . }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["ash", "/usr/local/bin/cron.sh"]
env:
{{- include "nextcloud.env" . | nindent 12 }}
ports:
- name: cron-metrics
containerPort: 8081
protocol: TCP
resources:
{{- toYaml .Values.cron.resources | nindent 12 }}
volumeMounts:
- name: nextcloud-data
mountPath: /data
volumes:
- name: nextcloud-data
persistentVolumeClaim:
{{ if .Values.storage.useExistingPvc }}
claimName: {{ .Values.storage.existingPvc }}
{{ else }}
claimName: {{ include "nextcloud.fullname" . }}
{{ end }}
{{- with .Values.cron.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.cron.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.cron.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- end }}

View file

@ -45,6 +45,8 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: RUN_CRON
value: {{ if .Values.cron.enabled }}"0"{{ else }}"1"{{ end }}
ports:
- name: http
containerPort: 8080

View file

@ -177,3 +177,18 @@ nextcloud:
user: "admin"
password: ""
existingSecret: "" # key has to be "password"
cron:
# Runs nextcloud's cron on a single dedicated pod instead of on every
# web replica, so scheduled jobs don't run multiple times in parallel.
enabled: false
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 10m
memory: 128Mi
nodeSelector: {}
tolerations: []
affinity: {}