探针

在我们之前提到过的deploy控制器里面,他是对pod的状态进行管理,只有当pod的状态不是running的时候他才会重构pod,但是如果我们启动了一个nginx的pod,但是这个pod的index文件被删除掉了,此时pod的状态依旧是running,但是用户无法拿到正常的页面,这种情况下控制器是做不了管控的,对于这种情况,我们可以来使用探针

liveness-probe 存活探针

这个探针的作用是保证pod的正常运行,注意,是正常运行而不是运行,这个就是他与deploy控制器最大的区别

我们可以使用这个探针来检测nginx容器的index.html文件是否存在,如果存在的话那么这个容器就是正常在运行的,如果不存在那么就是我们开头就提到过的那种情况,控制器看他的状态是running,但是他没有index文件,探针负责每隔一段时间就检查这个文件是否存在,如果存在就不做操作,如果不存在的话那么kebelet就会干掉这个容器并重新启动一个新的容器

探针的3种方式

探针有3种检测方式,分别是exec,httpGet和tcpSocket,这三种方式的定义方法都大差不差

1. exec

这种方式就是通过事先定义好的命令去检测,如果命令执行的结果为0那么就是正常的,如果命令的结果是非0的任何值,那么说明命令执行失败了,容器没有正常运行,需要重构容器

apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
# 存活探针
livenessProbe:
# 执行的操作
exec:
# 使用test -f 命令来检测文件是否存在
command: ["test","-f","/usr/share/nginx/html/index.html" ]
# 在执行第一次检测之前需要等待多少秒,这里是2
initialDelaySeconds: 2
# 每隔多少秒触发一次检测,这里也是2
periodSeconds: 2
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}

我们写好的yaml文件是检测index.html文件是否存在,那么我们现在来将他创建出来,然后删除index.html文件看看他会发生什么

[root@master k8s]# kubectl apply -f liveness-probe.yaml
pod/nginx created
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 3s

他现在是正常运行的,并且重启次数是0,那么我们使用命令删除掉他的index.html文件

[root@master k8s]# kubectl exec -it nginx -- rm -f /usr/share/nginx/html/index.html
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 60s
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 62s
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 1 (1s ago) 63s

我们可以看到,在我们执行完删除命令之后,过了几秒这个容器的重启次数就是1了,然后我们再来看看index.html文件是否回来了

[root@master k8s]# kubectl exec -it nginx -- ls -l /usr/share/nginx/html/index.html
-rw-r--r-- 1 root root 615 Oct 24 13:46 /usr/share/nginx/html/index.html

我们可以看到,这个文件确实重新回来了,为什么他会重新回来呢?

我们之前说过,只要存活探针检测到这个文件不存在了,那么他就会直接使用之前的镜像来创建一个新的pod

所以这个文件就又有了

2. httpGet

apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
livenessProbe:
# 只改动了这个地方
httpGet:
# http请求的地址,端口
path: /index.html
port: 80
initialDelaySeconds: 2
periodSeconds: 2
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}

通过上面的yaml文件我们可以很清楚的知道,我们需要请求的是这个网站的根目录下的index.html,注意,是网站的根目录,不是linux的根目录,注意区分开

[root@master k8s]# kubectl apply -f liveness-probe.yaml
pod/nginx created
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 3s

我们还是去删除掉他的index.html文件

[root@master k8s]# kubectl exec -it nginx -- rm -f /usr/share/nginx/html/index.html
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 47s
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 48s
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 1 (0s ago) 49s

我们可以看到,当我们删除掉这个文件之后,他请求不到index.html文件了,所以这个容器会重启

3. tcpSocket

这个方式就更加简单了,探针会对你定义的端口发起tcp连接,如果可以不能正常进行三次握手,那么就会重启容器

apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
livenessProbe:
tcpSocket:
port: 81
initialDelaySeconds: 2
periodSeconds: 2
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}

我们直接将80端口改为81端口,nginx使用的是80端口提供服务,我们现在让他去尝试连接81端口肯定是连接不上的,我们来看看效果

[root@master k8s]# kubectl apply -f liveness-probe.yaml
pod/nginx created
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 8s
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 1 (1s ago) 9s

这种方式比较简单,就是直接对你指定的端口发起tcp连接,能连上就代表正常,连不上那就重启

这就是探针的3种检测方式了,接下来看就绪探针

readness-probe 就绪探针

这种探针跟liveness探针的区别就是,liveness会重启容器,这个探针不会,这个探针就是说,如果你的deployment的副本数为3,我这个探针依旧是检测你的index.html文件,如果这个文件不存在的话,那么我就不会将流量转发到这个pod上了,并不会重启容器,我们来看看

这个探针依旧是那3种方式

1. exec

我们这里使用deployment的方式来操作,看的更加直观一点

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: deploy1
name: deploy1
spec:
replicas: 3
selector:
matchLabels:
app: deploy1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: deploy1
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
readinessProbe:
exec:
command: ["test", "-f", "/usr/share/nginx/html/index.html"]
initialDelaySeconds: 2
periodSeconds: 2
status: {}

我们接下来的操作就是,创建这个控制器,然后进入到每个容器里面修改他的index.html文件的内容,最后再去删除某个容器的index.html文件,尝试访问

# 我们先将这个控制器创建出来
[root@master k8s]# kubectl apply -f deploy1.yaml
deployment.apps/deploy1 created
[root@master k8s]# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deploy1-869c888fcd-nkqcr 1/1 Running 0 7s 10.244.104.9 node2 <none> <none>
deploy1-869c888fcd-qpcj6 1/1 Running 0 7s 10.244.104.12 node2 <none> <none>
deploy1-869c888fcd-s9gjk 1/1 Running 0 7s 10.244.166.149 node1 <none> <none>
# 然后我们将服务暴露出去
[root@master k8s]# kubectl expose deployment deploy1 --port=80 --target-port=80 --type=NodePort
service/deploy1 exposed
[root@master k8s]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
deploy1 NodePort 10.106.239.238 <none> 80:31268/TCP 33s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 30d
# 我们将每个容器的index文件改掉
[root@master k8s]# kubectl exec -it deploy1-869c888fcd-nkqcr -- bash
root@deploy1-869c888fcd-nkqcr:/# echo host01 > /usr/share/nginx/html/index.html
[root@master k8s]# kubectl exec -it deploy1-869c888fcd-qpcj6 -- bash
root@deploy1-869c888fcd-qpcj6:/# echo host02 > /usr/share/nginx/html/index.html
[root@master k8s]# kubectl exec -it deploy1-869c888fcd-s9gjk -- bash
root@deploy1-869c888fcd-s9gjk:/# echo host03 > /usr/share/nginx/html/index.html
root@deploy1-869c888fcd-s9gjk:/# exit
# 然后我们通过svc暴露出来的端口来访问
[root@master k8s]# curl localhost:31268
host03
[root@master k8s]# curl localhost:31268
host02
[root@master k8s]# curl localhost:31268
host01
# 我们可以发现他会将流量转发到每一个pod上,此时我们来删除第一个容器的index.html文件
[root@master k8s]# kubectl exec -it deploy1-869c888fcd-nkqcr -- rm -f /usr/share/nginx/html/index.html
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deploy1-869c888fcd-nkqcr 0/1 Running 0 9m42s
deploy1-869c888fcd-qpcj6 1/1 Running 0 9m42s
deploy1-869c888fcd-s9gjk 1/1 Running 0 9m42s

这个时候我们看到第一个pod的read状态是0/1了,我们再来尝试访问

[root@master k8s]# curl localhost:31268
host02
[root@master k8s]# curl localhost:31268
host02
[root@master k8s]# curl localhost:31268
host02
[root@master k8s]# curl localhost:31268
host03

可以看到我们现在无论怎么访问他都不会将流量转发到这个pod上了

2. httpGet

这种方式的定义跟之前的定义方式一样

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: deploy1
name: deploy1
spec:
replicas: 3
selector:
matchLabels:
app: deploy1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: deploy1
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
readinessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 2
periodSeconds: 2
status: {}
[root@master k8s]# kubectl apply -f deploy1.yaml
deployment.apps/deploy1 created
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deploy1-6757fdb6d9-dl7fc 1/1 Running 0 3s
deploy1-6757fdb6d9-mrttl 1/1 Running 0 3s
deploy1-6757fdb6d9-zkj5b 1/1 Running 0 3s

我们还是同样删除一个容器里的index.html,其他效果就不演示了,只看他的状态就知道了

[root@master k8s]# kubectl exec -it deploy1-6757fdb6d9-dl7fc -- rm -f /usr/share/nginx/html/index.html
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deploy1-6757fdb6d9-dl7fc 1/1 Running 0 78s
deploy1-6757fdb6d9-mrttl 1/1 Running 0 78s
deploy1-6757fdb6d9-zkj5b 1/1 Running 0 78s
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deploy1-6757fdb6d9-dl7fc 0/1 Running 0 79s
deploy1-6757fdb6d9-mrttl 1/1 Running 0 79s
deploy1-6757fdb6d9-zkj5b 1/1 Running 0 79s

可以看得到,ready那里已经变成0/1了也就是他不会在有流量转发到他这里了

3. tcpSocket

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: deploy1
name: deploy1
spec:
replicas: 3
selector:
matchLabels:
app: deploy1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: deploy1
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 2
periodSeconds: 2
status: {}

也是这样定义的,这个我就不演示了,效果跟前俩是一样的,tcp连接不上的话ready就会变成0

综述:虽然我这里全部都是使用nginx镜像,但是我们一般会使用exec来检测配置文件是否存在,使用httpGet来检测一些网站类的容器,使用tcpSocket来对开放了端口的容器进行检测

liveness-probe探针和readness-probe的更多相关文章

  1. FPGA editor 的使用之一 ------ Probe探针

    做FPGA设计经常会用到FPGA editor工具,今天开始总结下使用FPGA editor工具的经验. 1.添加probes 在debug时,经常要分析设计中的某一个信号的状态变化,需要观测这个信号 ...

  2. linux中probe函数中传递的参数来源(上)

    点击打开链接 上一篇中,我们追踪了probe函数在何时调用,知道了满足什么条件会调用probe函数,但probe函数中传递的参数我们并不知道在何时定义,到底是谁定义的,反正不是我们在驱动中定义的(当然 ...

  3. hostapd修改beacon帧和probe response帧

    在AP模式下,热点会不断定期地发送Beacon帧来宣告自己存在,告知设备可以加入网络: Probe Response帧是用于应答Probe Request帧,Probe Request帧是移动工作站用 ...

  4. Psi Probe 安装及使用说明

    这是一款 Tomcat 管理和监控工具,前身是 Lambda Probe.由于 Lambda Probe 2006不再更新,所以 PSI Probe 算是对其的一个 Fork 版本并一直更新至今. g ...

  5. platform_device和platform_driver的注册过程,及probe函数何时调用的分析 ⭐⭐⭐

    add  platform_device之后,需要注意的一个地方是这里,add是通过系统初始化里边调用platform_add_devices把所有放置在板级platform_device数组中的所有 ...

  6. Kubernetes Pod中容器的Liveness、Readiness和Startup探针

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 探针的作用 在Kubernetes的容器生命周期管理中,有三种探针,首先要知道,这探针是属于容器的,而不是Pod: 存 ...

  7. 详解k8s中的liveness和readiness的原理和区别

    liveness与readiness的探针工作方式源码解析 liveness和readiness作为k8s的探针,可以对应用进行健康探测. 二者支持的探测方式相同.主要的探测方式支持http探测,执行 ...

  8. WIFI探针技术

    1.WIFI 探针定义 WIFI 探针是一种能够主动识别 Android 和 IOS 设备,感知用户行为轨迹的精准数据收集前端,基于 WIFI探测技术.移动互联网和云计算等先进技术自动识别探针附近的智 ...

  9. Kubernetes之POD

    什么是Pod Pod是可以创建和管理Kubernetes计算的最小可部署单元.一个Pod代表着集群中运行的一个进程. Pod就像是豌豆荚一样,它由一个或者多个容器组成(例如Docker容器),它们共享 ...

  10. Kubernetes学习之路(十一)之Pod状态和生命周期管理

    一.什么是Pod? Pod是kubernetes中你可以创建和部署的最小也是最简的单位.一个Pod代表着集群中运行的一个进程. Pod中封装着应用的容器(有的情况下是好几个容器),存储.独立的网络IP ...

随机推荐

  1. node-sass安装失败,安装后无法使用 gyp verb check python checking for Python executable "python2" in the PATH

    这个问题搞了一会想起开始安装node-sass时的一句被我忽略的提示:执行 npm rebuild node-sass 这行后就可以了. 再说说 node-sass 的安装问题,现在使用 yarn i ...

  2. python中BeautifulSoup库使用小结

    转载请注明出处: BeautifulSoup是一个用于解析HTML和XML文档的Python库,它提供了一些简单但强大的API,让你可以从文档中提取数据.以下是一些BeautifulSoup的主要特性 ...

  3. Git-基本介绍

  4. Mygin实现简单的路由

    本文是Mygin第二篇 目的: 实现路由映射 提供了用户注册静态路由方法(GET.POST方法) 基于上一篇 Mygin 实现简单Http 且参照Gin 我使用了map数组实现简单路由的映射关系 不同 ...

  5. [转帖]离线部署单机kubenetes-1.28.4

    系统版本: openEuler 22.03 (LTS-SP2) docker版本:24.0.7 kubenetes版本: 1.28.4 虚机IP: 192.168.177.138 基于 https:/ ...

  6. [转帖]TIDB-TIDB节点磁盘已满报警

    一.背景 今日突然收到tidb节点的磁盘报警,磁盘容量已经超过了80%,但是tidb是不放数据的,磁盘怎么会满,这里就需要排查了 二.问题排查 解决步骤 1.df -h查看哪里占用磁盘比较多,然后通过 ...

  7. [转帖]Megacli 错误码

    MegaCLI Error Messages 0x00 Command completed successfully 0x01 Invalid command 0x02 DCMD opcode is ...

  8. [转帖]Linux Shell:date日期时间操作

    https://www.jianshu.com/p/cc9ebb212a8e 整理Linux Shell脚本中常用的日期操作,给予date命令,主要用法总结 获得当前日期,时间戳,date,date ...

  9. [转帖]HTTP与HTTPS超文本传输协议的区别是什么

    https://www.likecs.com/show-308649882.html 随着越来越多的网站使用HTTPS加密,现在HTTPS的使用已经成了硬性要求了.虽然说https是http的安全版, ...

  10. [转帖]多CPU && 多核CPU | 多进程 && 多线程 | 并行 && 并发

    https://cloud.tencent.com/developer/article/1886157?areaSource=&traceId=   文章目录 区分 多CPU &&am ...