0

messenger:consume在 Kubernetes 上运行任务的最佳方式是什么?

部署?

如果我们使用具有一定数量副本的部署来做到这一点,这可能会起作用,但是如果我们对部署进行滚动更新,然后导致一个 pod 被替换,尽管它现在处理了一个长时间运行的消息呢?

为了防止这种情况,我们可能会设置一个高点terminationGracePeriodSecondslifecycle.preStop结合使用?

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

但是在我的测试过程中,即使lifecycle.preStop任务提前停止,由定义的完整时间terminationGracePeriodSeconds仍然会等到 Pod 终止。

有人有好主意吗?

4

1 回答 1

1

这对我有用:

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

更多在这里

于 2020-03-04T10:01:30.903 回答