在 Kubernetes 上成功部署所有 pods 的应用程序的正确方法是什么?
What is the correct way to identify an application is deployed successfully with all pods up on Kubernetes?
部署 pods 后,如何确定所有 pods 都已启动并且 运行?我列出了几个我认为可能是正确的选项,但想了解识别成功部署的标准方法是什么。
- 通过其接口连接到应用程序并使用它来确定是否所有 pods(集群)都已启动(可能对有状态应用程序有益)。对于无状态应用程序,pod 已启动就足够了。
- 公开一个 Restful API 服务来监控部署并做出相应的响应。
- 使用
Kubectl
连接到pods并获取pods和容器运行的状态。
我认为第 1 种方法是正确的,但想了解社区对此的看法。
你所有的方法听起来都很合理并且可以完成工作,但为什么不使用 Kubernetes 为我们提供的工具呢? ;)
Kubernetes 使用的主要健康检查有两种:
- Liveness probe- 了解容器是否运行正在正常工作(未挂起,未处于死锁状态)
- Readiness probe - 了解容器是否能够接受更多请求
值得注意的还有“Startup probe”,它负责保护启动时间难以估计的慢启动容器。
活跃度:
如前所述,liveness probe的主要目标是确保容器没有死。如果它死了,Kubernetes 会删除 Pod 并启动一个新的。
准备就绪:
就绪探测的主要目标是检查容器是否能够处理额外的流量。在某些情况下,容器可能正在工作但无法接受流量。您定义的就绪探测与活动探测相同,但此探测的目的是检查应用程序是否能够在合理的时间内连续回答多个查询。如果没有,Kubernetes 会停止向 pod 发送流量,直到它通过就绪探测。
实施:
您有几种实现探测的方法:
- 运行 每隔指定时间执行一次命令并检查是否正确执行 - return 代码为 0(在 this example 中,命令
cat /tmp/healthy
是运行每隔几秒就 ning)。
- 每隔指定的时间段向容器发送一个 HTTP GET 请求并检查它是否 return 是一个成功代码(在 this example 中,Kubernetes 正在向端点发送一个 HTTP 请求
/healthz
在容器中定义)。
- 每隔指定的时间尝试在容器中打开 TCP 套接字并确保建立连接(在 this example 中,Kubernetes 正在连接到端口 8080 上的容器)。
对于这两个探测器,您可以定义几个参数:
initialDelaySeconds
: Number of seconds after the container has started before liveness or readiness probes are initiated. Defaults to 0 seconds. Minimum value is 0.
periodSeconds
: How often (in seconds) to perform the probe. Default to 10 seconds. Minimum value is 1.
timeoutSeconds
: Number of seconds after which the probe times out. Defaults to 1 second. Minimum value is 1.
successThreshold
: Minimum consecutive successes for the probe to be considered successful after having failed. Defaults to 1. Must be 1 for liveness and startup Probes. Minimum value is 1.
failureThreshold
: When a probe fails, Kubernetes will try failureThreshold
times before giving up. Giving up in case of liveness probe means restarting the container. In case of readiness probe the Pod will be marked Unready. Defaults to 3. Minimum value is 1.
结合这两项健康检查将确保应用程序已部署并正常工作 - 活动探测用于确保 pod 在其中的容器停止工作时重新启动,准备就绪探测用于确保流量未到达 pod容器未准备好或超载。 The proper functioning of the probes requires an appropriate selection of the implementation method and definition of arguments - most often by trial and error.查看这些文档:
部署 pods 后,如何确定所有 pods 都已启动并且 运行?我列出了几个我认为可能是正确的选项,但想了解识别成功部署的标准方法是什么。
- 通过其接口连接到应用程序并使用它来确定是否所有 pods(集群)都已启动(可能对有状态应用程序有益)。对于无状态应用程序,pod 已启动就足够了。
- 公开一个 Restful API 服务来监控部署并做出相应的响应。
- 使用
Kubectl
连接到pods并获取pods和容器运行的状态。
我认为第 1 种方法是正确的,但想了解社区对此的看法。
你所有的方法听起来都很合理并且可以完成工作,但为什么不使用 Kubernetes 为我们提供的工具呢? ;)
Kubernetes 使用的主要健康检查有两种:
- Liveness probe- 了解容器是否运行正在正常工作(未挂起,未处于死锁状态)
- Readiness probe - 了解容器是否能够接受更多请求
值得注意的还有“Startup probe”,它负责保护启动时间难以估计的慢启动容器。
活跃度:
如前所述,liveness probe的主要目标是确保容器没有死。如果它死了,Kubernetes 会删除 Pod 并启动一个新的。
准备就绪:
就绪探测的主要目标是检查容器是否能够处理额外的流量。在某些情况下,容器可能正在工作但无法接受流量。您定义的就绪探测与活动探测相同,但此探测的目的是检查应用程序是否能够在合理的时间内连续回答多个查询。如果没有,Kubernetes 会停止向 pod 发送流量,直到它通过就绪探测。
实施:
您有几种实现探测的方法:
- 运行 每隔指定时间执行一次命令并检查是否正确执行 - return 代码为 0(在 this example 中,命令
cat /tmp/healthy
是运行每隔几秒就 ning)。 - 每隔指定的时间段向容器发送一个 HTTP GET 请求并检查它是否 return 是一个成功代码(在 this example 中,Kubernetes 正在向端点发送一个 HTTP 请求
/healthz
在容器中定义)。 - 每隔指定的时间尝试在容器中打开 TCP 套接字并确保建立连接(在 this example 中,Kubernetes 正在连接到端口 8080 上的容器)。
对于这两个探测器,您可以定义几个参数:
initialDelaySeconds
: Number of seconds after the container has started before liveness or readiness probes are initiated. Defaults to 0 seconds. Minimum value is 0.periodSeconds
: How often (in seconds) to perform the probe. Default to 10 seconds. Minimum value is 1.timeoutSeconds
: Number of seconds after which the probe times out. Defaults to 1 second. Minimum value is 1.successThreshold
: Minimum consecutive successes for the probe to be considered successful after having failed. Defaults to 1. Must be 1 for liveness and startup Probes. Minimum value is 1.failureThreshold
: When a probe fails, Kubernetes will tryfailureThreshold
times before giving up. Giving up in case of liveness probe means restarting the container. In case of readiness probe the Pod will be marked Unready. Defaults to 3. Minimum value is 1.
结合这两项健康检查将确保应用程序已部署并正常工作 - 活动探测用于确保 pod 在其中的容器停止工作时重新启动,准备就绪探测用于确保流量未到达 pod容器未准备好或超载。 The proper functioning of the probes requires an appropriate selection of the implementation method and definition of arguments - most often by trial and error.查看这些文档: