1.kubectl explain pods.spec.containers查看容器信息。 lifecycle <Object>选取pod整个生命周期针,针对启动后和终止前。livenessProbe <Object>进行存活性探测针状态描述。 readinessProbe <Object>进行就绪性探测针状态描述。
[root@master manifests]# kubectl explain pods.spec.containers
lifecycle <Object>
Actions that the management system should take in response to container
lifecycle events. Cannot be updated.
livenessProbe <Object>
Periodic probe of container liveness. Container will be restarted if the
probe fails. Cannot be updated. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
readinessProbe <Object>
Periodic probe of container service readiness. Container will be removed
from service endpoints if the probe fails. Cannot be updated. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
2.kubectl explain pods.spec.containers.livenessProbe查看livenessProbe下的三种探针类型,exec <Object>,httpGet <Object>,tcpSocket <Object>。 failureThreshold <integer>指明失败后继续探测的次数。periodSeconds <integer>指明再次探测间隔的时间。timeoutSeconds <integer>指明探测等待的时间。initialDelaySeconds <integer>指初始化延迟探测的时间,因为容器不可能一启动就马上运行,等待一段时间其完全启动。
[root@master manifests]# kubectl explain pods.spec.containers.livenessProbe
exec <Object>
One and only one of the following should be specified. Exec specifies the
action to take.
failureThreshold <integer>
Minimum consecutive failures for the probe to be considered failed after
having succeeded. Defaults to 3. Minimum value is 1.
httpGet <Object>
HTTPGet specifies the http request to perform.
initialDelaySeconds <integer>
Number of seconds after the container has started before liveness probes
are initiated. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
periodSeconds <integer>
How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
value is 1.
successThreshold <integer>
Minimum consecutive successes for the probe to be considered successful
after having failed. Defaults to 1. Must be 1 for liveness. Minimum value
is 1.
tcpSocket <Object>
TCPSocket specifies an action involving a TCP port. TCP hooks not yet
supported
timeoutSeconds <integer>
Number of seconds after which the probe times out. Defaults to 1 second.
Minimum value is 1. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
3.编辑liveness-exec.yaml 文件,kubectl create -f liveness-exec.yaml使用资源脚本创建pod,使用 livenessProbe:探针探测/tmp/healty文件,因为command: 在执行的过程中已经将/tmp/healty文件删除,kubectl get pods可以获得liveness-exec-pod的RESTARTS的次数为3,对liveness-exec.yaml 进行验证。
[root@master manifests]# vim liveness-exec.yaml
[root@master manifests]# cat liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-container
image: busybox:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/healty"]
initialDelaySeconds: 1
periodSeconds: 3
[root@master manifests]# kubectl create -f liveness-exec.yaml
pod/liveness-exec-pod created
[root@master manifests]# kubectl get pods
NAME READY STAT