kubelet 探针

目录

1 k8s中kubelet 探针的介绍

1.1 探针是由 kubelet 对容器执行的定期诊断:

1.2 Kubelet 可以选择是否执行在容器上运行的三种探针执行和做出反应:

1.3 ReadinessProbe 与 LivenessProbe 的区别

1.4 StartupProbe 与 ReadinessProbe、LivenessProbe 的区别

2 实施探针的实例

2.1 LivenessProbe 探针

2.1.1 创建 Pod 的yml文件

2.1.2 启动容器观察容器在探测之后是否被干掉

2.1.3 将端口修改为web服务器默认的端口号

2.2 ReadinessProbe 探针

2.2.1 创建 Pod 的yml 文件

2.2.2 启动并查看效果

2.2.3 添加探针相应规则

2.2.4 查看规则匹配实现效果 


1 k8s中kubelet 探针的介绍

1.1 探针是由 kubelet 对容器执行的定期诊断:

  • ExecAction:在容器内执行指定命令。如果命令退出时返回码为 0 则认为诊断成功。

  • TCPSocketAction:对指定端口上的容器的 IP 地址进行 TCP 检查。如果端口打开,则诊断被认为是成功的。

  • HTTPGetAction:对指定的端口和路径上的容器的 IP 地址执行 HTTP Get 请求。如果响应的状态码大于等于200 且小于 400,则诊断被认为是成功的。

每次探测都将获得以下三种结果之一:

  • 成功:容器通过了诊断。

  • 失败:容器未通过诊断。

  • 未知:诊断失败,因此不会采取任何行动。

1.2 Kubelet 可以选择是否执行在容器上运行的三种探针执行和做出反应:

  • livenessProbe:指示容器是否正在运行。如果存活探测失败,则 kubelet 会杀死容器,并且容器将受到其 重启策略 的影响。如果容器不提供存活探针,则默认状态为 Success。

  • readinessProbe:指示容器是否准备好服务请求。如果就绪探测失败,端点控制器将从与 Pod 匹配的所有 Service 的端点中删除该 Pod 的 IP 地址。初始延迟之前的就绪状态默认为 Failure。如果容器不提供就绪探针,则默认状态为 Success。

  • startupProbe: 指示容器中的应用是否已经启动。如果提供了启动探测(startup probe),则禁用所有其他探测,直到它成功为止。如果启动探测失败,kubelet 将杀死容器,容器服从其重启策略进行重启。如果容器没有提供启动探测,则默认状态为成功Success。

1.3 ReadinessProbe 与 LivenessProbe 的区别

  • ReadinessProbe 当检测失败后,将 Pod 的 IP:Port 从对应的 EndPoint 列表中删除

  • LivenessProbe 当检测失败后,将杀死容器并根据 Pod 的重启策略来决定作出对应的措施

1.4 StartupProbe 与 ReadinessProbe、LivenessProbe 的区别

  • 如果三个探针同时存在,先执行 StartupProbe 探针,其他两个探针将会被暂时禁用,直到 pod 满足 StartupProbe 探针配置的条件,其他 2 个探针启动,如果不满足按照规则重启容器。

  • 另外两种探针在容器启动后,会按照配置,直到容器消亡才停止探测,而 StartupProbe 探针只是在容器启动后按照配置满足一次后,不在进行后续的探测。

2 实施探针的实例

2.1 LivenessProbe 探针

LivenessProbe 当检测失败后,将杀死容器并根据 Pod 的重启策略来决定作出对应的措施

2.1.1 创建 Pod 的yml文件

# 以yaml的格式输出一个模版并不做配置
[root@k8s-master yaml]# kubectl run shuyan --image myapp:v1 \
> --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: shuyan
  name: shuyan
spec:
  containers:
  - image: myapp:v1
    name: shuyan
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

# 将输出模版导入yml文件中
[root@k8s-master yaml]# kubectl run shuyan \
--image myapp:v1 --dry-run=client -o yaml > shuyan.yml

# 修改yml文件
[root@k8s-master yaml]# vim shuyan.yml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: shuyan
  name: shuyan
spec:
  containers:
  - image: myapp:v1
    name: shuyan

######################## 以上修改为 ######################

metadata:
  labels:
    run: shuyan
  name: shuyan
spec:
  containers:
  - image: myapp:v1
    name: shuyan
    livenessProbe:    # 检测失败直接杀死容器
      tcpSocket:      # 检测端口的存在性
        port: 8080    # 检测容器8080端口是否存在不存在就杀死
      initialDelaySeconds: 3    # 容器启动多少秒之后探针就开始工作
      periodSeconds: 1          # 这里为每隔一秒检测一次 
      timeoutSeconds: 1         # 探针执行探测的请求后,超时时间为1秒 ,默认时间为1秒  

2.1.2 启动容器观察容器在探测之后是否被干掉

启动发现到17秒的时候kubelet将这一个容器给杀死了 ,并根据 Pod 的重启策略来决定作出对应的措施

[root@k8s-master yaml]# kubectl apply -f shuyan.yml 
pod/shuyan created
[root@k8s-master yaml]# kubectl get pods 
NAME     READY   STATUS    RESTARTS   AGE
shuyan   1/1     Running   0          5s
[root@k8s-master yaml]# kubectl get pods 
NAME     READY   STATUS    RESTARTS   AGE
shuyan   1/1     Running   0          6s
[root@k8s-master yaml]# kubectl get pods 
NAME     READY   STATUS    RESTARTS     AGE
shuyan   1/1     Running   1 (2s ago)   8s
[root@k8s-master yaml]# kubectl get pods 
NAME     READY   STATUS    RESTARTS     AGE
shuyan   1/1     Running   1 (5s ago)   11s
[root@k8s-master yaml]# kubectl get pods 
NAME     READY   STATUS    RESTARTS     AGE
shuyan   1/1     Running   2 (2s ago)   13s
[root@k8s-master yaml]# kubectl get pods 
NAME     READY   STATUS    RESTARTS     AGE
shuyan   1/1     Running   2 (4s ago)   15s
[root@k8s-master yaml]# kubectl get pods 
NAME     READY   STATUS             RESTARTS     AGE
shuyan   0/1     CrashLoopBackOff   2 (1s ago)   17s


# 查看详细信息
[root@k8s-master yaml]# kubectl describe pods shuyan 
Events:
  Type     Reason     Age                 From               Message
  ----     ------     ----                ----               -------
  Normal   Scheduled  107s                default-scheduler  Successfully assigned default/shuyan to k8s-node2
  Normal   Started    97s (x3 over 107s)  kubelet            Started container shuyan
  Warning  Unhealthy  92s (x9 over 104s)  kubelet            Liveness probe failed: dial tcp 10.244.2.19:8080: connect: connection refused
  Normal   Killing    92s (x3 over 102s)  kubelet            Container shuyan failed liveness probe, will be restarted
  Warning  BackOff    91s (x2 over 92s)   kubelet            Back-off restarting failed container shuyan in pod shuyan_default(f7cb2433-0bad-45c8-a09c-681987c23e3a)
  Normal   Pulled     79s (x4 over 107s)  kubelet            Container image "myapp:v1" already present on machine
  Normal   Created    79s (x4 over 107s)  kubelet            Created container shuyan

2.1.3 将端口修改为web服务器默认的端口号

[root@k8s-master yaml]# vim shuyan.yml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: shuyan
  name: shuyan
spec:
  containers:
  - image: myapp:v1
    name: shuyan
    livenessProbe:
      tcpSocket:
        port: 80            # 将端口改回80
      initialDelaySeconds: 3
      periodSeconds: 1
      timeoutSeconds: 1

[root@k8s-master yaml]# kubectl delete pods shuyan 
pod "shuyan" deleted

[root@k8s-master yaml]# kubectl apply -f shuyan.yml 
pod/shuyan created

[root@k8s-master yaml]# kubectl get pods 
NAME     READY   STATUS    RESTARTS   AGE
shuyan   1/1     Running   0          8s


[root@k8s-master yaml]# kubectl describe pods shuyan
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  2m14s  default-scheduler  Successfully assigned default/shuyan to k8s-node1
  Normal  Pulled     2m14s  kubelet            Container image "myapp:v1" already present on machine
  Normal  Created    2m14s  kubelet            Created container shuyan
  Normal  Started    2m14s  kubelet            Started container shuyan

2.2 ReadinessProbe 探针

2.2.1 创建 Pod 的yml 文件

[root@k8s-master yaml]# kubectl run shuyan \
--image myapp:v1 --dry-run=client -o yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: shuyan
  name: shuyan
spec:
  containers:
  - image: myapp:v1
    name: shuyan
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

# 将文件模版导入
[root@k8s-master yaml]# kubectl run shuyan \
--image myapp:v1 --dry-run=client -o yaml > shuyan.yml

[root@k8s-master yaml]# vim shuyan.yml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: shuyan
  name: shuyan
spec:
  containers:
  - image: myapp:v1
    name: shuyan
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

# 修改为以下
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: shuyan
  name: shuyan
spec:
  containers:
  - image: myapp:v1
    name: shuyan
    readinessProbe:    # 就绪探针
      httpGet:
        path: /test.html   # 网站根目录下
        port: 80
      initialDelaySeconds: 1
      periodSeconds: 3 
      timeoutSeconds: 1

2.2.2 启动并查看效果

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

[root@k8s-master yaml]# kubectl describe service shuyan 
Name:              shuyan
Namespace:         default
Labels:            run=shuyan
Annotations:       <none>
Selector:          run=shuyan
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.111.0.138
IPs:               10.111.0.138
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:                                 # 没有暴露端口,就绪探测不满足条件
Session Affinity:  None
Events:            <none>

2.2.3 添加探针相应规则

[root@k8s-master yaml]# kubectl exec  pods/shuyan -c shuyan \
-- /bin/sh -c "echo test > /usr/share/nginx/html/test.html"

2.2.4 查看规则匹配实现效果 

[root@k8s-master yaml]# kubectl get pods 
NAME     READY   STATUS    RESTARTS   AGE
shuyan   1/1     Running   0          10m

[root@k8s-master yaml]# kubectl describe service shuyan 
Name:              shuyan
Namespace:         default
Labels:            run=shuyan
Annotations:       <none>
Selector:          run=shuyan
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.111.0.138          # 集群IP地址
IPs:               10.111.0.138
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.2.20:80        # 后端容器IP
Session Affinity:  None
Events:            <none>

满足条件暴露后端容器IP,这样的策略类似于keepalived配合LVS,
不匹配就会自动删除后端服务器策略
总结来说,这个服务 shuyan 是一个内部集群服务,
它将集群内部的流量通过 IP 地址 10.111.0.138 
和端口 80 转发到带有标签 run=shuyan 的 Pod 上。
当前的后端 Pod IP 地址是 10.244.2.20,端口为 80。


[root@k8s-master yaml]# kubectl get pods -o wide 
NAME     READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
shuyan   1/1     Running   0          10m   10.244.2.20   k8s-node2   <none>           <none>

[root@k8s-master yaml]# curl 10.244.2.20
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

妍妍的宝贝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值