Kubernetes的三种探针

Kubernetes 提供了探针 Probe 对容器的健康性进行检测。实际上我们不仅仅要对容器进行健康检测,还要对容器内布置的应用进行健康性检测。
编排工具运行时,虽说 pod 挂掉会在控制器的调度下会重启,出现 pod 重启的时候,但是 pod 状态是 running ,无法真实的反应当时 pod 健康状态,我们可以通过 Kubernetes 的探针监控到pod的实时状态。

Kubernetes三种探针类型

  1. 启动探针 Startup Probe :用于判断容器内应用程序是否启动,如果配置了startupProbe,就会先禁止其他的探针,直到它成功为止,成功后将不再进行探测。(1.16版本后加入,针对容器内应用服务是否已经启动监控)
  2. 就绪探针 Readiness Probe :判断容器是否已经就绪,若未就绪,容器将会处于未就绪,未就绪的容器,不会进行流量的调度。Kubernetes会把Podservice endpoints 中剔除。
  3. 存活探针 Liveness Probe :判断容器内的应用程序是否正常,若不正常,根据 PodrestartPolicy 重启策略操作,如果没有配置该探针,默认就是success。

探针的三种检查方法

  1. exec:通过在容器内执行指定命令,来判断命令退出时返回的状态码,返回状态码是0表示正常。
  2. httpGet:通过对容器的 IP 地址、端口和 URL 路径来发送 GET 请求;如果响应的状态码在 200 ~ 399 间,表示正常。
  3. tcpSocket:通过对容器的 IP 地址和指定端口,进行 TCP 检查,如果端口打开,发起 TCP Socket 建立成功,表示正常。

配置项

  • initialDelaySeconds:等待我们定义的时间 结束后便开始探针检查;
  • periodSeconds:探针的 间隔时间;
  • timeoutSeconds:探针的 超时时间,当超过我们定义的时间后,便会被视为失败;
  • successThreshold:探针的 最小连续成功数量;
  • failureThreshold:探针的 最小连续失败数量;

启动探针

[root@k8s-master01 ~]# cat pod.yaml | grep -v '^#'
apiVersion: v1        # 必选 API的版本号
kind: Pod             # 必选 类型Pod
metadata:             # 必选 元数据
  name: nginx         # 必选 符合RFC 1035规范的Pod名称
  #namespace: default # 可选 Pod所在的命名空间 不指定默认为default 可以使用-n指定namespace
  labels:             # 可选 标签选择器 一般用于过滤和区分Pod
    app: nginx-ready
spec:                  # 必选 用于定义容器的详细信息
  containers:          # 必选 容器列表                
  - name: nginx        # 必选 符合RFC 1035规范的容器名称
    image: nginx:latest   # 必选 容器所用的镜像的地址
    imagePullPolicy: Always   # 可选 镜像拉取策略 IfNotPresent:如果宿主机有这个镜像,就不用拉取了 Always:总是拉取 Never:不管存在不存在,都不拉取
    ports:             # 可选 容器需要暴露的端口号列表
    - name: http       # 端口名称
      containerPort: 80   # 端口号
      protocol: TCP    # 端口协议 默认TCP
    startupProbe:    # 可选 检测容器内进程是否完成启动 注意三种检查方式同时只能使用一种
      failureThreshold: 3 # 失败三次算探针失败
      exec: 
        command: ['/bin/sh','-c','echo Hello World']
      initialDelaySeconds: 3  # 容器启动完成后首次探测的时间,单位为秒
      timeoutSeconds: 2   # 对容器健康检查探测等待响应的超时时间,单位秒,默认1秒
      periodSeconds: 1    # 对容器监控检查的定期探测时间设置,单位秒,默认10秒一次
      successThreshold: 1 # 成功1次算探针OK
      failureThreshold: 3 # 失败三次算探针失败
  restartPolicy: Always # 可选 默认Always 容器故障或者没有启动成功 自动重启该容器 Onfailure: 容器以不为0的状态码终止 自动重启该容器 Never:无论何种状态 都不会重启

[root@k8s-master01 ~]# kubectl apply -f pod.yaml 
pod/nginx created
[root@k8s-master01 ~]# kubectl get pod
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          4s
[root@k8s-master01 ~]# kubectl describe pod nginx
[root@k8s-master01 ~]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          9m46s

就绪探针

[root@k8s-master01 ~]# kubectl delete pod nginx
pod "nginx" deleted
[root@k8s-master01 ~]# kubectl apply -f pod.yaml 
pod/nginx created
[root@k8s-master01 ~]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          8s

[root@k8s-master01 ~]# grep -v '^#' pod.yaml 
apiVersion: v1        # 必选 API的版本号
kind: Pod             # 必选 类型Pod
metadata:             # 必选 元数据
  name: nginx         # 必选 符合RFC 1035规范的Pod名称
  #namespace: default # 可选 Pod所在的命名空间 不指定默认为default 可以使用-n指定namespace
  labels:             # 可选 标签选择器 一般用于过滤和区分Pod
    app: nginx-ready
spec:                  # 必选 用于定义容器的详细信息
  containers:          # 必选 容器列表                
  - name: nginx        # 必选 符合RFC 1035规范的容器名称
    image: nginx:latest   # 必选 容器所用的镜像的地址
    imagePullPolicy: Always   # 可选 镜像拉取策略 IfNotPresent:如果宿主机有这个镜像,就不用拉取了 Always:总是拉取 Never:不管存在不存在,都不拉取
    ports:             # 可选 容器需要暴露的端口号列表
    - name: http       # 端口名称
      containerPort: 80   # 端口号
      protocol: TCP    # 端口协议 默认TCP
    readinessProbe:
      httpGet:  
        path: /
        port: 80  
      initialDelaySeconds: 3  # 容器启动完成后首次探测的时间,单位为秒
      timeoutSeconds: 2   # 对容器健康检查探测等待响应的超时时间,单位秒,默认1秒
      periodSeconds: 1    # 对容器监控检查的定期探测时间设置,单位秒,默认10秒一次
      successThreshold: 1 # 成功1次算探针OK
      failureThreshold: 3 # 失败三次算探针失败
  restartPolicy: Always # 可选 默认Always 容器故障或者没有启动成功 自动重启该容器 Onfailure: 容器以不为0的状态码终止 自动重启该容器 Never:无论何种状态 都不会重启

可以看到端口检测正常
添加nodeport类型进行流量访问验证

[root@k8s-master01 ~]# grep -v '^#' pod.yaml 
apiVersion: v1        # 必选 API的版本号
kind: Pod             # 必选 类型Pod
metadata:             # 必选 元数据
  name: nginx         # 必选 符合RFC 1035规范的Pod名称
  #namespace: default # 可选 Pod所在的命名空间 不指定默认为default 可以使用-n指定namespace
  labels:             # 可选 标签选择器 一般用于过滤和区分Pod
    app: nginx-ready
spec:                  # 必选 用于定义容器的详细信息
  containers:          # 必选 容器列表                
  - name: nginx        # 必选 符合RFC 1035规范的容器名称
    image: nginx:latest   # 必选 容器所用的镜像的地址
    imagePullPolicy: Always   # 可选 镜像拉取策略 IfNotPresent:如果宿主机有这个镜像,就不用拉取了 Always:总是拉取 Never:不管存在不存在,都不拉取
    ports:             # 可选 容器需要暴露的端口号列表
    - name: http       # 端口名称
      containerPort: 80   # 端口号
      protocol: TCP    # 端口协议 默认TCP
    readinessProbe:
      httpGet:  
        path: /
        port: 80  
      initialDelaySeconds: 3  # 容器启动完成后首次探测的时间,单位为秒
      timeoutSeconds: 2   # 对容器健康检查探测等待响应的超时时间,单位秒,默认1秒
      periodSeconds: 1    # 对容器监控检查的定期探测时间设置,单位秒,默认10秒一次
      successThreshold: 1 # 成功1次算探针OK
      failureThreshold: 3 # 失败三次算探针失败
  restartPolicy: Always # 可选 默认Always 容器故障或者没有启动成功 自动重启该容器 Onfailure: 容器以不为0的状态码终止 自动重启该容器 Never:无论何种状态 都不会重启
---
apiVersion: v1
kind: Service
metadata:
  name: ready-nodeport
  labels:
    name: ready-nodeport
spec:
  type: NodePort
  ports:
  - port: 88
    protocol: TCP
    targetPort: 80
    nodePort: 30880
  selector:
    app: nginx-ready

访问验证

[root@k8s-master01 ~]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          15s
[root@k8s-master01 ~]# kubectl get svc -owide
NAME             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes       ClusterIP   10.96.0.1      <none>        443/TCP        72d   <none>
ready-nodeport   NodePort    10.96.93.159   <none>        88:30880/TCP   20s   app=nginx-ready
[root@k8s-master01 ~]# curl http://192.168.10.10:30880
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

修改httpGet或tcpSocket端口为81模拟探针探测失败,探测失败流量会不会分配进入

[root@k8s-master01 ~]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   0/1     Running   0          22s
[root@k8s-master01 ~]# kubectl get svc -owide
NAME             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes       ClusterIP   10.96.0.1      <none>        443/TCP        72d   <none>
ready-nodeport   NodePort    10.96.115.11   <none>        88:30880/TCP   25s   app=nginx-ready
[root@k8s-master01 ~]# curl http://192.168.10.10:30880
curl: (7) Failed connect to 192.168.10.10:30880; 拒绝连接

describe nginx查看

显示81端口不通,ready状态为0,但pod是running的,请求结果为拒绝连接,流量打入失败。就绪探针失败,是不会给该pod打入流量的。

存活探针

[root@k8s-master01 ~]# grep -v '^#' pod.yaml 
apiVersion: v1        # 必选 API的版本号
kind: Pod             # 必选 类型Pod
metadata:             # 必选 元数据
  name: nginx         # 必选 符合RFC 1035规范的Pod名称
  #namespace: default # 可选 Pod所在的命名空间 不指定默认为default 可以使用-n指定namespace
  labels:             # 可选 标签选择器 一般用于过滤和区分Pod
    app: nginx-ready
spec:                  # 必选 用于定义容器的详细信息
  containers:          # 必选 容器列表                
  - name: nginx        # 必选 符合RFC 1035规范的容器名称
    image: nginx:latest   # 必选 容器所用的镜像的地址
    imagePullPolicy: Always   # 可选 镜像拉取策略 IfNotPresent:如果宿主机有这个镜像,就不用拉取了 Always:总是拉取 Never:不管存在不存在,都不拉取
    ports:             # 可选 容器需要暴露的端口号列表
    - name: http       # 端口名称
      containerPort: 80   # 端口号
      protocol: TCP    # 端口协议 默认TCP
    livenessProbe:
      httpGet:  
        path: /
        port: 80
        scheme: HTTP
      initialDelaySeconds: 3  # 容器启动完成后首次探测的时间,单位为秒
      timeoutSeconds: 2   # 对容器健康检查探测等待响应的超时时间,单位秒,默认1秒
      periodSeconds: 1    # 对容器监控检查的定期探测时间设置,单位秒,默认10秒一次
      successThreshold: 1 # 成功1次算探针OK
      failureThreshold: 3 # 失败三次算探针失败
  restartPolicy: Always # 可选 默认Always 容器故障或者没有启动成功 自动重启该容器 Onfailure: 容器以不为0的状态码终止 自动重启该容器 Never:无论何种状态 都不会重启
---
apiVersion: v1
kind: Service
metadata:
  name: ready-nodeport
  labels:
    name: ready-nodeport
spec:
  type: NodePort
  ports:
  - port: 88
    protocol: TCP
    targetPort: 80
    nodePort: 30880
  selector:
    app: nginx-ready

创建pod与service并访问测试

[root@k8s-master01 ~]# kubectl apply -f pod.yaml 
pod/nginx created
service/ready-nodeport created
[root@k8s-master01 ~]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          4s
[root@k8s-master01 ~]# kubectl get svc -owide
NAME             TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes       ClusterIP   10.96.0.1     <none>        443/TCP        72d   <none>
ready-nodeport   NodePort    10.96.54.68   <none>        88:30880/TCP   12s   app=nginx-ready
[root@k8s-master01 ~]# curl 192.168.10.10:30880
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

修改检测端口为81,模拟检测失败

探针检测失败会根据 restartPolicy 重启策略操作
默认Always 容器故障或者没有启动成功 自动重启该容器

重新创建该pod

[root@k8s-master01 ~]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          4s
[root@k8s-master01 ~]# 
[root@k8s-master01 ~]# kubectl get svc -owide
NAME             TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE     SELECTOR
kubernetes       ClusterIP   10.96.0.1     <none>        443/TCP        72d     <none>
ready-nodeport   NodePort    10.96.54.68   <none>        88:30880/TCP   7m28s   app=nginx-ready

[root@k8s-master01 ~]# curl http://192.168.10.10:30880
curl: (7) Failed connect to 192.168.10.10:30880; 拒绝连接

kubectl describe pod nginx 进行查看

查看pod是否进行重启,可以看到pod已经重启5次,状态也变为 CrashLoopBackOff

[root@k8s-master01 ~]# kubectl get pod
NAME    READY   STATUS             RESTARTS      AGE
nginx   0/1     CrashLoopBackOff   5 (28s ago)   2m10s
posted @ 2022-07-11 17:36  Chuyio  阅读(962)  评论(0编辑  收藏  举报