[单master节点k8s部署]8.pod健康探测

k8s默认的健康检查机制是,每个容器都有一个监控进程,如果进程退出时返回码非零,则认为容器发生故障。

存活探测

监测pod是否处于运行状态,当liveness probe探测失败的时候,根据重启策略判断是否需要重启。适用于需要在容器发生故障时需要立即重启的状态。

用指定的方式(exec,tcp,http等)监测pod中的容器是否正常运行

yaml文件如下:

[root@master yam_files]# cat live-http.yaml 
apiVersion: v1
kind: Pod
metadata:name: liveness-httpnamespace: defaultlabels:app: nginxspec:containers:- name: livenessimage: nginximagePullPolicy: IfNotPresentports:- containerPort: 80livenessProbe:httpGet: path: /index.htmlport: 80initialDelaySeconds: 5periodSeconds: 10readinessProbe:httpGet:path: /index.htmlport: 80initialDelaySeconds: 5periodSeconds: 10restartPolicy: Always 

新建的pod里面运行一个nginx,而且通过http访问,设定启动探测为5s,周期为10s,存活探测周期为10s,启动探测为5s。

启动pod后,破坏他,删除这个探测的路径index.html

kubectl exec -it liveness-http -- /bin/bash
root@liveness-http:/# cd /usr/share/nginx/html
root@liveness-http:/usr/share/nginx/html# ls
50x.html  index.html
root@liveness-http:/usr/share/nginx/html# rm index.html

 探针发现错误后重启pod

kubectl get pods -l app=nginx -w
NAME                          READY   STATUS    RESTARTS   AGE
liveness-http                 1/1     Running   0          5m37s
nginx-test-6cf9d87fbf-26h6m   1/1     Running   0          127m
nginx-test-6cf9d87fbf-wn94b   1/1     Running   0          7d17h
liveness-http                 0/1     Running   0          5m50s
liveness-http                 0/1     Running   1 (2s ago)   5m52s
liveness-http                 1/1     Running   1 (10s ago)   6m

随后用tcp做一个实验,写yaml文件如下

cat live-tcp.yaml
apiVersion: v1
kind: Pod
metadata:name: liveness-tcpspec:containers:- name: livenessimage: nginximagePullPolicy: IfNotPresentports:- containerPort: 80livenessProbe:tcpSocket:port: 80initialDelaySeconds: 2periodSeconds: 3

 随后启动pod,并且停掉其中的nginx服务

kubectl exec -it liveness-tcp -- /bin/bash
root@liveness-tcp:/# nginx -s stop
2024/06/24 05:44:54 [notice] 45#45: signal process started

发现pod重启

kubectl get pods  -w
>
NAME                          READY   STATUS    RESTARTS      AGE
first                         1/1     Running   0             158m
liveness-http                 1/1     Running   1 (25m ago)   31m
liveness-tcp                  1/1     Running   0             57s
nginx-test-6cf9d87fbf-26h6m   1/1     Running   0             153m
nginx-test-6cf9d87fbf-wn94b   1/1     Running   0             7d17h
liveness-tcp                  0/1     Completed   0             2m
liveness-tcp                  1/1     Running     1 (1s ago)    2m1s
就绪探测

readiness probe探测容器是否可以正常接受请求,如果探测失败,k8s立即停止将新的流量转发到该容器。从SVC移除

先建立一个service,再建立一个nginx的pod,通过service来转发流量

[root@master yam_files]# cat readiness-svc.yaml
apiVersion: v1
kind: Service
metadata:name: readinessnamespace:
spec:selector:app: my-podports:- port: 80targetPort: 80[root@master yam_files]# cat ready-http.yaml 
apiVersion: v1
kind: Pod
metadata:name: my-podlabels:app: my-podspec:containers:- name: nginx-containerimage: nginximagePullPolicy: IfNotPresentports:- containerPort: 80readinessProbe:httpGet:path: /index.htmlport: 80initialDelaySeconds: 30periodSeconds: 10failureThreshold: 2successThreshold: 1

可以看到设置的就绪探测为30s之后, 且每10s探测一次

一开始的service并没有连接到pod(endpoints为空)

kubectl describe svc readiness
>
Name:              readiness
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=my-pod
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.107.242.111
IPs:               10.107.242.111
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         
Session Affinity:  None
Events:            <none>

随后就绪探测成功,service也成功连上

kubectl get pods -w
>
NAME                          READY   STATUS    RESTARTS   AGE
nginx-test-6cf9d87fbf-26h6m   1/1     Running   0          3h28m
nginx-test-6cf9d87fbf-wn94b   1/1     Running   0          7d18h
my-pod                        0/1     Pending   0          0s
my-pod                        0/1     Pending   0          0s
my-pod                        0/1     ContainerCreating   0          0s
my-pod                        0/1     ContainerCreating   0          2s
my-pod                        0/1     Running             0          3s
my-pod                        1/1     Running             0          40skubectl describe svc readiness
>
Name:              readiness
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=my-pod
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.107.242.111
IPs:               10.107.242.111
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.166.145:80
Session Affinity:  None
Events:            <none>

 查看my-pod的ip,10.244.166.145,和上面svc的endpoint相同。在生产环境中,一般会部署副本,那么所有pod的ip都会在endpoint中,而这些ip也会写在防火墙中

kubectl get pods -owide
NAME                          READY   STATUS    RESTARTS   AGE     IP               NODE    NOMINATED NODE   READINESS GATES
my-pod                        1/1     Running   0          14m     10.244.166.145   node1   <none>           <none>
启动探测

startup probe用于监测容器是否成功启动并准备好接收流量,只会在启动的时候执行一次。

设置initialDelaySeconds:启动多久之后开始探测,避免容器初始化没完成就开始探测

periodSeconds:探测周期。默认10s

timeoutSeconds:默认1s

如果没有设置容器启动探测,则默认状态为成功success

apiVersion: v1
kind: Pod
metadata:name: startupprobe
spec:containers:- name: startupimage: xianchao/tomcat-8.5-jre8:v1imagePullPolicy: IfNotPresentports:- containerPort: 8080startupProbe:exec:command:- "/bin/bash"- "-c"- " ps aux | grep tomcat"initialDelaySeconds: 20 #容器启动后多久开始探测periodSeconds: 20 #执行探测的时间间隔timeoutSeconds: 10 #探针执行检测请求后,等待响应的超时时间successThreshold: 1 #成功多少次才算成功failureThreshold: 3 #失败多少次才算失败

 用exe做探针,执行后发现40s之后运行成功,探测到的时间=initialDelaySeconds + periodSeconds,第一次initialSelaySeconds没有探测成功

kubectl get pods -w
>
NAME                          READY   STATUS    RESTARTS   AGE
nginx-test-6cf9d87fbf-26h6m   1/1     Running   0          11h
nginx-test-6cf9d87fbf-wn94b   1/1     Running   0          8d
startupprobe                  0/1     Running   0          23s
startupprobe                  0/1     Running   0          40s
startupprobe                  1/1     Running   0          40s

修改yaml文件,使得探测失败,可以看到开始重启

“aa ps aux | grep tomcat”

第一次重启的时间:initialDelaySeconds + (periodSeconds + timeoutSeconds)* failureThreshold

kubectl get pods -w
>
NAME                          READY   STATUS    RESTARTS   AGE
startupprobe                  0/1     ContainerCreating   0          0s
startupprobe                  0/1     ContainerCreating   0          1s
startupprobe                  0/1     Running             0          2s
startupprobe                  0/1     Running             1 (0s ago)   80s
startupprobe                  0/1     Running             2 (1s ago)   2m21s

利用tcpSocket进行探测

apiVersion: v1
kind: Pod
metadata:name: startupprobe
spec:containers:- name: startupimage: xianchao/tomcat-8.5-jre8:v1imagePullPolicy: IfNotPresentports:- containerPort: 8080startupProbe:tcpSocket:port: 8080initialDelaySeconds: 20 #容器启动后多久开始探测periodSeconds: 20 #执行探测的时间间隔timeoutSeconds: 10 #探针执行检测请求后,等待响应的超时时间successThreshold: 1 #成功多少次才算成功failureThreshold: 3 #失败多少次才算失败
全生命周期健康监测

目前livenessProbe、ReadinessProbe和startupProbe都支持以下三种探针:

exec:如果执行成功,退出码为0则探测成功。

TCPSocketAction:通过容器的ip地址和端口号执行TCP检查,如果能够建立TCP连接,则表明容器健康。

HTTPGetAction:通过容器的ip地址、端口号以及调用HTTP Get方法,如果响应的状态码大于200小于400,则认为容器健康。

apiVersion: v1
kind: Pod
metadata:name: life-demospec:containers:- name: lifecycle-demo-containerimage: docker.io/xianchao/nginx:v1imagePullPolicy: IfNotPresentlifecycle:postStart:exec:command: ["/bin/bash","-c","echo 'lifecycle hookshandler' > /usr/share/nginx/html/test.html"]preStop:exec:command:- "/bin/sh"- "-c"- "nginx -s stop"

postStart和preStop都是容器生命管理的钩子,PostStart 钩子在容器启动后立即调用。这是在容器的主进程启动之后,且在容器被视为就绪之前发生的。如果 PostStart 钩子执行失败,容器将被视为启动失败,Kubernetes 会根据容器的重启策略处理这个失败的容器。PreStop 钩子是为了在容器终止前执行清理任务(如关闭连接、释放资源等)而设计的。

apiVersion: v1
kind: Pod
metadata:name: checknamespace: defaultlabels:app: check
spec:containers:- name: checkimage: busybox:1.28imagePullPolicy: IfNotPresentcommand:- /bin/sh- -c- sleep 10;exit

启动这个pod后,10s后容器就会退出,但是会一直重启

kubectl get pods -w
>
NAME                          READY   STATUS    RESTARTS   AGE
nginx-test-6cf9d87fbf-26h6m   1/1     Running   0          11h
nginx-test-6cf9d87fbf-wn94b   1/1     Running   0          8d
check                         0/1     Pending   0          0s
check                         0/1     Pending   0          0s
check                         0/1     ContainerCreating   0          0s
check                         0/1     ContainerCreating   0          1s
check                         1/1     Running             0          2s
check                         0/1     Completed           0          12s
check                         1/1     Running             1 (2s ago)   13s
check                         0/1     Completed           1 (12s ago)   23s
check                         0/1     CrashLoopBackOff    1 (14s ago)   36s
check                         1/1     Running             2 (14s ago)   36s
check                         0/1     Completed           2 (24s ago)   46s
check                         0/1     CrashLoopBackOff    2 (12s ago)   58s
check                         1/1     Running             3 (24s ago)   70s
check                         0/1     Completed           3 (34s ago)   80s
check                         0/1     CrashLoopBackOff    3 (16s ago)   95s
check                         1/1     Running             4 (43s ago)   2m2s

一个包含三种探测的pod文件

apiVersion: v1
kind: Service
metadata:name: springboot-livelabels:app: springbootspec:type: NodePortports:- name: serverport: 8080targetPort: 8080nodePort: 31180- name: managementport: 8081targetPort: 8081nodePort: 31181selector:app: springboot---
apiVersion: v1
kind: Pod
metadata:name: springboot-livelabels:app: springbootspec:containers:- name: springbootimage: mydlqclub/springboot-helloworld:0.0.1imagePullPolicy: IfNotPresentports:- name: servercontainerPort: 8080- name: managementcontainerPort: 8081readinessProbe:initialDelaySeconds: 20periodSeconds: 5timeoutSeconds: 10httpGet:scheme: HTTPport: 8081path: /actuator/healthlivenessProbe:initialDelaySeconds: 20periodSeconds: 5timeoutSeconds: 10httpGet:scheme: HTTPport: 8081path: /actuator/healthstartupProbe:initialDelaySeconds: 20periodSeconds: 5timeoutSeconds: 10httpGet:scheme: HTTPport: 8081path: /actuator/health

启动后发现svc的服务是正常的

kubectl describe svc springboot-live
Name:                     springboot-live
Namespace:                default
Labels:                   app=springboot
Annotations:              <none>
Selector:                 app=springboot
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.98.119.36
IPs:                      10.98.119.36
Port:                     server  8080/TCP
TargetPort:               8080/TCP
NodePort:                 server  31180/TCP
Endpoints:                10.244.166.156:8080
Port:                     management  8081/TCP
TargetPort:               8081/TCP
NodePort:                 management  31181/TCP
Endpoints:                10.244.166.156:8081
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

随后进入pod,删掉运行的程序

kubectl exec -it springboot-live -- /bin/sh
/ # ls
app.jar  dev      home     media    opt      root     sbin     sys      usr
bin      etc      lib      mnt      proc     run      srv      tmp      var
/ # ps -ef | grep springboot63 root      0:00 grep springboot
/ # ps -ef | grep hello65 root      0:00 grep hello
/ # kill  1
/ # command terminated with exit code 137

查看pod状态,发现20s后重启

kubectl get pods -w
NAME                          READY   STATUS    RESTARTS   AGE
springboot-live               1/1     Running   0          2m36s
springboot-live               0/1     Error     0          17m
springboot-live               0/1     Running   1 (1s ago)   17m
springboot-live               0/1     Running   1 (22s ago)   17m
springboot-live               1/1     Running   1 (22s ago)   17m

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/37886.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Win测试】窗口捕获的学习笔记

2 辨析笔记 2.1 mss&#xff1a;捕获屏幕可见区域&#xff0c;不适合捕获后台应用 Claude-3.5-Sonnet: MSS库可以用来捕获屏幕上可见的内容&#xff1b;然而&#xff0c;如果游戏窗口被其他窗口完全遮挡或最小化&#xff0c;MSS将无法捕获到被遮挡的游戏窗口内容&#xff0c;而…

天津惠灵顿:从心,致逐梦康桥|在这所天津国际学校从容不迫中走近梦想

在刚刚落下帷幕的申请季中&#xff0c;来自惠灵顿天津校区的Herman&#xff0c;陆续收到了剑桥大学、帝国理工学院、纽约大学、瓦萨学院等10余封录取通知书。面对纷至沓来的名校肯定&#xff0c;经历了短暂的尘埃落定的喜悦&#xff0c;Herman很快恢复了往日里的泰然自若。在他…

cv::Mat类的矩阵内容输出的各种格式的例子

操作系统&#xff1a;ubuntu22.04OpenCV版本&#xff1a;OpenCV4.9IDE:Visual Studio Code编程语言&#xff1a;C11 功能描述 我们可以这样使用&#xff1a;cv::Mat M(…); cout << M;&#xff0c;直接将矩阵内容输出到控制台。 输出格式支持多种风格&#xff0c;包括O…

第5章:Electron加载与显示内容(2)

5.4 加载和显示不同类型的资源 Electron 支持加载和显示多种类型的资源&#xff0c;包括图片、视频和其他静态文件。 5.4.1 加载图片的示例代码 index.html&#xff1a; <!DOCTYPE html> <html> <head><title>Load Image</title> </head&…

字符串常量池StringTable

String s1 "a"; 从常量池中取符号a->运行时常量池 ->"a"放入字符串常量池 -> 给s1 String s2 "b"; String s3 s1s2; 创建 new StringBuilder().append("a").append("b").toString() String s4 "a"&q…

鸿蒙使用 @Builder扩展出来的布局数据更新没法更新UI

由于业务的复杂&#xff0c;所以我们把相关UI抽离出来。但是数据变化了&#xff0c;没法更新UI Builder MyGridLayout() { } 通过日志打印发现数据的确是更新了&#xff0c;但是UI就没没办法&#xff0c;如何解决呢 Entry Component struct Page35 {// State sArray: bool…

【ajax实战09】内容管理页面——删除功能

本文章目标&#xff1a;点击删除图标实现对应数据删除 实现步骤如下&#xff1a; 一&#xff1a;将服务器端获取数据中数据id值绑定到删除图标&#xff08;重点&#xff09; 即在渲染时&#xff0c;利用自定义属性&#xff0c;为td设置id值 <td data-id "${ele.id}…

CEPH client.admin key获取

通过初始化完毕后&#xff0c;admin节点会在/etc/ceph目标下生成对应的配置文件和对应的key文件&#xff0c;通过ceph orch host add 增加的默认是没有的 如果很不幸admin节点挂了&#xff0c;怎么在其它节点使用ceph -s 命令呢 启蒙方法(比较实用) key可以通过ceph auth expor…

chunkers/maxent_ne_chunker/english_ace_multiclass.pickle 找不到

首先在这个nltk_data &#xff1a; NLTK Data官方下的数据集&#xff0c;找不到english_ace_multiclass.pic 说明缺少这个文件 : 那么在 nlp/resources/chunkers/maxent_ne_chunker/english_ace_multiclass.pickle at master teropa/nlp (github.com) 下载那两个文件 : 然…

在Vue3项目中引入Vite进行热更新

第一步&#xff1a;初始化一个Vue3项目&#xff0c;可以使用Vue CLI 在开始之前&#xff0c;我们需要确保已经安装了Vue CLI。可以通过以下命令安装Vue CLI&#xff1a; bash npm install -g vue/cli 接下来&#xff0c;使用Vue CLI初始化一个Vue3项目&#xff1a; bash vue …

基于SpringBoot的CSGO赛事管理系统

您好&#xff01;我是专注于计算机技术研究的码农小野。如果您对CSGO赛事管理系统感兴趣或有相关开发需求&#xff0c;欢迎随时联系我。 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;SpringBoot框架&#xff0c;Java技术 工具&#xff1a;Eclipse&a…

迈阿密色主题学科 HTML5静态导航源码

源码介绍 迈阿密色主题学科 HTML5静态导航源码&#xff0c;源码直接上传可用&#xff0c;有技术的可以拿去写个后端搜索调用百度接口&#xff0c;也可用于做引导页下面加你网址添加一个A标签就行了&#xff0c;很简单&#xff0c;需要的朋友就拿去吧 界面预览 源码下载 迈阿…

安装Anaconda + tensorflow

安装Anaconda tensorflow 下载Anaconda&#xff08;64位&#xff09; https://www.anaconda.com/download/ Anaconda3-xxxxxx-Windows-x86_64&#xff08;不要装最新的版本&#xff0c;确保Python是3.7&#xff09; 各种Anaconda老版本&#xff1a; https://mirrors.tuna.ts…

跳转的艺术:Batch文件中GOTO命令的深度解析

跳转的艺术&#xff1a;Batch文件中GOTO命令的深度解析 在批处理文件&#xff08;Batch&#xff09;的编程世界中&#xff0c;GOTO命令是实现流程控制的重要工具之一。它允许程序跳转到脚本中的特定标签位置&#xff0c;从而实现循环、条件分支等复杂的逻辑结构。本文将深入探…

EtherCAT主站IGH-- 4 -- IGH之datagram_pair.h/c文件解析

EtherCAT主站IGH-- 4 -- IGH之datagram_pair.h/c文件解析 0 预览一 该文件功能datagram_pair.c 文件功能函数预览 二 函数功能介绍datagram_pair.c 中主要函数的作用1. ec_datagram_pair_init2. ec_datagram_pair_clear3. ec_datagram_pair_process 三 h文件翻译四 c文件翻译该…

专题五:Spring源码之初始化容器上下文

上一篇我们通过如下一段基础代码作为切入点&#xff0c;最终找到核心的处理是refresh方法&#xff0c;从今天开始正式进入refresh方法的解读。 public class Main {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(…

鸿蒙本地签名不匹配问题

连接鸿蒙手机运行项目报如下错误 这是由于本地签名和鸿蒙设备签名不匹配导致的&#xff0c;需要注释掉如下代码&#xff0c;选择file project 自动签名 勾选auto选项&#xff0c;会在build-profile.json5中生成一个签名&#xff0c;然后运行就ok了~

【Lua】脚本入门

文章目录 总述一、Lua概述二、Lua环境安装三、Lua基本语法四、Lua的库和扩展五、Lua的应用场景六、学习资源 语法1. Lua基本语法示例变量和数据类型控制结构函数 2. Lua标准库示例字符串操作数学函数文件I/O 3. Lua作为脚本扩展示例&#xff08;假设Lua嵌入在某个应用程序中&am…

vscode python格式化

插件 Black Formatter Black 默认会遵循 PEP 8 的规范&#xff0c;可配置的参数很少&#xff0c;用的人很多。 setting.json 配置&#xff0c;更改插件的每行字符数限制 {"[python]": {"editor.defaultFormatter": "ms-python.black-formatter"…

Redis命令大全(基础版)

一、基础命令 redis-server --service-start # 开启服务 redis-server --service-stop # 停止服务redis-cli # 进入redis界面redis界面操作&#xff1a; ping # 检测状态&#xff0c;返回pong证明连接正常set key value # 设置 key 字段的值为value&#xff0c;返回o…