环境
- kubernetes 1.20.4
- Spring Boot 2.5.0-M2
目标
在容器启动后,执行一个命令。
执行命令
Pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
containers:
- name: busybox
image: busybox:stable
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo hello ${HOSTNAME}! > name.txt"]
resources:
limits:
memory: "128Mi"
cpu: "500m"
command: ["sleep", "3600"]
查看
[root@master ~]# kubectl exec busybox -- cat name.txt
hello busybox!
总结
通过参与容器的生命周期,在容器启动后,在容器中执行了一个命令。