Liveness Probe
LivenessProbe:kubelet使用livenessProbe在容器出现异常的时候进行重启的操作。
ReadinessProbe:Kubelet使用ReadinessProbe来指定Container是否为Ready状态。
结合Service用来限定哪些Ready的Container来接受流量。因为Service会将Container状态为Ready的Pod添加到Endpoint里,进行流量的分发。
Crontainer诊断方法:
Exec:在Container内执行指定的命令。如果命令以状态代码0退出,则认为诊断成功。
TCPSocket:对指定端口上的Container的IP地址执行TCP检查。如果端口打开,则诊断被认为是成功的。
HTTPGet:对指定端口和路径上的Container的IP地址执行HTTP
Get请求。如果响应的状态代码大于或等于200且小于400,则认为诊断成功。
cat << EOF > pod-liveness-command.yamlapiVersion: v1kind: Podmetadata:labels:test: livenessname: liveness-execspec:containers:- name: livenessimage: radial/busyboxplusimagePullPolicy: IfNotPresentargs:- /bin/sh- -c- touch /tmp/healthy; sleep 60; rm -rf /tmp/healthy; sleep 600livenessProbe:exec:command:- cat- /tmp/healthyinitialDelaySeconds: 5periodSeconds: 5EOF
[root@master01 damon]# kubectl apply -f pod-liveness-command.yaml
[root@master01 damon]# watch -n1 kubectl get pod //通过查看发现liveness-exec的RESTARTS 在30秒后由于检测到不健康一直在重启
NAME READY STATUS RESTARTS AGE
liveness-exec 1/1 Running 0 3s
nginx-deployment-676cc869-bnnvd 1/1 Running 0 30m
nginx-deployment-676cc869-ft8bw 1/1 Running 0 26m
[root@master01 damon]# kubectl get event // 查看Kubernetes事件
liveness HTTP request
**
cat << EOF > pod-liveness-http.yamlapiVersion: v1kind: Podmetadata:labels:test: livenessname: liveness-httpspec:containers:- name: livenessimage: seedoflife/livenessimagePullPolicy: IfNotPresentargs:- /serverlivenessProbe:httpGet:path: /healthzport: 8080httpHeaders:- name: X-Custom-Headervalue: AwesomeinitialDelaySeconds: 3periodSeconds: 3EOF
[root@master01 damon]# kubectl apply -f pod-liveness-http.yaml
[root@master01 damon]# curl -v 192.168.1.62:8080/healthz
About to connect() to 192.168.1.62 port 8080 (#0)
Trying 192.168.1.62…
Connected to 192.168.1.62 (192.168.1.62) port 8080 (#0)
> GET /healthz HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.62:8080
> Accept: /
>
< HTTP/1.1 200 OK // OK显示正常
< Date: Tue, 20 Nov 2018 12:01:26 GMT
< Content-Length: 2
< Content-Type: text/plain; charset=utf-8
<
Connection #0 to host 192.168.1.62 left intact
ok[root@master01 damon]# curl -v 192.168.1.62:8080/healthz
About to connect() to 192.168.1.62 port 8080 (#0)
Trying 192.168.1.62…
Connected to 192.168.1.62 (192.168.1.62) port 8080 (#0)
> GET /healthz HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.62:8080
> Accept: /
>
< HTTP/1.1 500 Internal Server Error // error检测异常
< Date: Tue, 20 Nov 2018 12:01:39 GMT
< Content-Length: 19
< Content-Type: text/plain; charset=utf-8
<
Connection #0 to host 192.168.1.62 left intact
TCP liveness probe
cat << EOF > pod-liveness-tcp.yamlapiVersion: v1kind: Podmetadata:name: liveness-tcplabels:app: liveness-tcpspec:containers:- name: liveness-tcpimage: python:2.7imagePullPolicy: IfNotPresentcommand: ["bash", "-c", "echo test > index.html && sleep 30 && python -m SimpleHTTPServer 8080"]ports:- containerPort: 8080readinessProbe:tcpSocket:port: 8080initialDelaySeconds: 35periodSeconds: 10livenessProbe:tcpSocket:port: 8080initialDelaySeconds: 15periodSeconds: 20EOF
[root@master01 ~]# kubectl apply -f pod-liveness-tcp.yaml
[root@master01 ~]# kubectl get pod —watch //参看30秒以内podr的R
NAME READY STATUS RESTARTS AGE
goproxy 1/1 Running 1 5h46m
liveness-tcp 0/1 Running 0 4s
readinessProbe
readinessProbe + livenessProbe+Service**
探针具有许多字段,可用于更精确地控制活动性和准备情况检查的行为:
initialDelaySeconds:启动容器后,启动活动或就绪探测器的秒数。默认为0秒。最小值为0。
periodSeconds:执行探测的频率(以秒为单位)。默认为10秒。最小值为1。
timeoutSeconds:探测超时的秒数。默认为1秒。最小值为1。
successThreshold:探测失败后,连续最小成功探测为成功。默认值为1,最小值为1。
failureThreshold:当Pod启动并且探测失败时,Kubernetes会尝试failureThreshold多次,当多次探测失败后执行重启容器的动作。放弃。默认值为3,最小值为1
cat << EOF > service-healthcheck.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: service-healthspec:replicas: 2selector:matchLabels:app: service-healthtemplate:metadata:labels:app: service-healthspec:containers:- name: service-healthimage: python:2.7imagePullPolicy: IfNotPresentcommand: ["/bin/bash","-c","echo \$(hostname) > index.html && sleep 30 && python -m SimpleHTTPServer 8080"]ports:- containerPort: 8080readinessProbe:tcpSocket:port: 8080initialDelaySeconds: 10periodSeconds: 10EOF
**
kubectl apply -f service-healthcheck.yaml
创建service
kubectl expose deployment service-health
[root@master01 ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1
service-health ClusterIP 10.109.21.81
curl 10.109.21.81:8080
