探测的目的
deployment的作用是用来维持 pod的健壮性
当pod挂掉之后,deployment会生成新的pod
但如果pod是正常运行的,而pod里面又出了问题,此时deployment是监测不到的。
故此需要探测(probe)
用户定义 “出现什么样的状况 “才叫出问题
当probe监测到此问题,会认为pod出现了问题,执行“重启大法”来解决问题
参数的意义
initialDelaySeconds:容器启动后第一次执行探测是需要等待多少秒。
periodSeconds:执行探测的频率,默认是10秒,最小1秒。
timeoutSeconds:探测超时时间,默认1秒,最小1秒。
successThreshold:探测失败后,最少连续探测成功多少次才被认定为成功,默认是1,对于liveness必须 是1,最小值是1。
failureThreshold:当 Pod 启动了并且探测到失败,Kubernetes 的重试次数。存活探测情况下的放弃就意味 着重新启动容器。就绪探测情况下的放弃 Pod 会被打上未就绪的标签。默认值是 3。最小值是 1
https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-liveness-readiness-
startup-probes/#configure-probes
liveness probe –command
[root@vms61 chap7-probe]# cat pod1.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: pod1
spec:
terminationGracePeriodSeconds: 0
containers:
- name: liveness
image: busybox
imagePullPolicy: IfNotPresent
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 100000
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
[root@vms61 chap7-probe]# kubectl apply -f pod1.yaml
pod/pod1 created
liveness probe –httpGET
[root@vms61 chap7-probe]# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: pod1
spec:
containers:
- name: liveness
image: nginx
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
path: /index.html
port: 80
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
[root@vms61 chap7-probe]# kubectl apply -f pod2.yaml
pod/pod1 created
[root@vms61 chap7-probe]# kubectl exec pod1 -- rm /usr/share/nginx/html/index.html
liveness probe –tcp
[root@vms61 chap7-probe]# cat pod3.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: pod1
spec:
containers:
- name: liveness
image: nginx
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
tcpSocket:
port: 80
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
readiness probe –command
liveness通过重启来解决问题
readiness检测到问题,并不重启
[root@vms61 chap7-probe]# cat web1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web1
name: web1
spec:
replicas: 3
selector:
matchLabels:
app: web1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web1
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
readinessProbe:
exec:
command:
- cat
- /usr/share/nginx/html/index.html
initialDelaySeconds: 5
periodSeconds: 5
status: {}
[root@vms61 chap7-probe]# kubectl apply -f web1.yaml
deployment.apps/web1 created
[root@vms61 chap7-probe]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web1-58468bf858-4r89d 0/1 Running 0 6s
web1-58468bf858-hzs2b 0/1 Running 0 6s
web1-58468bf858-kr8l9 0/1 Running 0 6s
[root@vms61 chap7-probe]# kubectl exec -it web1-58468bf858-4r89d -- bash
root@web1-58468bf858-4r89d:/# echo 11111 > /usr/share/nginx/html/index.html
root@web1-58468bf858-4r89d:/# exit
exit
[root@vms61 chap7-probe]# kubectl exec -it web1-58468bf858-hzs2b -- bash
root@web1-58468bf858-hzs2b:/# echo 22222 > /usr/share/nginx/html/index.html
root@web1-58468bf858-hzs2b:/# exit
exit
[root@vms61 chap7-probe]# kubectl exec -it web1-58468bf858-kr8l9 -- bash
root@web1-58468bf858-kr8l9:/# echo 33333 > /usr/share/nginx/html/index.html
root@web1-58468bf858-kr8l9:/# exit
exit
[root@vms61 chap7-probe]# kubectl expose deployment web1 --port=80 --target-port=80
service/web1 exposed
[root@vms61 chap7-probe]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web1 ClusterIP 10.111.157.194 <none> 80/TCP 4s
[root@vms61 chap7-probe]# curl -s 10.111.157.194
22222
[root@vms61 chap7-probe]# curl -s 10.111.157.194
33333
[root@vms61 chap7-probe]# curl -s 10.111.157.194
11111
[root@vms61 chap7-probe]# curl -s 10.111.157.194
33333
[root@vms61 chap7-probe]# curl -s 10.111.157.194
22222
[root@vms61 chap7-probe]# curl -s 10.111.157.194
11111
正文完