k8s pod
前言
k8s最小的单位pod
一、pod yaml
apiVersion: v1
kind: Pod
metadata:
name: lifecycle
spec:
restartPolicy: OnFailure
terminationGracePeriodSeconds: 5 ## 在pod进入Terminating 状态; 等待5s后会被强制kill,默认是30s
containers:
- image: centos # 镜像
imagePullPolicy: IfNotPresent
name: lifecycle1
##command: ["/bin/sh", "-c", " sh /test/test.sh "]
command: ["/bin/sh", "-c", " /test/test"]
volumeMounts:
- mountPath: /test
name: test-volume
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello from the postStart handler "]
preStop:
exec:
command: ["/bin/sh", "-c", " echo Hello from the preStop handler > /test/preStopedgdge "]
volumes:
- name: test-volume
hostPath:
path: /Users/test/docker/k8s/lifecycle
type: Directory
二、restartPolicy
- Always:当容器失效时,由kubelet自动重启该容器。
- OnFailure:当容器终止运行且退出码不为0时,由kubelet自动重启该容器。
- Never:不论容器运行状态如何,kubelet都不会重启该容器。
三、terminationGracePeriodSeconds
- 在pod被删除的时候会进入Terminating 状态,这个时候会等待一段时间,最长等待terminationGracePeriodSeconds秒,等待pod里面的容器关闭,pod才会被真正的删除,如果超时了pod会被强制删除。
- 假如设置成10s,当pod被删除的,pod里面的容器就会收到SIGTERM, SIGQUIT, SIGINT这些信息,这个时候可以进行一些优雅关闭之类的操作,如果10秒过去,容器还没有关闭,那么pod就会被管制关闭。
- 默认为30秒
总结
之后会补充一写关于生命周期钩子的说明