探测延迟和探测周期是5秒钟。
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name: liveness
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30;
rm -rf /tmp/healthy; sleep 600
image: busybox
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 92s default-scheduler Successfully assigned default/liveness-exec to k8s-node2
Normal Killing 47s kubelet, k8s-node2 Container liveness failed liveness probe, will be restarted
Warning Unhealthy 47s (x3 over 57s) kubelet, k8s-node2 Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
Normal Pulled 17s (x2 over 92s) kubelet, k8s-node2 Successfully pulled image "busybox"
Normal Created 17s (x2 over 92s) kubelet, k8s-node2 Created container liveness
Normal Pulling 17s (x2 over 92s) kubelet, k8s-node2 Pulling image "busybox"
Normal Started 16s (x2 over 91s) kubelet, k8s-node2 Started container liveness
Liveness: exec [cat /tmp/healthy] delay=5s timeout=1s period=5s #success=1 #failure=3
Delay=5s表示探针在容器启动后5秒开始进行第一次探测。
Timeout=1s表示容器必须在1秒内反馈信息给探针,否则视为失败。
Period=5s表示每5秒探针进行一次探测。
#success=1表示探测连续成功1次,表示成功。
#failure=3表示探测连续失败3次,视为Pod处于failure状态,重启容器
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
timeoutSeconds: 1
successThreshold: 1
successThreshold: 3
apiVersion: v1
kind: Pod
metadata:
name: ubuntu
labels:
app: ubuntu
spec:
containers:
- name: ubuntu
image: ubuntu
args:
- /bin/sh
- -c
- apt-get update && apt-get -y install openbsd-inetd telnetd && /etc/init.d/openbsd-inetd start; sleep 30000
livenessProbe:
tcpSocket:
port: 23
initialDelaySeconds: 60
periodSeconds: 20
类型 | 就绪探针 | 存活探针 |
---|---|---|
当pod未通过检测 | 等待 | 杀死pod,重启pod |
服务 | 如果检测失败,则从endpoint中移除pod | endpoint自动更新pod信息 |
作用 | pod是否准备好服务 | pod是否存活 |
kind: Service
metadata:
name: httpd-svc
spec:
selector:
app: httpd
ports:
- protocol: TCP
port: 8080
targetPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-deployment
spec:
replicas: 3
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd
ports:
- containerPort: 80
readinessProbe:
exec:
command:
- cat
- /usr/local/apache2/htdocs/index.html
initialDelaySeconds: 5
periodSeconds: 5
Endpoints: 10.244.1.15:80,10.244.2.22:80,10.244.2.23:80
NAME READY STATUS RESTARTS AGE
httpd-deployment-859778b7b6-7wcbt 1/1 Running 0 9m42s
httpd-deployment-859778b7b6-p62mw 1/1 Running 0 9m42s
httpd-deployment-859778b7b6-zvpsg 1/1 Running 0 9m42s
[root@k8s-master probe]# kubectl exec -it httpd-deployment-859778b7b6-7wcbt /bin/sh
\# rm /usr/local/apache2/htdocs/index.html
Endpoints: 10.244.1.15:80,10.244.2.23:80
NAME READY STATUS RESTARTS AGE
httpd-deployment-859778b7b6-7wcbt 0/1 Running 0 15m
httpd-deployment-859778b7b6-p62mw 1/1 Running 0 15m
httpd-deployment-859778b7b6-zvpsg 1/1 Running 0 15m
Warning Unhealthy 102s (x24 over 3m37s) kubelet, k8s-node1 Readiness probe failed: cat: /usr/local/apache2/htdocs/index.html: No such file or directory