1.3.2.6 livenessProbe 和 readinessProbe 的对比
配置参数一样
livenessProbe:连续探测失败会重启、重建 pod,readinessProbe 不会执行重启或者重建Pod操作
livenessProbe:连续检测指定次数失败后会将容器置于 (Crash Loop BackOff) 切不可用,readinessProbe 不
会
readinessProbe:连续探测失败会从 service 的 endpointd 中删除该 Pod,livenessProbe 不具备此功能,但是会将容器挂起 livenessProbe
livenessProbe 用户控制是否重启 pod,readinessProbe 用于控制 pod 是否添加至 service
建议:两个探针都配置
# 删除前面环境
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl delete -f redis-ExecAction.yml
deployment.apps "redis-deployment" deleted
service "waluna-redis-service" deleted
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
1.3.2.6.1 实现 livenessProbe
# 配置yaml文件
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# vim nginx-http.yml
......
#readinessProbe:
livenessProbe:
httpGet:
#path: /monitor/monitor.html
path: /index.html
port: 80
initialDelaySeconds: 5
periodSeconds: 3
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
......
# 创建pod
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl apply -f nginx-http.yml
deployment.apps/nginx-deployment created
service/waluna-nginx-service created
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 查看pod
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6889cb644-qgkcx 1/1 Running 0 13s
tomcat-deployment-6f6bc44c85-2fbh2 1/1 Running 0 2d23h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 查看service
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl describe svc waluna-nginx-service
Name: waluna-nginx-service
Namespace: default
Labels: app=waluna-nginx-service-label
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.20.198.52
IPs: 10.20.198.52
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30004/TCP
Endpoints: 10.10.3.32:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 删除pod
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl delete -f nginx-http.yml
deployment.apps "nginx-deployment" deleted
service "waluna-nginx-service" deleted
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 修改配置文件
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# vim nginx-http.yml
......
#readinessProbe:
livenessProbe:
httpGet:
path: /monitor/monitor.html
#path: /index.html
port: 80
initialDelaySeconds: 5
periodSeconds: 3
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
......
# 创建pod
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl apply -f nginx-http.yml
deployment.apps/nginx-deployment created
service/waluna-nginx-service created
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 查看pod和service
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-5555767bcf-rlp5w 1/1 Running 0 13s
tomcat-deployment-6f6bc44c85-2fbh2 1/1 Running 0 2d23h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.20.0.1 <none> 443/TCP 5d20h
waluna-nginx-service NodePort 10.20.223.1 <none> 80:30004/TCP 17s
waluna-tomcat-service NodePort 10.20.139.128 <none> 80:30005/TCP 5d3h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl describe svc waluna-nginx-service
Name: waluna-nginx-service
Namespace: default
Labels: app=waluna-nginx-service-label
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.20.223.1
IPs: 10.20.223.1
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30004/TCP
Endpoints: 10.10.3.33:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-5555767bcf-rlp5w 1/1 Running 1 27s
tomcat-deployment-6f6bc44c85-2fbh2 1/1 Running 0 2d23h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.20.0.1 <none> 443/TCP 5d20h
waluna-nginx-service NodePort 10.20.223.1 <none> 80:30004/TCP 30s
waluna-tomcat-service NodePort 10.20.139.128 <none> 80:30005/TCP 5d3h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl describe svc waluna-nginx-service
Name: waluna-nginx-service
Namespace: default
Labels: app=waluna-nginx-service-label
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.20.223.1
IPs: 10.20.223.1
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30004/TCP
Endpoints: 10.10.3.33:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-5555767bcf-rlp5w 0/1 CrashLoopBackOff 3 67s
tomcat-deployment-6f6bc44c85-2fbh2 1/1 Running 0 2d23h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.20.0.1 <none> 443/TCP 5d20h
waluna-nginx-service NodePort 10.20.223.1 <none> 80:30004/TCP 71s
waluna-tomcat-service NodePort 10.20.139.128 <none> 80:30005/TCP 5d3h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl describe svc waluna-nginx-service
Name: waluna-nginx-service
Namespace: default
Labels: app=waluna-nginx-service-label
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.20.223.1
IPs: 10.20.223.1
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30004/TCP
Endpoints:
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
从上面测试可以看到使用 livenessProbe 探针,会按照指定的策略,每次检测5s,检测三次不通过就会重启,重启过程中不会从service中摘除,重启三次后还不通过就会置为不可用,并会从service中删除。
1.3.2.6.2 实现 readinessProbe
# 删除前面实验pod
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl delete -f nginx-http.yml
deployment.apps "nginx-deployment" deleted
service "waluna-nginx-service" deleted
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 配置yaml文件
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# vim nginx-http.yml
......
readinessProbe:
#livenessProbe:
httpGet:
#path: /monitor/monitor.html
path: /index.html
port: 80
initialDelaySeconds: 5
periodSeconds: 3
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
......
# 创建pod
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl apply -f nginx-http.yml
deployment.apps/nginx-deployment created
service/waluna-nginx-service created
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 查看pod
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6b76d666c8-5cz2n 1/1 Running 0 8s
tomcat-deployment-6f6bc44c85-2fbh2 1/1 Running 0 2d23h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.20.0.1 <none> 443/TCP 5d20h
waluna-nginx-service NodePort 10.20.21.114 <none> 80:30004/TCP 12s
waluna-tomcat-service NodePort 10.20.139.128 <none> 80:30005/TCP 5d3h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl describe svc waluna-nginx-service
Name: waluna-nginx-service
Namespace: default
Labels: app=waluna-nginx-service-label
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.20.21.114
IPs: 10.20.21.114
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30004/TCP
Endpoints: 10.10.3.35:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 进入容器讲探测的文件移走
[root@k8s-master1 ~]# kubectl exec -it nginx-deployment-6b76d666c8-5cz2n bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-deployment-6b76d666c8-5cz2n:/# mv /usr/share/nginx/html/index.html /opt/
root@nginx-deployment-6b76d666c8-5cz2n:/# exit
exit
[root@k8s-master1 ~]#
# 再次查看pod
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6b76d666c8-5cz2n 0/1 Running 0 82s
tomcat-deployment-6f6bc44c85-2fbh2 1/1 Running 0 2d23h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl describe svc waluna-nginx-service
Name: waluna-nginx-service
Namespace: default
Labels: app=waluna-nginx-service-label
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.20.21.114
IPs: 10.20.21.114
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30004/TCP
Endpoints:
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6b76d666c8-5cz2n 0/1 Running 0 94s
tomcat-deployment-6f6bc44c85-2fbh2 1/1 Running 0 2d23h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
从上面实验可以看到使用 readinessProbe 探针,检测失败后会将pod从service中摘除。
1.3.2.6.3 同时配置 livenessProbe 和 readinessProbe
# 删除前面实验的pod
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl delete -f nginx-http.yml
deployment.apps "nginx-deployment" deleted
service "waluna-nginx-service" deleted
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 配置yaml文件
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# vim nginx-http.yml
......
livenessProbe:
httpGet:
#path: /monitor/monitor.html
path: /index.html
port: 80
initialDelaySeconds: 5
periodSeconds: 3
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
readinessProbe:
httpGet:
#path: /monitor/monitor.html
path: /index.html
port: 80
initialDelaySeconds: 5
periodSeconds: 3
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
......
# 创建pod
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl apply -f nginx-http.yml
deployment.apps/nginx-deployment created
service/waluna-nginx-service created
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 查看pod和service
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6b54b4595-tkjcr 1/1 Running 0 17s
tomcat-deployment-6f6bc44c85-2fbh2 1/1 Running 0 3d
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.20.0.1 <none> 443/TCP 5d21h
waluna-nginx-service NodePort 10.20.140.112 <none> 80:30004/TCP 20s
waluna-tomcat-service NodePort 10.20.139.128 <none> 80:30005/TCP 5d4h
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl describe svc waluna-nginx-service
Name: waluna-nginx-service
Namespace: default
Labels: app=waluna-nginx-service-label
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.20.140.112
IPs: 10.20.140.112
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30004/TCP
Endpoints: 10.10.3.37:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 进入容器删除检测文件
[root@k8s-master1 ~]# kubectl exec -it nginx-deployment-6b54b4595-tkjcr bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-deployment-6b54b4595-tkjcr:/# mv /usr/share/nginx/html/index.html /opt/
root@nginx-deployment-6b54b4595-tkjcr:/#
# 查看pod和service状态
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6b54b4595-tkjcr 0/1 Running 1 92s
tomcat-deployment-6f6bc44c85-2fbh2 1/1 Running 0 3d
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl describe svc waluna-nginx-service
Name: waluna-nginx-service
Namespace: default
Labels: app=waluna-nginx-service-label
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.20.140.112
IPs: 10.20.140.112
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30004/TCP
Endpoints:
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6b54b4595-tkjcr 1/1 Running 1 100s
tomcat-deployment-6f6bc44c85-2fbh2 1/1 Running 0 3d
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]# kubectl describe svc waluna-nginx-service
Name: waluna-nginx-service
Namespace: default
Labels: app=waluna-nginx-service-label
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.20.140.112
IPs: 10.20.140.112
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30004/TCP
Endpoints: 10.10.3.37:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master1 /data/kubeadm-waluna/nginx-yml]#
# 查看容器已经退出
[root@k8s-master1 ~]# kubectl exec -it nginx-deployment-6b54b4595-tkjcr bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-deployment-6b54b4595-tkjcr:/# mv /usr/share/nginx/html/index.html /opt/
root@nginx-deployment-6b54b4595-tkjcr:/# command terminated with exit code 137
[root@k8s-master1 ~]#
可以得出结论,readinessProbe 检测不通过后将pod从service中摘除,同时 livenessProbe 检测失败后执行重启,重启后 readinessProbe 检测通过后又将pod添加至service。
1.4 Pod 重启策略
k8s 在 Pod 出现异常的时候会自动将 Pod 重启以恢复 Pod 中的服务。
官方文档:
https://kubernetes.io/zh/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy
restartPolicy:
- Always:当容器异常时,k8s 自动重启该容器,ReplicationController/Replicaset/Deployment。
- OnFailure:当容器失败时 (容器停止运行且退出码不为0),k8s 自动重启该容器。
- Never:不论容器运行状态如何都不会重启该容器,Job 或 CronJob。
示例
containers:
- name: waluna-tomcat-app1-container
image: harbor.waluna.top/waluna/tomcat:app
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 8080
protocol: TCP
name: http
env:
- name: "password"
value: "123456"
- name: "age"
value: "18"
resources:
limits:
cpu: 1
memory: "512Mi"
requests:
cpu: 500m
memory: "512Mi"
restartPolicy: Always







Comments | NOTHING