如何在 kubernetes 中配置 pod 的就绪性和活性探测,其中一个独立的 go 服务是 运行?

How to configure readiness and liveness probe of pod in kubernetes, where a standalone go service is running?

我用go写了一个job scheduler service,它会定期上传文件。该服务中没有 HTTP 服务 运行。

正常情况下,对于HTTP服务,下面是liveness和readiness probe配置,需要在部署文件中添加。

readinessProbe:
  httpGet:
    path: <http_get_path>
    port: 3401
  initialDelaySeconds: 5
  timeoutSeconds: 1
  periodSeconds: 15

livenessProbe:
  httpGet:
    path: <http_get_path>
    port: 3401
  initialDelaySeconds: 15
  timeoutSeconds: 1
  periodSeconds: 15

对于如下的独立服务:

//main : main function to trigger the services
func main() {
    scheduler.RunScheduler()
}

//RunScheduler : Run the scheduler
func RunScheduler() {
    done := make(chan bool)
    quit := make(chan bool)

    go startJob1()

    go startJob2()
    
    sigs := make(chan os.Signal, 1)
    //pass done "True", only if the application is stopped
    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
    <-sigs
    .....
}

它只是定期运行,不监听任何端口,我们可以配置任何类型的就绪探测器和活性探测器吗?

由于您的容器不处理您不需要的任何 http 请求readinessProbe

readinessProbe: Indicates whether the container is ready to respond to requests. If the readiness probe fails, the endpoints controller removes the Pod's IP address from the endpoints of all Services that match the Pod.

When should you use a liveness probe?

您需要 livenessProbe.

livenessProbe: Indicates whether the container is running. If the liveness probe fails, the kubelet kills the container, and the container is subjected to its restart policy.

When should you use a readiness probe?

这是一个 ps 进程的示例:

livenessProbe:
    exec:
     command:
     - ps -ef | grep my_process_name
    initialDelaySeconds: 120
    periodSeconds: 30

确保 $PATH 中有 ps 命令。

另一种方法是在 pod 启动时在 tmp 目录中创建一个文件,并检查 exec 命令中是否存在该文件。