探测类型:
ExecAction、TCPSocketAction、HTTPGetAction
使用命令kubectl explain pods.spec.containers可以看到的三个主要类型;
(1)存活探针,容器是否存活;
livenessProbe
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
(2)就绪探针,容器是否就绪;
readinessProbe
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
注意:存活未必就绪;
三种方式:
exec
One and only one of the following should be specified. Exec specifies the
action to take.
httpGet
HTTPGet specifies the http request to perform.
tcpSocket
TCPSocket specifies an action involving a TCP port. TCP hooks not yet
supported
(3)优雅启停;
lifecycle
Actions that the management system should take in response to container
lifecycle events. Cannot be updated.
postStart
PostStart is called immediately after a container is created. If the
handler fails, the container is terminated and restarted according to its
restart policy. Other management of the container blocks until the hook
completes. More info:
https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
preStop
PreStop is called immediately before a container is terminated due to an
API request or management event such as liveness/startup probe failure,
preemption, resource contention, etc. The handler is not called if the
container crashes or exits. The reason for termination is passed to the
handler. The Pod’s termination grace period countdown begins before the
PreStop hooked is executed. Regardless of the outcome of the handler, the
container will eventually terminate within the Pod’s termination grace
period. Other management of the container blocks until the hook completes
or until the termination grace period is reached. More info:
https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
Pod的YAML框架:
apiVersion:v1
kind:pod
metadata:
name:
namespace:
labels:
annotations:
…
spec:
containers:
-name:
image:
imagePullPolicy:(#Always、Never、IfNotPresent)
ports:
name:
containerPort:
lifecycle:
livenessProbe:
readinessProbe:
restartPolicy:(#Always、Never、OnFailture)
nodeName:
nodeSelector:
status:(只读)
示例:
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/healthy”]
initialDelaySeconds: 1
periodSeconds: 3
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
namespace: default
spec:
containers:
- name: readiness-httpget-container
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports: - name: http
containerPort: 80
readinessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3