t3n/flow-healthstatus

用于检查应用程序健康状态的 Flow 框架包

安装数量: 25,996

依赖项: 0

建议者: 0

安全: 0

星标: 6

关注者: 10

分支: 5

开放问题: 4

类型:neos-package

2.5.0 2023-08-25 07:26 UTC

README

CircleCI Latest Stable Version Total Downloads License

t3n.Flow.HealthStatus

用于检查流应用程序健康状态的包。

在 Kubernetes 环境中,与存活性和就绪性探测一起使用,非常有用,以确定 Pod 是否可以处理流量以及它是否仍然存活。

用法

要确定应用程序当前的运行状态,您可以检查应用程序是否已就绪或仍然存活。

就绪性

只需执行以下 flow 命令:./flow app:isready

这将执行 Settings.yaml 中定义的所有测试 t3n.Flow.HealthStatus.testChain。如果所有测试都通过,则将执行 readyChain 任务。

在成功运行 readyChain 之后,将设置一个内部锁以防止重复执行。每次运行都会执行 testChain。因此,readyChain 应该将您的应用程序置于“就绪状态”。请确保初始化所需的一切。testChain 应该ping您应用程序依赖的所有服务。

存活性

执行 ./flow app:isalive 以检查您的 Pod 是否仍然存活。

这将执行 livenessChain

目前,默认情况下,liveness chain 为空,有一个可能的测试:statusCode

配置

请按照以下格式将所有测试添加到您的应用程序 Settings.yaml 中

t3n:
  Flow:
    HealthStatus:
      testChain:
        yourUniqueTestKey:
          name: 'Optional name'
          test: 'database' // shorthand for a predefined task in t3n\Flow\HealthStatus\Test\*.Test or a full qualified class name
          options:
            key: 'value' // optional options for your test
          position: 'after otherTestKey' // optional position

之后,检查将执行就绪链

t3n:
  Flow:
    HealthStatus:
      readyChain:
        yourUniqueTaskKey:
          name: 'Optional name'
          task: 'command' // shorthand for a predefined task in t3n\Flow\HealthStatus\Task\*.Task or a full qualified class name
          options:
            key: 'value' // optional options for your task
          position: 'after otherTaskKey' / optional position
          lockName: 'mylock' // optional lock override. This will create a lock for this task only and ignore the global lock

在成功调用就绪链之后,您可以通过调用 ./flow app:isalive 来执行您的存活性链

t3n:
  Flow:
    HealthStatus:
      livenessChain:
        yourUniqueTestKey:
          name: 'Optional name'
          task: 'statusCode' // shorthand for a predefined task in t3n\Flow\HealthStatus\LivenessTest\*.Test or a full qualified class name
          options:
            key: 'value' // optional options for your task
          position: 'after otherTaskKey' / optional position

高级配置

在尝试执行就绪任务之前,检查将测试 t3n.Flow.HealthStatus.defaultReadyTaskCondition 以查看任务是否应该执行。在默认配置中,这只是一个检查以查看就绪锁是否尚未设置。

您可以根据任务逐个覆盖此行为

t3n:
  Flow:
    HealthStatus:
      readyChain:
        yourUniqueTaskKey:
          condition: '${Lock.isSet("mylock")}' // this can be any eel expression
          afterInvocation: '${Lock.set("mylock")}' // this will be executed after a successfull invocation

(lockName 设置只是这个示例的简称)

要扩展 eel 上下文,您可以在 t3n.Flow.HealthStatus.defaultContext 中提供额外的辅助工具。

通过 HTTP 请求检查健康状态

如果您想通过 HTTP 而不是 CLI 命令来检查应用程序,您可以在 Routes.yaml 中包含路由来实现。

-
  name: 'Health-Routes'
  uriPattern: '<HealthStatusSubroutes>'
  subRoutes:
    'HealthStatusSubroutes':
      package: 't3n.Flow.HealthStatus'
      variables:
        'healthStatusEndpoint': 'your-endpoint-name'

之后将出现两个新路由:`//ready` 和 `/<your-endpoint-name>/live`。根据您的需要进行调整。这两个端点都将返回 JSON 格式的输出。在成功运行时,响应状态码为 200;如果存在任何错误,状态码将为 500。

示例配置

此示例可用于您的 Flow 包,以确保应用程序 Pod 处于就绪状态以处理流量。因此,它将始终检查 doctrine、redis 和 beanstalk 的 ping 状态。在第一次运行时,将执行所有缺失的数据库迁移、刷新 redis 缓存和发布静态资源。成功运行之后,将再次执行测试链。

t3n:
  Flow:
    HealthStatus:
      testChain:
        database:
          test: doctrine
          position: start
        redis:
          test: redis
          options:
            hostname: your-redis-host
        beanstalk:
          test: beanstalk
          options:
            hostname: your-beanstalk-host
      readyChain:
        migrations:
          task: command
          options:
            command: 'neos.flow:doctrine:migrate'
        flushRedis:
          name: 'Flush redis'
          position: 'start 100'
          task: redis
          options:
            hostname: your-redis-host
            command: FLUSHDB
            database: 0
        staticResources:
          name: 'Publish static resources'
          task: command
          position: 'end 20'
          lockname: staticresources
          cacheName: t3n_FlowHealthStatus_LocalLock
          options:
            command: 'neos.flow:resource:publish'
            arguments:
              collection: static
      livenessChain:
        home:
          test: statusCode
          name: 'Homepage responds'
          options:
            url: '/'
            method: 'GET'
            statusCode: 200

请注意lockname配置。此配置允许您在每个部署中只运行一次任务或始终运行。默认情况下,使用t3n_FlowHealthStatus_Lock缓存来读写锁。将其添加到您的Caches.yaml文件中,所有应用程序Pod都将依赖于相同的锁文件,因为它们不使用本地文件存储而是使用redis。这将导致每个部署只执行一次。

t3n_FlowHealthStatus_Lock:
  backend: Neos\Cache\Backend\RedisBackend
  backendOptions:
    hostname: 'your-redis-server'
    database: 2

staticResources任务配置了自定义的cacheName。为确保此任务在每个应用程序Pod中执行,请将其设置为本地文件存储。

t3n_FlowHealthStatus_LocalLock:
  frontend: Neos\Cache\Frontend\StringFrontend
  backend: Neos\Cache\Backend\FileBackend