title: How to best run Symfony Messenger Worker on Kubernetes
body: <p>What's the best way to run the <code>messenger:consume</code> task on Kubernetes?</p>
  
  <p><strong>Deployment?</strong></p>
  
  <p>If we do it with a deployment with a certain number of replicas this could work, but what if we do a rolling update of the deployment which then results in a pod to be replaced although a long running message is handled by it right now?</p>
  
  <p>To prevent this we could probably set a high <code>terminationGracePeriodSeconds</code> and use <code>lifecycle.preStop</code> in combination?</p>
  
  <pre><code>apiVersion: apps/v1
  kind: Deployment
  metadata:
    creationTimestamp: null
    labels:
      run: deploy
    name: deploy
  spec:
    replicas: 1
    selector:
      matchLabels:
        run: deploy
    strategy: {}
    template:
      metadata:
        creationTimestamp: null
        labels:
          run: deploy
      spec:
        terminationGracePeriodSeconds: 6000
        containers:
        - command:
          - sh
          - -c
          - sleep 1d # bin/console messenger:consume
          image: bash
          name: deploy
          lifecycle:
            preStop:
              exec:
                command:
                - sh
                - -c
                - echo "Test if a message is consumed at the moment and prevent POD shutdown till then?"                    
  </code></pre>
  
  <p>But during my tests, even if the <code>lifecycle.preStop</code> tasks stops early, the full time defined by <code>terminationGracePeriodSeconds</code> is still waited till the pod is terminated.</p>
  
  <p>Anyone got a good idea out there?</p>