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

在这里插入图片描述

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

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

🏆 作者简介:景天科技苑
🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,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-podlabels:app: myapp
spec:containers:- name: myapp-containerimage: busybox:1.28command: ['sh', '-c', 'echo The app is running! && sleep 3600']initContainers:- name: init-myserviceimage: busybox:1.28command: ['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-mydbimage: busybox:1.28command: ['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: TCPport: 80targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:name: mydb
spec:ports:- protocol: TCPport: 80targetPort: 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:  v1RESOURCE: livenessProbe <Object>DESCRIPTION:Periodic probe of container liveness. Container will be restarted if theprobe fails. Cannot be updated. More info:https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probesProbe describes a health check to be performed against a container todetermine whether it is alive or ready to receive traffic.FIELDS:exec	<Object>One and only one of the following should be specified. Exec specifies theaction to take.failureThreshold	<integer>Minimum consecutive failures for the probe to be considered failed afterhaving 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 probesare initiated. More info:https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probesperiodSeconds	<integer>How often (in seconds) to perform the probe. Default to 10 seconds. Minimumvalue is 1.successThreshold	<integer>Minimum consecutive successes for the probe to be considered successfulafter 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 yetsupportedterminationGracePeriodSeconds	<integer>Optional duration in seconds the pod needs to terminate gracefully uponprobe failure. The grace period is the duration in seconds after theprocesses running in the pod are sent a termination signal and the timewhen the processes are forcibly halted with a kill signal. Set this valuelonger than the expected cleanup time for your process. If this value isnil, the pod's terminationGracePeriodSeconds will be used. Otherwise, thisvalue overrides the value provided by the pod spec. Value must benon-negative integer. The value zero indicates stop immediately via thekill signal (no opportunity to shut down). This is an alpha field andrequires 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:  v1RESOURCE: readinessProbe <Object>DESCRIPTION:Periodic probe of container service readiness. Container will be removedfrom service endpoints if the probe fails. Cannot be updated. More info:https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probesProbe describes a health check to be performed against a container todetermine whether it is alive or ready to receive traffic.FIELDS:exec	<Object>One and only one of the following should be specified. Exec specifies theaction to take.failureThreshold	<integer>Minimum consecutive failures for the probe to be considered failed afterhaving 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 probesare initiated. More info:https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probesperiodSeconds	<integer>How often (in seconds) to perform the probe. Default to 10 seconds. Minimumvalue is 1.successThreshold	<integer>Minimum consecutive successes for the probe to be considered successfulafter 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 yetsupportedterminationGracePeriodSeconds	<integer>Optional duration in seconds the pod needs to terminate gracefully uponprobe failure. The grace period is the duration in seconds after theprocesses running in the pod are sent a termination signal and the timewhen the processes are forcibly halted with a kill signal. Set this valuelonger than the expected cleanup time for your process. If this value isnil, the pod's terminationGracePeriodSeconds will be used. Otherwise, thisvalue overrides the value provided by the pod spec. Value must benon-negative integer. The value zero indicates stop immediately via thekill signal (no opportunity to shut down). This is an alpha field andrequires 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:  v1RESOURCE: lifecycle <Object>DESCRIPTION:Actions that the management system should take in response to containerlifecycle events. Cannot be updated.Lifecycle describes actions that the management system should take inresponse to container lifecycle events. For the PostStart and PreStoplifecycle handlers, management of the container blocks until the action iscomplete, unless the container process fails, in which case the handler isaborted.FIELDS:postStart	<Object>PostStart is called immediately after a container is created. If thehandler fails, the container is terminated and restarted according to itsrestart policy. Other management of the container blocks until the hookcompletes. More info:https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hookspreStop	<Object>PreStop is called immediately before a container is terminated due to anAPI request or management event such as liveness/startup probe failure,preemption, resource contention, etc. The handler is not called if thecontainer crashes or exits. The reason for termination is passed to thehandler. The Pod's termination grace period countdown begins before thePreStop hooked is executed. Regardless of the outcome of the handler, thecontainer will eventually terminate within the Pod's termination graceperiod. Other management of the container blocks until the hook completesor 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:  v1RESOURCE: postStart <Object>DESCRIPTION:PostStart is called immediately after a container is created. If thehandler fails, the container is terminated and restarted according to itsrestart policy. Other management of the container blocks until the hookcompletes. More info:https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooksHandler defines a specific action that should be takenFIELDS:exec	<Object>One and only one of the following should be specified. Exec specifies theaction to take.httpGet	<Object>HTTPGet specifies the http request to perform.tcpSocket	<Object>TCPSocket specifies an action involving a TCP port. TCP hooks not yetsupported

2.Pod容器探测和钩子

2.1 容器钩子:postStart和preStop

  • postStart:容器创建成功后,运行前的任务,用于资源部署、环境准备等。
  • preStop:在容器被终止前的任务,用于优雅关闭应用程序、通知其他系统等。
[root@master01 pod-test ]# kubectl explain pods.spec.containers.lifecycle.postStart.exec
KIND:     Pod
VERSION:  v1RESOURCE: exec <Object>DESCRIPTION:One and only one of the following should be specified. Exec specifies theaction to take.ExecAction describes a "run in container" action.FIELDS:command	<[]string>Command is the command line to execute inside the container, the workingdirectory for the command is root ('/') in the container's filesystem. Thecommand is simply exec'd, it is not run inside a shell, so traditionalshell instructions ('|', etc) won't work. To use a shell, you need toexplicitly call out to that shell. Exit status of 0 is treated aslive/healthy and non-zero is unhealthy.

演示postStart和preStop用法

......
containers:
- image: sample:v2  name: warlifecycle:postStart:exec:command:- "cp"- "/sample.war"- "/app"prestop:httpGet:host: monitor.compath: /waringport: 8080scheme: 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:  v1RESOURCE: 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 inthe 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-execlabels:app: liveness
spec:containers:- name: livenessimage: busyboxargs:                       #创建测试探针探测的文件- /bin/sh- -c- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600livenessProbe: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-httplabels:test: liveness
spec:containers:- name: livenessimage: mydlqclub/springboot-helloworld:0.0.1livenessProbe:initialDelaySeconds: 20   #延迟加载时间,表示在容器启动后,延时多少秒才开始探测periodSeconds: 5          #重试时间间隔timeoutSeconds: 10        #超时时间设置httpGet:scheme: HTTPport: 8081path: /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:  v1RESOURCE: tcpSocket <Object>DESCRIPTION:TCPSocket specifies an action involving a TCP port. TCP hooks not yetsupportedTCPSocketAction describes an action based on opening a socketFIELDS: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 inthe 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-tcplabels:app: liveness
spec:containers:- name: livenessimage: nginxlivenessProbe:initialDelaySeconds: 15periodSeconds: 20tcpSocket: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: springbootlabels:app: springboot
spec:type: NodePortports:- name: serverport: 8080targetPort: 8080nodePort: 31180- name: managementport: 8081targetPort: 8081nodePort: 31181selector:app: springboot
---
apiVersion: v1
kind: Pod
metadata:name: springbootlabels:app: springboot
spec:containers:- name: springbootimage: mydlqclub/springboot-helloworld:0.0.1ports:- name: servercontainerPort: 8080- name: managementcontainerPort: 8081readinessProbe:initialDelaySeconds: 20   periodSeconds: 5          timeoutSeconds: 10   httpGet:scheme: HTTPport: 8081path: /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: springboot1labels:app: springboot1
spec:type: NodePortports:- name: serverport: 8080targetPort: 8080nodePort: 30180- name: management1port: 8081targetPort: 8081nodePort: 30181selector:app: springboot1
---
apiVersion: apps/v1
kind: Deployment
metadata:name: springboot1labels:app: springboot1
spec:replicas: 1selector:matchLabels:app: springboot1template:metadata:name: springboot1labels:app: springboot1spec:containers:- name: readinessimage: mydlqclub/springboot-helloworld:0.0.1ports:- name: server1 containerPort: 8080- name: management1containerPort: 8081readinessProbe:initialDelaySeconds: 20 periodSeconds: 5      timeoutSeconds: 10        httpGet:scheme: HTTPport: 8081path: /actuator/healthlivenessProbe:initialDelaySeconds: 30 periodSeconds: 10 timeoutSeconds: 5 httpGet:scheme: HTTPport: 8081path: /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.mzph.cn/web/21533.shtml

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

相关文章

强国机械制造有限公司引入先进制造技术,提升产品质量和生产效率

强国机械制造有限公司2024年6月3日宣布引入了一系列先进制造技术,包括机器学习、人工智能和物联网等,旨在提升其产品的质量和生产效率。这些前沿技术的应用,使得公司的制造过程更加智能化和数据驱动,显著提高了产品的精度和稳定性。 通过机器学习算法,强国机械能够分析和预测生…

《深入浅出C语言:从基础到指针的全面指南》

1. 简介 C语言是一种通用的编程语言&#xff0c;广泛应用于系统编程、嵌入式系统和高性能应用程序。它由Dennis Ritchie在1972年开发&#xff0c;并且至今仍然非常流行。C语言以其高效、灵活和强大的功能著称&#xff0c;是许多现代编程语言的基础。 2. 基本语法 2.1 Hello, …

VIKOR方法

简介 VIKOR方法是一种多标准决策&#xff08;MCDM&#xff09;或多标准决策分析方法。它最初由 Serafim Opricovic 开发&#xff0c;用于解决具有冲突和不可通约&#xff08;不同单位&#xff09;标准的决策问题&#xff0c;假设冲突解决可以接受妥协&#xff0c;决策者想要一…

C++中static关键字用法总结

在C中&#xff0c;关键字static有多种用途&#xff0c;它可以用于变量、函数。下面是static在不同上下文中的作用和举例。下面从static修饰的变量、函数三方面进行总结。 1、静态变量 静态变量分为全局变量、局部变量、函数中变量两种。 1.1 静态全局变量 静态全局变量声明在…

前端框架前置知识之Node.js:Node.js入门

前端程序员有必要学 Node.js 吗&#xff1f;要学到什么程度&#xff1f; 小朋友&#xff0c;你是否有很多问号&#xff1f; 对于node.js&#xff0c;不知道你是否和我一样有很多问号&#xff1f; 其实在学习node.js之前&#xff0c;我已经学完了Vue框架&#xff0c;而且已经…

排序算法(C++)

参考C算法&#xff0c;这里面有些写法也值得商榷。 1. 冒泡排序算法 冒泡排序算法代码和思路比较简单&#xff0c;大家如果在面试时被要求实现排序时&#xff0c;可以用这种方法来实现。 该算法里&#xff0c;会统一地遍历待排序的数据&#xff0c;每次比较两个相邻的数据&a…

变现 5w+,一个被严重低估的 AI 蓝海赛道,居然用这个免费的AI绘画工具就能做!

大家好&#xff0c;我是画画的小强&#xff0c;致力于分享各类的 AI 工具&#xff0c;包括 AI 绘画工具、AI 视频工具、AI 写作工具等等。 但单纯地为了学而学&#xff0c;是没有任何意义的。 这些 AI 工具&#xff0c;学会了&#xff0c;用起来&#xff0c;才能发挥出他们的…

深入探讨ChatGPT API中的Tokens计算方式和计算库

引言 在现代人工智能应用中&#xff0c;自然语言处理&#xff08;NLP&#xff09;技术无疑是最受关注的领域之一。OpenAI推出的ChatGPT&#xff0c;作为一种先进的对话模型&#xff0c;已经在多个领域展示了其强大的语言生成能力。为了更好地使用ChatGPT API&#xff0c;理解其…

Amazon云计算AWS(二)

目录 三、简单存储服务S3&#xff08;一&#xff09;S3的基本概念和操作&#xff08;二&#xff09;S3的数据一致性模型&#xff08;三&#xff09;S3的安全措施 四、非关系型数据库服务SimpleDB和DynamoDB&#xff08;一&#xff09;非关系型数据库与传统关系数据库的比较&…

短剧出海的优势分析

海外短剧作为一种新兴的内容形式&#xff0c;正以其独特的魅力迅速占领市场&#xff0c;为企业带来了前所未有的商业机遇。本文将深入探讨短剧出海的优势&#xff0c;并为企业和老板们提供实用的操作指南。短剧出海是一个包含多个步骤的复杂过程&#xff0c;短剧出海需要综合考…

山景BP1048固件加密

1.在电脑的USB口插入山景公司的加密狗。 2.打开MVAssistant_BP10xx_V1.8.15(2022.04.19)软件 3.选择芯片型号。 4.选择M4模式 5.code数据选择编译好的固件&#xff0c;const数据选择编译好的提示音 6.输入加密密码 7.点击代码加密 8.导出MVA文件

强化学习 (三) 动态规划

文章目录 迭代法网友认为的迭代策略评估与价值迭代的区别 迭代策略评估的进一步解释附录 传统dp作用有限&#xff1a; 需要完备的环境模型计算的复杂度极高 其它方法都是对dp的近似&#xff0c;近似的出发点是解决上面两个问题。 有一种说法是&#xff0c;强化学习其实就是拟…

PS系统教程09

修复照片 修饰工具 污点修复画笔工具&#xff08;J&#xff09; 主要作用&#xff1a;去除一些污点或者不需要的 【&#xff1a;缩小】&#xff1a;放大 目标&#xff1a;去掉这两个点 修复画笔工具 也就是说我们要有取样点 选择修复画笔工具按住Alt键吸取周边相近颜色松开单机…

导航时间与坐标转换

前言&#xff1a; 该章节代码均在Gitee中开源&#xff1a;因为这章是学校作业&#xff0c;所以稍微正经点. 时空位置转换https://gitee.com/Ehundred/navigation-engineering/tree/master/%E5%8D%AB%E6%98%9F%E5%AF%BC%E8%88%AA%E5%8E%9F%E7%90%86/%E5%AF%BC%E8%88%AA%E6%97…

tmux工具使用鼠标滚动窗口及分屏命令

tmux工具使用鼠标滚动窗口及分屏命令 1. tmux source配置文件 长期生效2. 临时生效3. 实现分屏 1. tmux source配置文件 长期生效 vim ~/.tmux.conf echo "set -g mouse on" > ~/.tmux.conf tmux source-file ~/.tmux.conf2. 临时生效 1. 进入到tmux命令窗口 2.…

必看!硬核科普!什么是冻干?可以当主食喂的猫咪冻干分享

冻干猫粮作为近年来备受推崇的高品质选择&#xff0c;吸引了越来越多养猫人的目光。有着丰富养猫经验的我&#xff0c;早已开始采用冻干喂养。新手养猫的人可能会对冻干猫粮感到陌生&#xff0c;并产生疑问&#xff1a;这到底是什么&#xff1f;猫咪冻干可以天天喂吗&#xff1…

如何在自己的电脑上添加静态路由

1.任务栏搜索powershell 选择以管理员身份运行 2.输入 route add -p (永久) 目的网络地址例如192.168.10.0 mask 255.255.255.0&#xff08;子网掩码&#xff09;192.168.20.1&#xff08;下一跳地址&#xff09;。回车即可生效

LeetCode刷题 | Day 1 最大子序列求和(Largest K Subsequence Sum)

LeetCode刷题 | Day 1 最大子序列求和(Largest K Subsequence Sum) 文章目录 LeetCode刷题 | Day 1 最大子序列求和(Largest K Subsequence Sum)前言一、题目概述二、解题方法2.1 贪心思路2.1.1 思路讲解2.1.2 伪代码 + 逐步输出示例2.1.3 Python代码如下2.1.4 C++代码如下…

用WebStorm和VS Code断点调试Vue

大家好&#xff0c;我是咕噜铁蛋&#xff01;。今天&#xff0c;我想和大家分享一下如何在WebStorm和VS Code这两款流行的开发工具中&#xff0c;使用断点调试Vue.js项目。Vue.js作为前端三大框架之一&#xff0c;以其轻量级和组件化的特性&#xff0c;受到了广大开发者的喜爱。…

客观评价一下GPT-4o

评价GPT-4o&#xff08;即OpenAI发布的升级版语言模型&#xff09;&#xff0c;以下是上大学网&#xff08;www.sdaxue.com&#xff09;从技术能力与创新性、性能与效率、功能实用性与用户体验等几个维度进行评价&#xff0c;不周之出&#xff0c;请大家指正。 技术能力与创新性…