LivenessProbe探针用于判断容器是否存活,如果探测到容器不健康,则kubelet将杀掉该容器,然后根据重启策略处理。
LivenessProbe的实现方式:
下面我们就一一来实践一下
yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: liveness
name: liveness
spec:
containers:
- image: busybox
name: liveness
args:
- /bin/sh
- -c
- echo ok > /tmp/health; sleep 10; rm -rf /tmp/health; sleep 600
livenessProbe:
exec:
command:
- cat1
- /tmp/health
initialDelaySeconds: 5
timeoutSeconds: 1
我们把cat命令故意写错成cat1,测试如下:
可以看到探测不成功一直在重启,符合预期!
yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: liveness
name: liveness
spec:
containers:
- image: nginx
name: liveness
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 5
timeoutSeconds: 1
我们把port改成8080,测试如下:
可以看到探测失败在不断重启,符合预期!
yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: liveness
name: liveness
spec:
containers:
- image: nginx
name: liveness
livenessProbe:
httpGet:
port: 8080
path: /
initialDelaySeconds: 5
timeoutSeconds: 1
测试如下:
可以看到探测失败在不断重启,符合预期!