kubernetes-handbook/concepts/pod-hook.md

41 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Pod hook
Pod hook钩子是由Kubernetes管理的kubelet发起的当容器中的进程启动前或者容器中的进程终止之前运行这是包含在容器的生命周期之中。可以同时为Pod中的所有容器都配置hook。
Hook的类型包括两种
- exec执行一段命令
- HTTP发送HTTP请求。
参考下面的配置:
```yaml
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: lifecycle-demo-container
image: nginx
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
preStop:
exec:
command: ["/usr/sbin/nginx","-s","quit"]
```
在容器创建之后容器的Entrypoint执行之前这时候Pod已经被调度到某台node上被某个kubelet管理了这时候kubelet会调用postStart操作该操作跟容器的启动命令是在异步执行的也就是说在postStart操作执行完成之前kubelet会锁住容器不让应用程序的进程启动只有在 postStart操作完成之后容器的状态才会被设置成为RUNNING。
如果postStart或者preStop hook失败将会终止容器。
## 调试hook
Hook调用的日志没有暴露个给Pod的event所以只能通过`describe`命令来获取,如果有错误将可以看到`FailedPostStartHook`或`FailedPreStopHook`这样的event。
## 参考
- [Attach Handlers to Container Lifecycle Events](https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/)
- [Container Lifecycle Hooks](https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/)