我们一般将pod对象创建这段时间为pod的声明周期,主要包含夏眠的过程:
- pod创建过程
- 运行初始化容器过程
- 运行主容器过程
- 容器启动后钩子,容器终止前钩子
- 容器的存货性探测,就序性探测
4. pod终止过程
容器探测
- liveness probe:存活性探针,用于检测应用实列当前够处于正常运行状态,若不是,k8s会重启容器
- rediness probes:就序性探针,用于检测应用实列当前是否可以接受请求,如果不饿能,k8s不会转发流量
- exec
- tcpsocket
- httpget
[root@master ~]# cat pod.base.yaml [root@master ~]# cat pod.base.yaml apiVersion: v1 kind: Pod metadata: name: test-v1 namespace: dev labels: version: "1" env: "test" spec: containers: - name: nginx1 image: nginx:1 ports: - name: nginx-port containerPort: 80 resources: limits: cpu: "0.1" memory: "1G" livenessProbe: exec: command: ["/bin/cat","/tmp/h.t"] #执行查看一个文件 - name: busybox image: busybox command: ["/bin/sh","-c","touch /tmp/1.txt;while true;do /bin/echo $(date + %T)>> /tmp/1.txt; sleep 3; done;"] env: - name: "HOSTNAME" value: "slurmmaster"
通过查看pod,发现容器是一直尝试重启
---- ------ ---- ---- -------
Normal Scheduled 2m28s default-scheduler Successfully assigned dev/test-v1 to server1
Normal Pulling 2m28s kubelet Pulling image "busybox"
Normal Started 2m27s kubelet Started container busybox
Normal Pulled 2m27s kubelet Successfully pulled image "busybox" in 368.507155ms
Normal Created 2m27s kubelet Created container busybox
Normal Created 88s (x3 over 2m28s) kubelet Created container nginx1
Normal Started 88s (x3 over 2m28s) kubelet Started container nginx1
Normal Pulled 88s (x3 over 2m28s) kubelet Container image "nginx:1" already present on machine
Warning Unhealthy 58s (x9 over 2m18s) kubelet Liveness probe failed: /bin/cat: /tmp/h.t: No such file or directory
Normal Killing 58s (x3 over 118s) kubelet Container nginx1 failed liveness probe, will be restarted
重启策略
restartPolicy: //属于pod的子选项
- always:容器失效时,自动重启该容器,这也是默认值
- onfalilure: 容器终止运行且退出代码不为0时重启
- never: 不论状态为何,都不重启该容器