【Kubernetes】启动后执行-HTTPGET
环境
- kubernetes 1.20.4
- Spring Boot 2.5.0-M2
目标
在容器启动后,访问一个 HTTP 的 GET 请求。
示例
Pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
containers:
- name: busybox
image: busybox:stable
lifecycle:
postStart:
httpGet:
scheme: HTTP
port: 80
host: www.baidu.com
resources:
limits:
memory: "128Mi"
cpu: "500m"
command: ["sleep", "3600"]
查看
[root@master ~]# kubectl get pod busybox -o jsonpath="{.spec.containers[0].lifecycle}" | jq
{
"postStart": {
"httpGet": {
"host": "www.baidu.com",
"path": "/",
"port": 80,
"scheme": "HTTP"
}
}
}
测试错误 GET
将端口从 80 修改为 8080,再次创建 Pod,查看 event 里的信息。
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 80s default-scheduler Successfully assigned default/busybox to node2
Warning FailedPostStartHook 50s kubelet Http lifecycle hook (/) for Container "busybox" in Pod "busybox_default(b31715ff-55bc-4988-8b18-98804dd70eb3)" failed - error: Get "http://www.baidu.com:8080//": dial tcp 14.215.177.39:8080: i/o timeout, message: ""
Normal Killing 50s kubelet FailedPostStartHook
Normal Pulled 19s (x2 over 80s) kubelet Container image "busybox:stable" already present on machine
Normal Created 19s (x2 over 80s) kubelet Created container busybox
Normal Started 19s (x2 over 80s) kubelet Started container busybox
可以看到有一个失败的事件,证明 GET 请求没有成功。
总结
通过参与容器的生命周期,在容器启动后,在容器中执行了一个 HTTP 的 GET 请求。
附录
分类:
Kubernetes
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!