k8s_livenessProbe & readinessProbe(基础)的零散笔记
结论: livenessProbe中有三种测试方法,分别是exec、 httpGet、tcpSocketexec 的yaml范例apiVersion: v1kind: Podmetadata:labels:test: liveness-execname: liveness-execspec:containers:- name: liveness-demo...
·
结论:
livenessProbe中有三种测试方法,分别是exec、 httpGet、tcpSocket
readinessProbe 是就绪状态检测,和livenessProbe一样,它也有三种检测形式。
name | 用途 |
---|---|
livenessProbe | 用于检测节点是否处于存活(健康)状态,如有异常会直接重启pod |
readinessProbe | 用于检测节点是否处于就绪(ready)状态,可以在kubectl get pod中看到 x/5时,x代表就绪的容器数。service会通过pod的就绪状态,来决定是否要将请求转发给该节点。 |
下面都是基于livenessProbe的范例
exec 的yaml范例
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness-exec
name: liveness-exec
spec:
containers:
- name: liveness-demo
image: busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600 容器会生成一个代表健康状态的文件夹,并保持30s, 随后将其删除。
livenessProbe: #健康状态检查,通过执行test -e /tmp/healthy 检查文件夹是否存在,如果异常则重启pod 。 一段时间后回在RESTARTS栏位观察到重启次数不断增加。
exec: #此处使用exec来执行命令
command:
- test #测试
- -e #存在性判断
- /tmp/healthy
httpget 的yaml范例
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness-demo
image: nginx:1.14-alpine #nginx
ports: #这里定义了http=80端口
- name: http
containerPort: 80
lifecycle: #生命周期
postStart: #在容器后马上执行,如果有多个postStart,则串行执行
exec:
command:
- /bin/sh
- -c
- 'echo Healty > /usr/share/nginx/html/healthz' #放一个文件到对应目录代表健康状态
livenessProbe:
httpGet:
path: /healthz #定义路径
port: http #端口 http=80 上面定义的,如果没有定义可以直接写80
scheme: HTTP #协议
periodSeconds: 2 #检测周期2s
failureThreshold: 3 #连续3次算失败
initialDelaySeconds: 3 # 容器创建后等待三秒才开始检测。
可以用exec 进入容器,删除文件来观察pod的restart数值是否发生变化
使用describe检查时会看到http-get http://:http/healthz ,这里并没有主机地址,就意味着向本机发请求
tcpSocket 的yaml范例
[root@test chapter4]# cat liveness-tcpsocket.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-tcpsocket
spec:
containers:
- name: liveness-tcpsocket
image: nginx:1.14-alpine
ports:
- name: http
containerPort: 80
livenessProbe:
tcpSocket:
port: 81
periodSeconds: 2
failureThreshold: 3
initialDelaySeconds: 3
注意,这里可以故意设错检测端口,使用get pods 命令时会发现容器会不断重启。
[root@test chapter4]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
liveness-exec 0/1 CrashLoopBackOff 22 96m
liveness-http 1/1 Running 1 46m
liveness-tcpsocket 1/1 Running 0 6s
pod-demo 1/1 Running 0 6h12m
liveness-tcpsocket 1/1 Running 1 8s
liveness-exec 1/1 Running 23 96m
liveness-tcpsocket 1/1 Running 2 17s
liveness-tcpsocket 0/1 CrashLoopBackOff 2 25s
liveness-tcpsocket 1/1 Running 3 40s
liveness-tcpsocket 1/1 Running 4 49s
其他MEMO:
kubectl get pods -w
注意这里的-w选项会实时输出状态。
- exec 后会跟上command,command在容器内部运行的,默认在/下执行。
- httpget 使用describe检查时会看到http-get http://:http/healthz ,这里并没有主机地址,就意味着向本机发请求
- 使用exec进入指定容器后,可以使用ctrl p q 退出当前会话
- kubectl exec readiness-exec – rm -f /test/ready #不进入会话,直接删文件
- 看到这种错,请检查你的yaml文件格式
[root@test chapter4]# kubectl apply -f liveness-http.yaml
error: error validating "liveness-http.yaml": error validating data: [ValidationError(Pod.spec.containers[0].livenessProbe.httpGet): u nknown field "failureThreshold" in io.k8s.api.core.v1.HTTPGetAction, ValidationError(Pod.spec.containers[0].livenessProbe.httpGet): un known field "initalDelaySeconds" in io.k8s.api.core.v1.HTTPGetAction, ValidationError(Pod.spec.containers[0].livenessProbe.httpGet): u nknown field "periodSeconds" in io.k8s.api.core.v1.HTTPGetAction]; if you choose to ignore these errors, turn validation off with --validate=false
- 看到下列错误时,记得一些情况下要先删pod (可能不准确,仅供参考)
[root@test chapter4]# kubectl apply -f liveness-tcpsocket.yaml
The Pod "liveness-tcpsocket" is invalid: spec: Forbidden: pod updates may not change fields other than `spec.containers[*].image`, `spec.initContainers[*].image`, `spec.activeDeadlineSeconds` or `spec.tolerations` (only additions to existing tolerations)
core.PodSpec{
Volumes: []core.Volume{{Name: "default-token-dwm22", VolumeSource: core.VolumeSource{Secret: &core.SecretVolumeSource{SecretName: "default-token-dwm22", DefaultMode: &420}}}},
InitContainers: nil,
Containers: []core.Container{
{
... // 9 identical fields
VolumeMounts: []core.VolumeMount{{Name: "default-token-dwm22", ReadOnly: true, MountPath: "/var/run/secrets/kubernetes.io/serviceaccount"}},
VolumeDevices: nil,
LivenessProbe: &core.Probe{
Handler: core.Handler{
Exec: nil,
HTTPGet: nil,
TCPSocket: &core.TCPSocketAction{
Port: intstr.IntOrString{
Type: 0,
- IntVal: 1020,
+ IntVal: 80,
StrVal: "",
},
Host: "",
},
},
InitialDelaySeconds: 3,
TimeoutSeconds: 1,
... // 3 identical fields
},
ReadinessProbe: nil,
StartupProbe: nil,
... // 8 identical fields
},
},
EphemeralContainers: nil,
RestartPolicy: "Always",
... // 24 identical fields
}
更多推荐
已为社区贡献13条内容
所有评论(0)