How to best run Symfony Messenger Worker on Kubernetes

7/3/2019

What's the best way to run the messenger:consume task on Kubernetes?

Deployment?

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?

To prevent this we could probably set a high terminationGracePeriodSeconds and use lifecycle.preStop in combination?

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?"                    

But during my tests, even if the lifecycle.preStop tasks stops early, the full time defined by terminationGracePeriodSeconds is still waited till the pod is terminated.

Anyone got a good idea out there?

-- Kim
deployment
kubernetes
symfony
symfony-messenger
symfony4

1 Answer

3/4/2020

This works for me:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: agent
spec:
  replicas: 8
  selector:
    matchLabels:
      id: agent-pod
  template:
    metadata:
      labels:
        id: agent-pod
    spec:
      terminationGracePeriodSeconds: 3600
      containers:
      - name: agent
        volumeMounts:
          - mountPath: /share
            name: share
        lifecycle:
          preStop:
            exec:
              command: ["sh", "-c", "touch /tmp/kill_me"]
        command:
        - /bin/sh
        - -c
        - >
          sleep 3 && rm -rf var/cache/* && bin/console cache:clear &&
          while ! [ -f /tmp/kill_me ];
            do
            bin/console messenger:consume queue --limit=1 --time-limit=60 -vv
          done;
        image: my-symfony-agent-image

More here.

-- Kim
Source: StackOverflow