如何正确对DB2容器进行健康检查,以确保我的应用容器可以安全启动?

huangapple go评论68阅读模式
英文:

How to proper healthcheck a db2 container so my app container can go up safely?

问题

目前,我只等待一个相对安全的时间窗口,然后启动这个 Docker Compose 文件中的应用容器:

# 这个 Compose 文件用于启动用于开发目的的 db2 数据库
version: "3"
services:
  db2: # https://hub.docker.com/r/ibmcom/db2
    #    image: ibmcom/db2:11.5.8.0
    image: icr.io/db2_community/db2:11.5.8.0
    privileged: true
    expose:
      - 50000
    ports:
      - "50000:50000"
    environment:
      LICENSE: accept
      DBNAME: sample
      DB2INSTANCE: db2inst1
      DB2INST1_PASSWORD: change-me-please
    healthcheck:
      # TODO 寻找一个适用于 db2 的真正健康检查
      test: ["CMD", "sleep", "300"]
      interval: 30s
      timeout: 301s
      retries: 3
  app:
    build:
      context: ../../
      dockerfile: ./src/infrastructure/Dockerfile
    expose:
      - 7070
    ports:
      - "7071:7070"
    environment:
      APP_PROFILE: compose
    depends_on:
      db2:
        condition: service_healthy
    restart: on-failure

是否有任何专门针对 db2 的工具可以提供更好的健康检查?
欢迎任何指导。
为了上下文,整个项目在这里

英文:

Currently i am just waiting a somewhat safe time frame before up the app container inside this docker-compose file:

# this compose spins up a db2 database for development purposes
version: "3"
services:
  db2: # https://hub.docker.com/r/ibmcom/db2
    #    image: ibmcom/db2:11.5.8.0
    image: icr.io/db2_community/db2:11.5.8.0
    privileged: true
    expose:
      - 50000
    ports:
      - "50000:50000"
    environment:
      LICENSE: accept
      DBNAME: sample
      DB2INSTANCE: db2inst1
      DB2INST1_PASSWORD: change-me-please
    healthcheck:
      # TODO find a real health check for db2
      test: [ "CMD", "sleep", "300"]
      interval: 30s
      timeout: 301s
      retries: 3
  app:
    build:
      context: ../../
      dockerfile: ./src/infrastructure/Dockerfile
    expose:
      - 7070
    ports:
      - "7071:7070"
    environment:
      APP_PROFILE: compose
    depends_on:
      db2:
        condition: service_healthy
    restart: on-failure

Is there any db2-specific tool able to give me better healthchecks?
Any guidance is welcome.
For context, the entire project is here.

答案1

得分: 1

有一个 db2gcf 实用程序,必须在服务器上本地运行,要么以 root 用户身份,要么以 db2 实例所有者(在您的情况下是 db2inst1)身份。即:
db2gcf -s
它会返回相应的返回码以供检查需要。

英文:

There is a db2gcf utility which must be run on the server locally either as root or db2 instance owner (db2inst1 in your case). That is:
db2gcf -s
It returns the corresponding return code to check if needed.

答案2

得分: 1

另一种进行健康检查的方法是使用以下测试连接到数据库(将<>之间的属性替换为值):

healthcheck:
test: ["CMD-SHELL", "su - db2inst1 -c "db2 connect to <DB_NAME> user <USER_NAME> using ""]
interval: 30s
timeout: 10s
retries: 3
Docker将定期尝试连接到数据库以确保其可用。如果命令失败,Docker将认为容器处于“不健康”状态。

英文:

Another way of doing this healthcheck is to connect to the database using this test (replace the property between <> by the value) :

healthcheck:
  test: [&quot;CMD-SHELL&quot;, &quot;su - db2inst1 -c \&quot;db2 connect to &lt;DB_NAME&gt; user &lt;USER_NAME&gt; using &lt;PASSWORD&gt;\&quot;&quot;]
  interval: 30s
  timeout: 10s
  retries: 3

Docker will attempt to connect to the database at regular intervals to ensure that it is available. If the command fails, Docker will consider the container to be "unhealthy".

huangapple
  • 本文由 发表于 2023年5月18日 11:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定