<p>I have Celery workers running on Kubernetes 1.20 cluster on AWS EKS using AWS Elasticache Redis as the broker. Because of the nature of the project ~80% of the time celery workers are running idle so the logical thing was to have them scale automatically. Scaling based on CPU/memory works ok. At about 4 workers node scaling also needs to kick in and that works ok as well.
An obvious problem is that it takes some time for a new node to start and get fully operational before that node can start taking on new celery worker pods. So some waiting to scale up is expected.</p>
<p>Somewhere in all that waiting a fresh celery worker pod gets started and starts accepting new tasks and executing them, but for some unknown reason the <code>startupProbe</code> does not complete. Because <code>startupProbe</code> was not successful the whole pod is killed potentially in the middle of a running task.</p>
<p><strong>Question</strong>:
Can I prevent celery from taking on tasks before the <code>startupProbe</code> is considered successful?</p>
<p>Celery config</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: celery-worker
  labels:
    app: celery-worker
spec:
  selector:
    matchLabels:
      app: celery-worker
  progressDeadlineSeconds: 900
  template:
    metadata:
      labels:
        app: celery-worker
    spec:
      containers:
      - name: celery-worker
        image: -redacted-
        imagePullPolicy: Always
        command: [&quot;./scripts/celery_worker_entrypoint_infra.sh&quot;]
        env:
          - name: CELERY_BROKER_URL
            valueFrom:
              secretKeyRef:
                name: celery-broker-url-secret
                key: broker-url
        startupProbe:
          exec:
            command: [&quot;/bin/bash&quot;, &quot;-c&quot;, &quot;celery -q -A app inspect -d celery@$HOSTNAME --timeout 10 ping&quot;]
          initialDelaySeconds: 20
          timeoutSeconds: 10
          successThreshold: 1
          failureThreshold: 30
          periodSeconds: 10
        readinessProbe:
          exec:
            command: [&quot;/bin/bash&quot;, &quot;-c&quot;, &quot;celery -q -b $CELERY_BROKER_URL inspect -d celery@$HOSTNAME --timeout 10 ping&quot;]
          periodSeconds: 120
          timeoutSeconds: 10
          successThreshold: 1
          failureThreshold: 3
        livenessProbe:
          exec:
            command: [&quot;/bin/bash&quot;, &quot;-c&quot;, &quot;celery -q -b $CELERY_BROKER_URL inspect -d celery@$HOSTNAME --timeout 10 ping&quot;]
          periodSeconds: 120
          timeoutSeconds: 10
          successThreshold: 1
          failureThreshold: 5
        resources:
          requests:
            memory: &quot;384Mi&quot;
            cpu: &quot;250m&quot;
          limits:
            memory: &quot;1Gi&quot;
            cpu: &quot;500m&quot;
      terminationGracePeriodSeconds: 2400
</code></pre>
<p>Celery HPA config</p>
<pre><code>kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta2
metadata:
  name: celery-worker
spec:
  minReplicas: 2
  maxReplicas: 40
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: celery-worker
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 90
</code></pre>
<p>Celery startup script</p>
<pre><code>python manage.py check
exec celery --quiet -A app worker \
        --loglevel info \
        --concurrency 1 \
        --uid=nobody \
        --gid=nogroup
</code></pre>
<p>I'm sharing complete configs including <code>readinessProbe</code> and <code>livenessProbe</code> whose values are a bit inflated, but are a consequence of various try-and-error scenarios.</p>
<p><strong>Edit:</strong>
This is a catch-22 situation.</p>
<p>I have defined <code>startupProbe</code> to check if celery is running in current host and that will only be true if celery worker is running. And if celery worker is running it will accept tasks. And if it will accept tasks <code>celery inspect</code> command might take too long causing the <code>startupProbe</code> to hang and failing. If <code>startupProbe</code> fails too many times it will kill the pod.</p>
<p>Furthermore, if I call <code>celery inspect</code> without destination (host) defined, <code>startupProbe</code> will fail on initial deployment.</p>
<p>Conclusion: <code>celery inspect</code> is not a good <code>startupProbe</code> candidate.</p>

mislavcimpersak
<blockquote>
<p>Conclusion: <code>celery inspect</code> is not a good <code>startupProbe</code> candidate.</p>
</blockquote>
<p>I agree.</p>
<p>You also mentioned that you have an active <code>healtcheck</code></p>
<blockquote>
<p>I have in another container a web service that uses Django framework and exposes health check at <code>/health</code></p>
</blockquote>
<p>I think it is worth using it to create <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-startup-probes" rel="nofollow noreferrer">startup probe</a>:</p>
<blockquote>
<p>Sometimes, you have to deal with legacy applications that might require an additional startup time on their first initialization. In such cases, it can be tricky to set up liveness probe parameters without compromising the fast response to deadlocks that motivated such a probe. The trick is to set up a startup probe with the same command, HTTP or TCP check, with a <code>failureThreshold * periodSeconds</code> long enough to cover the worse case startup time.</p>
</blockquote>
<p>You also mention an example attack:</p>
<blockquote>
<p>Hmm, I'm not sure that is a smart thing to do. If for instance my web service that serves <code>/health</code> gets DDoS-ed my celery workers would also fail.</p>
</blockquote>
<p>However, this shouldn't be a problem. Startup probe will only run when the container is started. The chance that someone will attack you while the environment is being launched is practically zero. You should use <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes" rel="nofollow noreferrer">readiness or liveness</a> probe to check if your container is alive while the application is running.</p>
<blockquote>
<p>The kubelet uses startup probes to know when a container application has started. If such a probe is configured, it disables liveness and readiness checks until it succeeds, making sure those probes don't interfere with the application startup. This can be used to adopt liveness checks on slow starting containers, avoiding them getting killed by the kubelet before they are up and running.</p>
</blockquote>
<p>Here you can find example yaml</p>
<pre class="lang-yaml prettyprint-override"><code>ports:
- name: liveness-port
  containerPort: 8080
  hostPort: 8080

livenessProbe:
  httpGet:
    path: /healthz
    port: liveness-port
  failureThreshold: 1
  periodSeconds: 10

startupProbe:
  httpGet:
    path: /healthz
    port: liveness-port
  failureThreshold: 30
  periodSeconds: 10
</code></pre>
<p>In this case <code>startupProbe</code> will execute on their first initialization, then will be used <code>livenessProbe</code>.</p>
