ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] # Pod 生命周期(钩子Hook) Pod钩子作用于: * Pod 创建后立刻执行**(PostStart)** * Pod 容器终止之前立刻执行**(PreStop)** **PostStart:** 并不能保证钩子将在容器`ENTRYPOINT`之前运行,不能进行参数传递给pod,感觉有点像异步执行 **PreStop:** 在容器终止之前调用,是阻塞的,需要执行完该步骤后,才能真正执行终止容器的步骤。主要用那些需要按照步骤一步一步进行关停的Pod(例如数据库???)。 钩子的类型分为两种: * exec:执行一段命令 * HTTP:发送HTTP请求 `postStart`和`preStop` ~~~ apiVersion: v1 kind: Pod metadata: name: lifecycle-demo labels: app: hook spec: containers: - name: lifecycle-demo-container image: nginx ports: - name: webport containerPort: 80 lifecycle: postStart: exec: command: ["/bin/sh", "-c", "echo Hello from the postStart handler >> /usr/share/message"] preStop: exec: command: ["/bin/sh", "-c", "echo Hello from the preStop handler >> /usr/share/message"] volumeMounts: - name: message #这里的name名称要和volumes相同 mountPath: /usr/share volumes: - name: message #创建 volumes hostPath: path: /tmp ~~~ 验证: ![](https://img.kancloud.cn/34/a6/34a65fa878f893c1dfe2e5ac8e504d36_464x105.png) ![](https://img.kancloud.cn/ca/11/ca11dbf94c899893c2b516612961ddf4_508x69.png) 查看pod所在宿主机的`/tmp`目录下的message文件 ![](https://img.kancloud.cn/b4/ce/b4cee34ef92d2c596e39c3b9ced60f82_266x59.png)