【云原生】kubernetes中pod的生命周期、探测钩子的实战应用案例解析

news2024/10/6 22:24:54

在这里插入图片描述

✨✨ 欢迎大家来到景天科技苑✨✨

🎈🎈 养成好习惯,先赞后看哦~🎈🎈

🏆 作者简介:景天科技苑
🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。
🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi,flask等框架,云原生k8s,linux,shell脚本等实操经验,网站搭建,数据库等分享。

所属的专栏:云原生K8S,零基础到进阶实战
景天的主页:景天科技苑

文章目录

  • 1.Pod生命周期
    • 1.1 init容器
    • 1.2 主容器
      • 容器钩子
    • 1.3 创建pod需要经过哪些阶段?
  • 2.Pod容器探测和钩子
    • 2.1 容器钩子:postStart和preStop
    • 2.2 存活性探测livenessProbe和就绪性探测readinessProbe
      • 1、LivenessProbe 探针使用示例
      • 2、ReadinessProbe 探针使用示例
      • 3、ReadinessProbe + LivenessProbe 配合使用示例

1.Pod生命周期

1.1 init容器

Pod 里面可以有一个或者多个容器,部署应用的容器可以称为主容器,
在创建Pod时候,Pod 中可以有一个或多个先于主容器启动的Init容器,这个init容器就可以成为初始化容器,
初始化容器一旦执行完,它从启动开始到初始化代码执行完就退出了,它不会一直存在,
所以在主容器启动之前执行初始化,初始化容器可以有多个,多个初始化容器是要串行执行的,先执行初始化容器1,
在执行初始化容器2等,等初始化容器执行完初始化就退出了,然后再执行主容器,主容器一退出,pod就结束了,
主容器退出的时间点就是pod的结束点,它俩时间轴是一致的;

Init容器就是做初始化工作的容器。可以有一个或多个,如果多个按照定义的顺序依次执行,
只有所有的初始化容器执行完后,主容器才启动。由于一个Pod里的存储卷是共享的,
所以Init Container里产生的数据可以被主容器使用到,Init Container可以在多种K8S资源里被使用到,
如Deployment、DaemonSet, StatefulSet、Job等,但都是在Pod启动时,在主容器启动前执行,做初始化工作。

kubectl explain pod.spec.containers 这里定义的才是主容器

Init容器与普通的容器区别是:
1、Init 容器不支持 Readiness,因为它们必须在Pod就绪之前运行完成
2、每个Init容器必须运行成功,下一个才能够运行
3、如果 Pod 的 Init 容器失败,Kubernetes 会不断地重启该 Pod,直到 Init 容器成功为止,
然而,如果Pod对应的restartPolicy值为 Never,它不会重新启动。

创建个init资源清单:

[root@master01 pod-test ]# cat init.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
[root@master01 pod-test ]# kubectl get pods -owide
NAME                          READY   STATUS     RESTARTS   AGE     IP               NODE     NOMINATED NODE   READINESS GATES
demo-nodeselector             1/1     Running    3          2d23h   172.21.231.155   node02   <none>           <none>
myapp-deploy                  1/1     Running    1          22h     172.21.231.158   node02   <none>           <none>
myapp-pod                     0/1     Init:0/2   0          52s     172.29.55.35     node01   <none>           <none>
nginx-test-64b444bff5-6t2mb   1/1     Running    0          93m     172.29.55.33     node01   <none>           <none>
nginx-test-64b444bff5-ltj29   1/1     Running    0          93m     172.21.231.160   node02   <none>           <none>
pod-node-affinity-demo-2      1/1     Running    2          2d6h    172.21.231.157   node02   <none>           <none>
pod-restart                   1/1     Running    0          70m     172.29.55.34     node01   <none>           <none>
test                          1/1     Running    4          5d23h   172.21.231.159   node02   <none>           <none>

myapp-pod 一直是init状态:

[root@master01 pod-test ]# kubectl logs myapp-pod 
Error from server (BadRequest): container "myapp-container" in pod "myapp-pod" is waiting to start: PodInitializing

直到解析出service才会初始化成功,创建主容器,不然初始化一直在循环

创建service文件:

[root@master01 pod-test ]# cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9377
[root@master01 pod-test ]# kubectl apply -f service.yaml 
service/myservice created
service/mydb created
[root@master01 pod-test ]# kubectl get pods -owide
NAME                          READY   STATUS    RESTARTS   AGE     IP               NODE     NOMINATED NODE   READINESS GATES
demo-nodeselector             1/1     Running   3          2d23h   172.21.231.155   node02   <none>           <none>
myapp-deploy                  1/1     Running   1          22h     172.21.231.158   node02   <none>           <none>
myapp-pod                     1/1     Running   0          5m10s   172.29.55.35     node01   <none>           <none>
nginx-test-64b444bff5-6t2mb   1/1     Running   0          97m     172.29.55.33     node01   <none>           <none>
nginx-test-64b444bff5-ltj29   1/1     Running   0          97m     172.21.231.160   node02   <none>           <none>
pod-node-affinity-demo-2      1/1     Running   2          2d6h    172.21.231.157   node02   <none>           <none>
pod-restart                   1/1     Running   0          75m     172.29.55.34     node01   <none>           <none>
test                          1/1     Running   4          5d23h   172.21.231.159   node02   <none>           <none>

可以看到myapp-pod 已经启动

主容器在创建之前,先经历两个初始化容器步骤

1.2 主容器

容器钩子

初始化容器启动之后,开始启动主容器,在主容器启动之前有一个post start hook(容器启动后钩子)和pre stop hook(容器结束前钩子),
无论启动后还是结束前所做的事我们可以把它放两个钩子,这个钩子就表示用户可以用它来钩住一些命令,来执行它,做开场前的预设,
结束前的清理,如awk有begin,end,和这个效果类似;

  • postStart:该钩子在容器被创建后立刻触发,通知容器它已经被创建。
    如果该钩子对应的hook handler执行失败,则该容器会被杀死,并根据该容器的重启策略决定是否要重启该容器,这个钩子不需要传递任何参数。

  • preStop:该钩子在容器被删除前触发,其所对应的hook handler必须在删除该容器的请求发送给Docker daemon之前完成。
    在该钩子对应的hook handler完成后不论执行的结果如何,
    Docker daemon会发送一个SGTERN信号量给Docker daemon来删除该容器,这个钩子不需要传递任何参数。

在k8s中支持两类对pod的检测:
第一类叫做livenessprobe(pod存活性探测):
存活探针主要作用是,用指定的方式检测pod中的容器应用是否正常运行,
如果检测失败,则认为容器不健康,那么Kubelet将根据Pod中设置的 restartPolicy来判断Pod 是否要进行重启操作,
如果容器配置中没有配置 livenessProbe,Kubelet 将认为存活探针探测一直为成功状态,一直是running。

第二类是状态检readinessprobe(pod就绪性探测):
用于判断容器中应用是否启动完成,
当探测成功后才使Pod对外提供网络访问,设置容器Ready状态为true,如果探测失败,则设置容器的Ready状态为false。

查看官方解读用法:

livenessProbe:

[root@master01 pod-test ]# kubectl explain pods.spec.containers.livenessProbe
KIND:     Pod
VERSION:  v1

RESOURCE: livenessProbe <Object>

DESCRIPTION:
     Periodic probe of container liveness. Container will be restarted if the
     probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

     Probe describes a health check to be performed against a container to
     determine whether it is alive or ready to receive traffic.

FIELDS:
   exec	<Object>
     One and only one of the following should be specified. Exec specifies the
     action to take.

   failureThreshold	<integer>
     Minimum consecutive failures for the probe to be considered failed after
     having succeeded. Defaults to 3. Minimum value is 1.

   httpGet	<Object>
     HTTPGet specifies the http request to perform.

   initialDelaySeconds	<integer>
     Number of seconds after the container has started before liveness probes
     are initiated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

   periodSeconds	<integer>
     How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
     value is 1.

   successThreshold	<integer>
     Minimum consecutive successes for the probe to be considered successful
     after having failed. Defaults to 1. Must be 1 for liveness and startup.
     Minimum value is 1.

   tcpSocket	<Object>
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported

   terminationGracePeriodSeconds	<integer>
     Optional duration in seconds the pod needs to terminate gracefully upon
     probe failure. The grace period is the duration in seconds after the
     processes running in the pod are sent a termination signal and the time
     when the processes are forcibly halted with a kill signal. Set this value
     longer than the expected cleanup time for your process. If this value is
     nil, the pod's terminationGracePeriodSeconds will be used. Otherwise, this
     value overrides the value provided by the pod spec. Value must be
     non-negative integer. The value zero indicates stop immediately via the
     kill signal (no opportunity to shut down). This is an alpha field and
     requires enabling ProbeTerminationGracePeriod feature gate.

   timeoutSeconds	<integer>
     Number of seconds after which the probe times out. Defaults to 1 second.
     Minimum value is 1. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

readinessProbe:

[root@master01 pod-test ]# kubectl explain pods.spec.containers.readinessProbe
KIND:     Pod
VERSION:  v1

RESOURCE: readinessProbe <Object>

DESCRIPTION:
     Periodic probe of container service readiness. Container will be removed
     from service endpoints if the probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

     Probe describes a health check to be performed against a container to
     determine whether it is alive or ready to receive traffic.

FIELDS:
   exec	<Object>
     One and only one of the following should be specified. Exec specifies the
     action to take.

   failureThreshold	<integer>
     Minimum consecutive failures for the probe to be considered failed after
     having succeeded. Defaults to 3. Minimum value is 1.

   httpGet	<Object>
     HTTPGet specifies the http request to perform.

   initialDelaySeconds	<integer>
     Number of seconds after the container has started before liveness probes
     are initiated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

   periodSeconds	<integer>
     How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
     value is 1.

   successThreshold	<integer>
     Minimum consecutive successes for the probe to be considered successful
     after having failed. Defaults to 1. Must be 1 for liveness and startup.
     Minimum value is 1.

   tcpSocket	<Object>
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported

   terminationGracePeriodSeconds	<integer>
     Optional duration in seconds the pod needs to terminate gracefully upon
     probe failure. The grace period is the duration in seconds after the
     processes running in the pod are sent a termination signal and the time
     when the processes are forcibly halted with a kill signal. Set this value
     longer than the expected cleanup time for your process. If this value is
     nil, the pod's terminationGracePeriodSeconds will be used. Otherwise, this
     value overrides the value provided by the pod spec. Value must be
     non-negative integer. The value zero indicates stop immediately via the
     kill signal (no opportunity to shut down). This is an alpha field and
     requires enabling ProbeTerminationGracePeriod feature gate.

   timeoutSeconds	<integer>
     Number of seconds after which the probe times out. Defaults to 1 second.
     Minimum value is 1. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

1.3 创建pod需要经过哪些阶段?

当用户创建pod时,这个请求给apiserver,apiserver把创建请求的状态保存在etcd中;
接下来apiserver会请求scheduler来完成调度,
如果调度成功,会把调度的结果(如调度到哪个节点上了,运行在哪个节点上了,把它更新到etcd的pod资源状态中)保存在etcd中,
一旦存到etcd中并且完成更新以后,如调度到node01上,
那么node01节点上的kubelet通过apiserver当中的状态变化知道有一些任务被执行了,
所以此时此kubelet会拿到用户创建时所提交的清单,这个清单会在当前节点上运行或者启动这个pod,
如果创建成功或者失败会有一个当前状态,当前这个状态会发给apiserver,apiserver在存到etcd中;
在这个过程中,etcd和apiserver一直在打交道,不停的交互,scheduler也参与其中,负责调度pod到合适的node节点上,
这个就是pod的创建过程

pod在整个生命周期中有非常多的用户行为:
1、初始化容器完成初始化
2、主容器启动后可以做启动后钩子 postStart
3、主容器结束前可以做结束前钩子 preStop
4、在主容器运行中可以做一些健康检测,如liveness probe,readness probe

查看生命周期钩子官方解读:

[root@master01 pod-test ]# kubectl explain pods.spec.containers.lifecycle
KIND:     Pod
VERSION:  v1

RESOURCE: lifecycle <Object>

DESCRIPTION:
     Actions that the management system should take in response to container
     lifecycle events. Cannot be updated.

     Lifecycle describes actions that the management system should take in
     response to container lifecycle events. For the PostStart and PreStop
     lifecycle handlers, management of the container blocks until the action is
     complete, unless the container process fails, in which case the handler is
     aborted.

FIELDS:
   postStart	<Object>
     PostStart is called immediately after a container is created. If the
     handler fails, the container is terminated and restarted according to its
     restart policy. Other management of the container blocks until the hook
     completes. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks

   preStop	<Object>
     PreStop is called immediately before a container is terminated due to an
     API request or management event such as liveness/startup probe failure,
     preemption, resource contention, etc. The handler is not called if the
     container crashes or exits. The reason for termination is passed to the
     handler. The Pod's termination grace period countdown begins before the
     PreStop hooked is executed. Regardless of the outcome of the handler, the
     container will eventually terminate within the Pod's termination grace
     period. Other management of the container blocks until the hook completes
     or until the termination grace period is reached. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
[root@master01 pod-test ]# kubectl explain pods.spec.containers.lifecycle.postStart
KIND:     Pod
VERSION:  v1

RESOURCE: postStart <Object>

DESCRIPTION:
     PostStart is called immediately after a container is created. If the
     handler fails, the container is terminated and restarted according to its
     restart policy. Other management of the container blocks until the hook
     completes. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks

     Handler defines a specific action that should be taken

FIELDS:
   exec	<Object>
     One and only one of the following should be specified. Exec specifies the
     action to take.

   httpGet	<Object>
     HTTPGet specifies the http request to perform.

   tcpSocket	<Object>
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported

2.Pod容器探测和钩子

2.1 容器钩子:postStart和preStop

  • postStart:容器创建成功后,运行前的任务,用于资源部署、环境准备等。
  • preStop:在容器被终止前的任务,用于优雅关闭应用程序、通知其他系统等。
[root@master01 pod-test ]# kubectl explain pods.spec.containers.lifecycle.postStart.exec
KIND:     Pod
VERSION:  v1

RESOURCE: exec <Object>

DESCRIPTION:
     One and only one of the following should be specified. Exec specifies the
     action to take.

     ExecAction describes a "run in container" action.

FIELDS:
   command	<[]string>
     Command is the command line to execute inside the container, the working
     directory for the command is root ('/') in the container's filesystem. The
     command is simply exec'd, it is not run inside a shell, so traditional
     shell instructions ('|', etc) won't work. To use a shell, you need to
     explicitly call out to that shell. Exit status of 0 is treated as
     live/healthy and non-zero is unhealthy.

演示postStart和preStop用法

......
containers:
- image: sample:v2  
     name: war
     lifecycle:
      postStart:
       exec:
         command:
          - "cp"
          - "/sample.war"
          - "/app"
      prestop:
       httpGet:
        host: monitor.com
        path: /waring
        port: 8080
        scheme: HTTP
......

以上示例中,定义了一个Pod,包含一个JAVA的web应用容器,其中设置了PostStart和PreStop回调函数。
即在容器创建成功后,复制/sample.war到/app文件夹中。
而在容器终止之前,发送HTTP请求到http://monitor.com:8080/waring,即向监控系统发送警告。

2.2 存活性探测livenessProbe和就绪性探测readinessProbe

  • livenessProbe:存活性探测
    许多应用程序经过长时间运行,最终过渡到无法运行的状态,除了重启,无法恢复。
    通常情况下,K8S会发现应用程序已经终止,然后重启应用程序pod。
    有时应用程序可能因为某些原因(后端服务故障等)导致暂时无法对外提供服务,但应用软件没有终止,
    导致K8S无法隔离有故障的pod,调用者可能会访问到有故障的pod,导致业务不稳定。
    K8S提供livenessProbe来检测容器是否正常运行,并且对相应状况进行相应的补救措施。

  • readinessProbe:就绪性探测
    在没有配置readinessProbe的资源对象中,pod中的容器启动完成后,就认为pod中的应用程序可以对外提供服务,
    该pod就会加入相对应的service,对外提供服务。但有时一些应用程序启动后,需要较长时间的加载才能对外服务,
    如果这时对外提供服务,执行结果必然无法达到预期效果,影响用户体验。比如使用tomcat的应用程序来说,
    并不是简单地说tomcat启动成功就可以对外提供服务的,还需要等待spring容器初始化,数据库连接上等等。

目前LivenessProbe和ReadinessProbe两种探针都支持下面三种探测方法:

  • 1、Exec Action:在容器中执行指定的命令,如果执行成功,退出码为 0 则探测成功。
  • 2、TCPSocket Action:通过容器的 IP 地址和端口号执行 TCP 检查,如果能够建立 TCP 连接,则表明容器健康。
  • 3、HTTPGet Action:通过容器的IP地址、端口号及路径调用 HTTP Get方法,如果响应的状态码大于等于200且小于400,则认为容器健康

探针探测结果有以下值:

  • 1、Success:表示通过检测。
  • 2、Failure:表示未通过检测。
  • 3、Unknown:表示检测没有正常进行。

Pod探针相关的属性:
探针(Probe)有许多可选字段,可以用来更加精确的控制Liveness和Readiness两种探针的行为
initialDelaySeconds: Pod启动后首次进行检查的等待时间,单位“秒”。
periodSeconds: 检查的间隔时间,默认为10s,单位“秒” 最小值1秒。
timeoutSeconds: 探针执行检测请求后,等待响应的超时时间,默认为1s,单位“秒”。
successThreshold: 连续探测几次成功,才认为探测成功,默认为 1,在 Liveness 探针中必须为1,最小值为1。
failureThreshold: 探测失败的重试次数,重试一定次数后将认为失败,
在 readiness 探针中,Pod会被标记为未就绪,默认为 3,最小值为 1

两种探针区别:
ReadinessProbe 和 livenessProbe 可以使用相同探测方式,只是对 Pod 的处置方式不同:
readinessProbe 当检测失败后,将 Pod 的 IP:Port 从对应的 EndPoint 列表中删除。
livenessProbe 当检测失败后,将杀死容器并根据 Pod 的重启策略来决定作出对应的措施。

[root@master01 probe ]# kubectl explain pods.spec.containers.readinessProbe.httpGet
KIND:     Pod
VERSION:  v1

RESOURCE: httpGet <Object>

DESCRIPTION:
     HTTPGet specifies the http request to perform.

     HTTPGetAction describes an action based on HTTP Get requests.

FIELDS:
   host	<string>
     Host name to connect to, defaults to the pod IP. You probably want to set
     "Host" in httpHeaders instead.

   httpHeaders	<[]Object>
     Custom headers to set in the request. HTTP allows repeated headers.

   path	<string>
     Path to access on the HTTP server.

   port	<string> -required-
     Name or number of the port to access on the container. Number must be in
     the range 1 to 65535. Name must be an IANA_SVC_NAME.

   scheme	<string>
     Scheme to use for connecting to the host. Defaults to HTTP.

Pod探针使用示例:

1、LivenessProbe 探针使用示例

(1)、通过exec方式做健康探测
示例文件 liveness-exec.yaml

apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec
  labels:
    app: liveness
spec:
  containers:
  - name: liveness
    image: busybox
    args:                       #创建测试探针探测的文件
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
    livenessProbe:
      initialDelaySeconds: 10   #延迟检测时间
      periodSeconds: 5          #检测时间间隔
      exec:
        command:
        - cat
        - /tmp/healthy

容器启动设置执行的命令:
/bin/sh -c “touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600”
容器在初始化后,首先创建一个 /tmp/healthy 文件,然后执行睡眠命令,睡眠 30 秒,到时间后执行删除 /tmp/healthy 文件命令。
而设置的存活探针检检测方式为执行 shell 命令,用 cat 命令输出 healthy 文件的内容,
如果能成功执行这条命令,存活探针就认为探测成功,否则探测失败。在前 30 秒内,
由于文件存在,所以存活探针探测时执行 cat /tmp/healthy 命令成功执行。30 秒后 healthy 文件被删除,
所以执行命令失败,Kubernetes 会根据 Pod 设置的重启策略来判断,是否重启 Pod。

(2)、通过HTTP方式做健康探测:

[root@master01 probe ]# vim liveness-http.yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-http
  labels:
    test: liveness
spec:
  containers:
  - name: liveness
    image: mydlqclub/springboot-helloworld:0.0.1
    livenessProbe:
      initialDelaySeconds: 20   #延迟加载时间,表示在容器启动后,延时多少秒才开始探测
      periodSeconds: 5          #重试时间间隔
      timeoutSeconds: 10        #超时时间设置
      httpGet:
        scheme: HTTP
        port: 8081
        path: /actuator/health
[root@master01 probe ]# kubectl get pods  -owide
NAME                          READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
demo-nodeselector             1/1     Running   9          14d   172.21.231.190   node02   <none>           <none>
liveness-exec                 1/1     Running   13         40m   172.21.231.191   node02   <none>           <none>
liveness-http                 1/1     Running   5          8d    172.29.55.60     node01   <none>           <none>
liveness-tcp                  1/1     Running   4          8d    172.29.55.58     node01   <none>           <none>
myapp-pod                     1/1     Running   67         11d   172.29.55.59     node01   <none>           <none>
nginx-test-64b444bff5-6t2mb   1/1     Running   7          11d   172.29.55.57     node01   <none>           <none>
nginx-test-64b444bff5-ltj29   1/1     Running   6          11d   172.21.231.189   node02   <none>           <none>
pod-node-affinity-demo-2      1/1     Running   8          14d   172.21.231.187   node02   <none>           <none>
test                          1/1     Running   10         17d   172.21.231.186   node02   <none>           <none>

host默认是pod的ip

curl -I url返回状态码

[root@master01 probe ]# curl -I http://172.29.55.60:8081/actuator/health
HTTP/1.1 200 
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 16 Aug 2022 06:32:57 GMT

可见返回码是200,存活性探测成功

上面 Pod 中启动的容器是一个 SpringBoot 应用,其中引用了 Actuator 组件,提供了 /actuator/health 健康检查地址,
存活探针可以使用 HTTPGet 方式向服务发起请求,请求 8081 端口的 /actuator/health 路径来进行存活判断:

任何大于或等于200且小于400的代码表示探测成功。
任何其他代码表示失败。

如果探测失败,则会杀死 Pod 进行重启操作。

httpGet探测方式有如下可选的控制字段:
scheme: 用于连接host的协议,默认为HTTP。
host:要连接的主机名,默认为Pod IP,可以在http request head中设置host头部。
port:容器上要访问端口号或名称。
path:http服务器上的访问URI。
httpHeaders:自定义HTTP请求headers,HTTP允许重复headers。

(3)、通过TCP方式做健康探测:

[root@master01 probe ]# kubectl explain pods.spec.containers.readinessProbe.tcpSocket
KIND:     Pod
VERSION:  v1

RESOURCE: tcpSocket <Object>

DESCRIPTION:
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported

     TCPSocketAction describes an action based on opening a socket

FIELDS:
   host	<string>
     Optional: Host name to connect to, defaults to the pod IP.

   port	<string> -required-
     Number or name of the port to access on the container. Number must be in
     the range 1 to 65535. Name must be an IANA_SVC_NAME.
[root@master01 probe ]# vim liveness-tcp.yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-tcp
  labels:
    app: liveness
spec:
  containers:
  - name: liveness
    image: nginx
    livenessProbe:
      initialDelaySeconds: 15
      periodSeconds: 20
      tcpSocket:
        port: 80
[root@master01 probe ]# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
demo-nodeselector             1/1     Running   5          6d21h
liveness-http                 1/1     Running   0          11m
liveness-tcp                  1/1     Running   0          93s
myapp-pod                     1/1     Running   32         3d22h
nginx-test-64b444bff5-6t2mb   1/1     Running   3          3d23h
nginx-test-64b444bff5-ltj29   1/1     Running   2          3d23h
pod-node-affinity-demo-2      1/1     Running   4          6d4h
test                          1/1     Running   6          9d

2、ReadinessProbe 探针使用示例

Pod 的ReadinessProbe 探针使用方式和 LivenessProbe 探针探测方法一样,也是支持三种,
只是一个是用于探测应用的存活,
一个是判断是否对外提供流量的条件。
这里用一个 Springboot 项目,设置 ReadinessProbe
探测 SpringBoot 项目的 8081 端口下的 /actuator/health 接口,如果探测成功则代表内部程序以及启动,就开放对外提供接口访问,
否则内部应用没有成功启动,暂不对外提供访问,直到就绪探针探测成功。

[root@master01 probe ]# cat readiness-exec.yaml 
apiVersion: v1
kind: Service
metadata:
  name: springboot
  labels:
    app: springboot
spec:
  type: NodePort
  ports:
  - name: server
    port: 8080
    targetPort: 8080
    nodePort: 31180
  - name: management
    port: 8081
    targetPort: 8081
    nodePort: 31181
  selector:
    app: springboot
---
apiVersion: v1
kind: Pod
metadata:
  name: springboot
  labels:
    app: springboot
spec:
  containers:
  - name: springboot
    image: mydlqclub/springboot-helloworld:0.0.1
    ports:
    - name: server
      containerPort: 8080
    - name: management
      containerPort: 8081
    readinessProbe:
      initialDelaySeconds: 20   
      periodSeconds: 5          
      timeoutSeconds: 10   
      httpGet:
        scheme: HTTP
        port: 8081
        path: /actuator/health
[root@master01 probe ]# kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                         AGE
kubernetes   ClusterIP   192.168.0.1      <none>        443/TCP                         18d
mydb         ClusterIP   192.168.138.78   <none>        80/TCP                          11d
myservice    ClusterIP   192.168.81.58    <none>        80/TCP                          11d
springboot   NodePort    192.168.236.10   <none>        8080:31180/TCP,8081:31181/TCP   86s
[root@master01 probe ]# kubectl get pods -owide
NAME                          READY   STATUS             RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
demo-nodeselector             1/1     Running            9          14d   172.21.231.190   node02   <none>           <none>
liveness-exec                 0/1     CrashLoopBackOff   20         66m   172.21.231.191   node02   <none>           <none>
liveness-http                 1/1     Running            5          8d    172.29.55.60     node01   <none>           <none>
liveness-tcp                  1/1     Running            4          8d    172.29.55.58     node01   <none>           <none>
myapp-pod                     1/1     Running            68         11d   172.29.55.59     node01   <none>           <none>
nginx-test-64b444bff5-6t2mb   1/1     Running            7          12d   172.29.55.57     node01   <none>           <none>
nginx-test-64b444bff5-ltj29   1/1     Running            6          12d   172.21.231.189   node02   <none>           <none>
pod-node-affinity-demo-2      1/1     Running            8          14d   172.21.231.187   node02   <none>           <none>
springboot                    1/1     Running            0          9m    172.29.55.61     node01   <none>           <none>
test                          1/1     Running            10         17d   172.21.231.186   node02   <none>           <none>

验证

[root@master01 probe ]# curl -I http://172.29.55.61:8081/actuator/health
HTTP/1.1 200 
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 16 Aug 2022 07:08:12 GMT

3、ReadinessProbe + LivenessProbe 配合使用示例

一般程序中需要设置两种探针结合使用,并且也要结合实际情况,来配置初始化检查时间和检测间隔,
下面列一个简单的 SpringBoot 项目的 Deployment 例子。

[root@master01 probe ]# cat live-redi.yaml 
apiVersion: v1
kind: Service
metadata:
  name: springboot1
  labels:
    app: springboot1
spec:
  type: NodePort
  ports:
  - name: server
    port: 8080
    targetPort: 8080
    nodePort: 30180
  - name: management1
    port: 8081
    targetPort: 8081
    nodePort: 30181
  selector:
    app: springboot1
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot1
  labels:
    app: springboot1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: springboot1
  template:
    metadata:
      name: springboot1
      labels:
        app: springboot1
    spec:
      containers:
      - name: readiness
        image: mydlqclub/springboot-helloworld:0.0.1
        ports:
        - name: server1 
          containerPort: 8080
        - name: management1
          containerPort: 8081
        readinessProbe:
          initialDelaySeconds: 20 
          periodSeconds: 5      
          timeoutSeconds: 10        
          httpGet:
            scheme: HTTP
            port: 8081
            path: /actuator/health
        livenessProbe:
          initialDelaySeconds: 30 
          periodSeconds: 10 
          timeoutSeconds: 5 
          httpGet:
            scheme: HTTP
            port: 8081
            path: /actuator/health

kind类型是deployment时。创建pod要定义template字段

[root@master01 probe ]# kubectl get pods -owide
NAME                           READY   STATUS             RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
demo-nodeselector              1/1     Running            9          14d   172.21.231.190   node02   <none>           <none>
liveness-exec                  0/1     CrashLoopBackOff   26         90m   172.21.231.191   node02   <none>           <none>
liveness-http                  1/1     Running            5          8d    172.29.55.60     node01   <none>           <none>
liveness-tcp                   1/1     Running            4          8d    172.29.55.58     node01   <none>           <none>
myapp-pod                      1/1     Running            68         11d   172.29.55.59     node01   <none>           <none>
nginx-test-64b444bff5-6t2mb    1/1     Running            7          12d   172.29.55.57     node01   <none>           <none>
nginx-test-64b444bff5-ltj29    1/1     Running            6          12d   172.21.231.189   node02   <none>           <none>
pod-node-affinity-demo-2       1/1     Running            8          14d   172.21.231.187   node02   <none>           <none>
springboot                     1/1     Running            0          32m   172.29.55.61     node01   <none>           <none>
springboot1-7cf5df696d-pk5bv   1/1     Running            0          67s   172.29.55.62     node01   <none>           <none>
test                           1/1     Running            10         17d   172.21.231.186   node02   <none>           <none>
[root@master01 probe ]# kubectl get service
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                         AGE
kubernetes    ClusterIP   192.168.0.1      <none>        443/TCP                         18d
mydb          ClusterIP   192.168.138.78   <none>        80/TCP                          11d
myservice     ClusterIP   192.168.81.58    <none>        80/TCP                          11d
springboot    NodePort    192.168.236.10   <none>        8080:31180/TCP,8081:31181/TCP   32m
springboot1   NodePort    192.168.78.180   <none>        8080:30180/TCP,8081:30181/TCP   89s

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

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

相关文章

向量数据库引领 AI 创新——Zilliz 亮相 2024 亚马逊云科技中国峰会

2024年5月29日&#xff0c;亚马逊云科技中国峰会在上海召开&#xff0c;此次峰会聚集了来自全球各地的科技领袖、行业专家和创新企业&#xff0c;探讨云计算、大数据、人工智能等前沿技术的发展趋势和应用场景。作为领先的向量数据库技术公司&#xff0c;Zilliz 在本次峰会上展…

Pont在小程序开发的使用

Pont是一个很好的前后端桥&#xff0c;但是有个问题。默认产生的代码&#xff0c;无法支持微信小程序开发。根本原因是因为使用了window给全局的对象注入了API和refs属性&#xff0c;由于小程序没有window属性&#xff0c;当然就无法使用了&#xff0c;解决办法也比较简单。只需…

轻松入门:HTML网页制作指南 进阶篇

一.表格标签 1.1表格的主要作用 表格不是用来布局页面的,而是用来展示数据的。 1.2基本语法 <table><tr><td>单元格内的文字</td>...</tr>...</table>说明&#xff1a; 1.<table> </table> 是用于定义表格的标签。 2.<t…

Iphone自动化指令每隔固定天数打开闹钟关闭闹钟(一)

注意&#xff1a;因为是第一次用iphone的快捷指令&#xff0c;不是很明白&#xff0c;所以之后多次运行发现有bug&#xff0c;所以快捷指令部分在下一章重新写&#xff0c;我用两个日期测试了&#xff0c;没问题&#xff0c;这一章可以当做熟悉快捷指令的一些操作用&#xff0c…

【协议开发系列】梳理关于TCP和UDP两种协议的区别和使用场景

起源 前二天项目上在核对外部对接服务的五元组列表的时候&#xff0c;有一位客户提问对于同样的服务同时支持tcp和udp二种方式&#xff0c;有什么优点和缺点&#xff0c;应该如何选择&#xff1f;这个问题突然让我愣了一下&#xff0c;确实好久没有“温故”了&#xff0c;相关…

【哈希】用哈希桶封装unordered_map unordered_set

&#x1f389;博主首页&#xff1a; 有趣的中国人 &#x1f389;专栏首页&#xff1a; C进阶 &#x1f389;其它专栏&#xff1a; C初阶 | Linux | 初阶数据结构 小伙伴们大家好&#xff0c;本片文章将会讲解 用哈希桶封装 unordered_map & unordered_set 的相关内容。 如…

pdf拆分成有图和无图的pdf(方便打印)

pdf拆分成有图和无图的pdf(方便打印) 原因 打印图片要彩印&#xff0c;每次都要手动弄&#xff0c;打印的时候很麻烦&#xff1b; 随着打印次数的增加&#xff0c;时间就越来越多 为解决此问题&#xff0c;使用python写一个exe解决这个问题 历程 找一个python的GUI界面找到 t…

USART串口外设

USART介绍 USART&#xff1a;另外我们经常还会遇到串口&#xff0c;叫UART&#xff0c;少了个S&#xff0c;就是通用异步收发器&#xff0c;一般我们串口很少使用这个同步功能&#xff0c;所以USART和UART使用起来&#xff0c;也没有什么区别。 其实这个STM32的USART同步模式&a…

MySQL 索引的使用

本篇主要介绍MySQL中索引使用的相关内容。 目录 一、最左前缀法则 二、索引失效的场景 索引列运算 字符串无引号 模糊查询 or连接条件 数据分布 一、最左前缀法则 当我们在使用多个字段构成的索引时&#xff08;联合索引&#xff09;&#xff0c;需要考虑最左前缀法则…

基于物理的分析模型,用于具有场板结构的GaN HEMT的输入、输出及反向电容

Physics-Based Analytical Model for Input, Output, and Reverse Capacitance of a GaN HEMT With the Field-Plate Structure&#xff08;TPE 17年&#xff09; 摘要 该论文提出了一种分析模型&#xff0c;用于描述带有场板结构的常开型AlGaN/GaN高电子迁移率晶体管&#x…

多屏多机同控!天途首发瑶光智控地面站

瑶光智控地面站全新发布&#xff01;高性能处理器&#xff0c;高亮三屏显示。内置天途云控系统&#xff0c;融合图传、控制、存储和数据处理等功能与一体&#xff0c;强大算力&#xff0c;高度集成无人机、无人船、无人车和机械狗等多种无人装备进行云控云算。 内置4G公网通讯模…

RabbitMQ-发布/订阅模式

1、发布/订阅模式介绍 在普通的生产者、消费者模式&#xff0c;rabbitmq会将消息依次传递给每一个消费者&#xff0c;一个worker一个&#xff0c;平均分配&#xff0c;这就是Round-robin调度方式&#xff0c;为了实现更加复杂的调度&#xff0c;我们就需要使用发布/订阅的方式…

现货白银的交易时间有多连贯?

国际市场上的现货白银优势很多&#xff0c;它除了具备国内同类型品种所不具备的数十倍资金杠杆外&#xff0c;也基本上实现了全天24小时不间断的交易时间&#xff0c;所以投资者可以在全天候连贯的行情中&#xff0c;寻找属于自己的交易获利机会。 但对于内地的投资者来说&…

【香橙派 AIpro】新手保姆级开箱教程:Linux镜像+vscode远程连接

香橙派 AIpro 开发板 AI 应用部署测评 写在最前面一、开发板概述官方资料试用印象适用场景 二、详细开发前准备步骤1. 环境准备2. 环境搭建3. vscode安装ssh插件4. 香橙派 AIpro 添加连接配置5. 连接香橙派 AIpro6. SSH配置 二、详细开发步骤1. 登录 juypter lab2. 样例运行3. …

HQChart使用教程100-uniapp如何在vue3运行微信小程序

HQChart使用教程100-uniapp如何在vue3运行微信小程序 症状原因分析解决思路解决步骤1. 修改vender.js2. 修改HQChartControl.js 完整实例HQChart代码地址 症状 HQChart插件在uniappvue3的项目编译成小程序以后&#xff0c; 运行会报错&#xff0c;见下图。 原因分析 查了下…

从了解到掌握 Spark 计算框架(二)RDD

文章目录 RDD 概述RDD 组成RDD 的作用RDD 算子分类RDD 的创建1.从外部数据源读取2.从已有的集合或数组创建3.从已有的 RDD 进行转换 RDD 常用算子大全转换算子行动算子 RDD 算子综合练习RDD 依赖关系窄依赖宽依赖宽窄依赖算子区分 RDD 血统信息血统信息的作用血统信息的组成代码…

【C语言回顾】预处理

前言1. 简单概要2. 预处理命令讲解结语 上期回顾: 【C语言回顾】编译和链接 个人主页&#xff1a;C_GUIQU 归属专栏&#xff1a;【C语言学习】 前言 各位小伙伴大家好&#xff01;上期小编给大家讲解了C语言中的编译和链接&#xff0c;接下来我们讲解一下预处理&#xff01; …

k8s自定义资源你会创建吗

创建自定义资源定义 CustomResourceDefinition 当你创建新的 CustomResourceDefinition&#xff08;CRD&#xff09;时&#xff0c;Kubernetes API 服务器会为你所 指定的每一个版本生成一个 RESTful 的 资源路径。CRD 可以是名字空间作用域的&#xff0c;也可以是集群作用域的…

接口测试工具:Postman的下载安装及使用

1 Postman 介绍 1.1 Postman 是什么 Postman 是一款功能超级强大的用于发送 HTTP 请求的 测试工具 做 WEB 页面开发和测试的人员常用工具 创建和发送任何的 HTTP 请求(Get/Post/Put/Delete...) 1.2 Postman 相关资源 1.2.1 官方网站&#xff1a;https://www.postman.com/ …

算法(七)插入排序

文章目录 插入排序简介代码实现 插入排序简介 插入排序&#xff08;insertion sort)是从第一个元素开始&#xff0c;该元素就认为已经被排序过了。然后取出下一个元素&#xff0c;从该元素的前一个索引下标开始往前扫描&#xff0c;比该值大的元素往后移动。直到遇到比它小的元…