1 min read

How to regenerate sitemap every day via livenessProbe in Kubernetes

How to regenerate sitemap every day via livenessProbe in Kubernetes

kubectl apply -f --namespace=app deployment.yaml

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    workload.user.cattle.io/workloadselector: apps.deployment-sitemaps
  name: sitemaps
spec:
  progressDeadlineSeconds: 10
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      workload.user.cattle.io/workloadselector: apps.deployment-sitemaps
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 0%
    type: RollingUpdate
  template:
    metadata:
      labels:
        workload.user.cattle.io/workloadselector: apps.deployment-sitemaps
    spec:
      affinity: {}
      containers:
      - name: nginx
        image: nginx:alpine
        readinessProbe:
          failureThreshold: 3
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: 80
          timeoutSeconds: 1
        volumeMounts:
        - mountPath: /usr/share/nginx/html/sitemap-search/
          name: sitemap-search
      - name: sitemaps-generator
        command:
        - /bin/sh
        - -c
        - node app.js && echo 'FINISH' && tail -f /dev/null
        env:
        - name: API_BASE_URL
          value: http://api/api
        image: sitemap-generator:latest
        imagePullPolicy: Always
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - exit $(test $(date +%H) -eq 3 && echo 1 || echo 0)
          failureThreshold: 1
          initialDelaySeconds: 3600
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        volumeMounts:
        - mountPath: /app/sitemap-search
          name: sitemap-search
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      volumes:
      - emptyDir: {}
        name: sitemap-search

Sitemaps generator will autorestart every day at 3:00AM (-eq 3 in livenessProbe)
The most tricky part is `tail -f /dev/null` which prevents container exit after sitemap generate. You cannot use initContainers because livenessProbe cannot restart inited containers (in 2022)!
Docker image sitemap-generator:latest is my private custom node.js app! You have to code it by yourself