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

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

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

问题

我已经用中文翻译了你的内容:

我用Go语言编写了一个作业调度服务,它在固定时间间隔内上传文件。该服务内部没有运行HTTP服务。

通常情况下,对于HTTP服务,需要在部署文件中添加以下就绪探针和存活探针配置:

就绪探针:

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: 触发服务的主函数
func main() {
    scheduler.RunScheduler()
}

// RunScheduler: 运行调度器
func RunScheduler() {
    done := make(chan bool)
    quit := make(chan bool)

    go startJob1()

    go startJob2()

    sigs := make(chan os.Signal, 1)
    // 只有当应用程序停止时,传递 done "True"
    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
    <-sigs
    .....
}

该服务只是定期运行,并且不监听任何端口,我们能配置任何类型的就绪探针和存活探针吗?

英文:

I have written a job scheduler service in go, which uploads files under a periodic interval. There is no HTTP service is running inside that service.

Normally, for HTTP service, below are liveness and readiness probe configuration, need to be added at the deployment file.

readinessProbe:
  httpGet:
    path: &lt;http_get_path&gt;
    port: 3401
  initialDelaySeconds: 5
  timeoutSeconds: 1
  periodSeconds: 15

livenessProbe:
  httpGet:
    path: &lt;http_get_path&gt;
    port: 3401
  initialDelaySeconds: 15
  timeoutSeconds: 1
  periodSeconds: 15

For standalone service like below:

//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 &quot;True&quot;, only if the application is stopped
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	&lt;-sigs
    .....
}

which just runs periodically and don't listen to any port, can we configure any kind of readiness probe and liveness probe?

答案1

得分: 3

由于您的容器不处理任何HTTP请求,因此不需要readinessProbe

> readinessProbe:指示容器是否准备好响应请求。如果就绪探针失败,端点控制器将从所有与Pod匹配的服务的端点中删除Pod的IP地址。

何时应使用存活探针?

您需要一个livenessProbe

> livenessProbe:指示容器是否正在运行。如果存活探针失败,kubelet将终止容器,并将容器受其重启策略的影响。

何时应使用就绪探针?

以下是一个使用ps命令的示例:

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

请确保在$PATH中有ps命令。

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

英文:

Since your container doesn't handle any http request you dont need 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?

You need a 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?

Here is an example with ps of process:

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

Be sure you have a ps command in the $PATH.

Another way is to create a file in the tmp dir on the pod start and check if a file present in the exec command.

huangapple
  • 本文由 发表于 2020年8月7日 15:36:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63297290.html
匿名

发表评论

匿名网友

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

确定