使用 python manage.py migrate --check in kubernetes readinessProbe 永远不会成功

Using python manage.py migrate --check in kubernetes readinessProbe never succeeds

我在 kubernetes 集群和 readinessProbe 中部署了 django,我是 运行 pythonmanage.pymigrate--check.我可以看到此命令的 return 值为 0,但 pod 从未就绪。

我的部署片段:

      containers:                                                                             
        - name: myapp                                                                      
          ...
          imagePullPolicy: Always                                                             
          readinessProbe:                                                                     
            exec:                                                                             
              command: ["python", "manage.py", "migrate", "--check"]                          
            initialDelaySeconds: 15                                                           
            periodSeconds: 5  

当我描述还没有准备好的pod时:

Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  66s                default-scheduler  Successfully assigned ...  Normal   Pulled     66s                kubelet            Successfully pulled image ...
  Normal   Created    66s                kubelet            Created container ...
  Normal   Started    66s                kubelet            Started container ...
  Warning  Unhealthy  5s (x10 over 50s)  kubelet            Readiness probe failed:

我可以看到 migrate --check returns 0 通过进入仍处于未就绪状态的容器和 运行

python manage.py migrate
echo $?
0

我作为 readinessProbe 传递的 exec 命令是否有问题?

我使用的kubernetes服务器版本是1.21.7。 我部署的基础镜像是 python:3.7-slim.

问题的解决方法是增加timeoutSeconds parameter, which is by default set to 1 second:

  • timeoutSeconds: Number of seconds after which the probe times out. Defaults to 1 second. Minimum value is 1.

增加timeoutSeconds参数后,应用程序能够通过就绪探测。

timeoutSeconds 参数设置为 5 的部署示例片段:

      containers:                                                                             
        - name: myapp                                                                      
          ...
          imagePullPolicy: Always                                                             
          readinessProbe:                                                                     
            exec:                                                                             
              command: ["python", "manage.py", "migrate", "--check"]                          
            initialDelaySeconds: 15                                                           
            periodSeconds: 5
            timeoutSeconds: 5