[root@master ~]# kubectl explain pods.spec.containers.livenessProbe
KIND:Pod
VERSION:v1RESOURCE: livenessProbe DESCRIPTION:
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-probesProbe describes a health check to be performed against a container to
determine whether it is alive or ready to receive traffic.FIELDS:
exec
One and only one of the following should be specified. Exec specifies the
action to take.failureThreshold
Minimum consecutive failures for the probe to be considered failed after
having succeeded. Defaults to 3. Minimum value is 1.httpGet
HTTPGet specifies the http request to perform.initialDelaySeconds
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-probesperiodSeconds
How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
value is 1.successThreshold
Minimum consecutive successes for the probe to be considered successful
after having failed. Defaults to 1. Must be 1 for liveness and startup.
Minimum value is 1.tcpSocket
TCPSocket specifies an action involving a TCP port. TCP hooks not yet
supportedtimeoutSeconds
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
[root@master ~]# kubectl explain pods.spec.containers.livenessProbe.exec
KIND:Pod
VERSION:v1RESOURCE: exec DESCRIPTION:
One and only one of the following should be specified. Exec specifies the
action to take.ExecAction describes a "run in container" action.FIELDS:
command<[]string>
Command is the command line to execute inside the container, the working
directory for the command is root ('/') in the container's filesystem. The
command is simply exec'd, it is not run inside a shell, so traditional
shell instructions ('|', etc) won't work. To use a shell, you need to
explicitly call out to that shell. Exit status of 0 is treated as
live/healthy and non-zero is unhealthy.
[root@master ~]# vim liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-container
namespace: default
spec:
containers:
- name: liveness-exec-container
image: busybox:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/healthy;
sleep 30;
rm -rf /tmp/healthy;
sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/healthy"]#检测文件是否存在,判断存活
initialDelaySeconds: 1
periodSeconds: 3[root@master ~]# kubectl get pod
NAMEREADYSTATUSRESTARTSAGE
liveness-exec-container1/1Running07s
[root@master ~]# kubectl describe pod liveness-exec-container
...
Events:
TypeReasonAgeFromMessage
-------------------------
NormalScheduled16sdefault-schedulerSuccessfully assigned default/liveness-exec-container to node1
NormalPulled16skubeletContainer image "busybox:latest" already present on machine
NormalCreated16skubeletCreated container liveness-exec-container
NormalStarted15skubeletStarted container liveness-exec-container
此时可以看到容器启动成功,等待一段时间再次查看,会发现Liveness probe failed,容器重启
[root@master ~]# kubectl describe pod liveness-exec-container
...
Restart Count:3
...
Events:
TypeReasonAgeFromMessage
-------------------------
NormalScheduled40sdefault-schedulerSuccessfully assigned default/liveness-exec-container to node1
NormalPulled40skubeletContainer image "busybox:latest" already present on machine
NormalCreated40skubeletCreated container liveness-exec-container
NormalStarted39skubeletStarted container liveness-exec-container
WarningUnhealthy2s (x3 over 8s)kubeletLiveness probe failed:
NormalKilling2skubeletContainer liveness-exec-container failed liveness probe, will be restarted
查看livenessProbe下的httpGet的使用
[root@master ~]# kubectl explain pods.spec.containers.livenessProbe.httpGet
KIND:Pod
VERSION:v1RESOURCE: httpGet DESCRIPTION:
HTTPGet specifies the http request to perform.HTTPGetAction describes an action based on HTTP Get requests.FIELDS:
host >
Host name to connect to, defaults to the pod IP. You probably want to set
"Host" in httpHeaders instead.httpHeaders<[]Object>
Custom headers to set in the request. HTTP allows repeated headers.path >
Path to access on the HTTP server.port > -required-
Name or number of the port to access on the container. Number must be in
the range 1 to 65535. Name must be an IANA_SVC_NAME.scheme>
Scheme to use for connecting to the host. Defaults to HTTP.