K8S POD 启动探针 startupProbe 的使用

在这里插入图片描述

当我们启动一个POD 时, 当k8s detect 里面的容器启动成功时, 就会认为这个POD 启动完成了, 通常就会在状态里表示 ready 1/1 …

例如

root@k8s-master:~# kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
bq-api-demo   1/1     Running   0          34m

至于K8S 是怎么判断pod 是否启动完成的:

对于容器内没有设置探测规则的情况,默认的探测规则如下:

启动完成检测:Kubernetes将监视容器的启动状态。如果容器的进程启动并且不处于终止状态(例如,未崩溃),Kubernetes将认为该容器已启动完成。

就绪状态检测:在没有设置就绪探针的情况下,默认情况下,Kubernetes将假定容器处于就绪状态。这意味着在Pod调度到节点后,Kubernetes将立即将流量转发到该容器。

需要注意的是,这些默认规则可能不足以确保应用程序完全启动和可用。因此,强烈建议在Pod的配置文件(YAML)中设置适当的启动探针(startupProbe)和就绪探针(readinessProbe),以便更精确地确定Pod是否已启动完成和就绪,从而确保应用程序的可靠性和稳定性。

所以在生产环境上 我们有必要设置 startupProbe 来让k8s 正确判断pod 已经启动完成, 置于readinessProbe 不在本文讨论范围内。



构建2个api 判断程序是否启动完成

这里作为例子, 我们创建了两个api, 1个模拟成功, 1个模拟失败

模拟成功的api 我们直接用 /actuator/info

@Component
@Slf4j
public class AppVersionInfo implements InfoContributor {@Autowiredprivate Environment environment;@Value("${pom.version}") // https://stackoverflow.com/questions/3697449/retrieve-version-from-maven-pom-xml-in-codeprivate String appVersion;@Overridepublic void contribute(Info.Builder builder) {log.info("AppVersionInfo: contribute ...");builder.withDetail("app", "Sales API").withDetail("version", appVersion).withDetail("description", "This is a simple Spring Boot application to demonstrate the use of BigQuery in GCP.");}
}

模拟失败的api 我们自己写1个 /test/hello/fail

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {@GetMapping("/hello/fail")public ResponseEntity<ApiResponse<String>> getSalesDetails() {log.error("/test/hello/fail ... this api will already return 500 error");ApiResponse<String> response = new ApiResponse<>();response.setReturnCode(-1);response.setReturnMsg("this api will already return 500 error");return ResponseEntity.status(500).body(response);}
}



编辑pod yaml file

请留意startupProde 那一段的具体解释

apiVersion: v1 # api version
kind: Pod # type of this resource e.g. Pod/Deployment ..
metadata: name: bq-api-demolabels: pod-type: app # custom key valuepod-version: v1.0.1namespace: 'default'
spec: # detail descriptioncontainers: # key point- name: bq-api-service # custom nameimage: europe-west2-docker.pkg.dev/jason-hsbc/my-docker-repo/bq-api-service:1.1.1imagePullPolicy: IfNotPresent # try to use local image first, if no, then pull image from remotestartupProbe:httpGet: # Responses within the range of 200 to 399 code will be considered successfulpath: /actuator/infoport: 8080initialDelaySeconds: 20 # prode 20 seconds to the service before check the statup statusfailureThreshold: 3 # Only when there are three consecutive failed attempts, it is considered a startup failureperiodSeconds: 5 # Retry every 5 seconds (after a failure).timeoutSeconds: 5 # If the API does not return within 5 seconds, it is considered a failureports:- name: http8080containerPort: 8080 # the port used by the container serviceprotocol: TCPenv:- name: JVM_OPTSvalue: '-Xms128m -Xmx2048m'resources:requests: # at least need cpu: 1000m # 1000m = 1 corememory: 1000Mi limits: # at max can usecpu: 2000m memory: 2000MirestartPolicy: OnFailure



重新部署

pod_name=bq-api-demo
yaml_filename=bq-api-service-startup-probe.yaml
namespace=default# 删除指定 Pod
kubectl delete pod $pod_name -n $namespace# 等待 Pod 被删除并重新创建
echo "Waiting for the pod to be deleted..."
kubectl wait pod $pod_name --for=delete -n $namespace# 使用指定的 YAML 文件重新创建 Pod
kubectl create -f $yaml_filename -n $namespace

可以见到K8s 仍然可以detect pod 启动成功

root@k8s-master:~# kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
bq-api-demo   1/1     Running   0          34m

describe 一下:
的确描述了启动规则

root@k8s-master:~# kubectl describe pod bq-api-demo
...
Containers:bq-api-service:Container ID:   docker://15c666bd6e22e174d54ccf8757838a26d89a26562a21edca9174f8bcdb03fa90Image:          europe-west2-docker.pkg.dev/jason-hsbc/my-docker-repo/bq-api-service:1.1.1Image ID:       docker-pullable://europe-west2-docker.pkg.dev/jason-hsbc/my-docker-repo/bq-api-service@sha256:30fb2cebd2bf82863608037ce41048114c061acbf1182261a748dadefff2372fPort:           8080/TCPHost Port:      0/TCPState:          RunningStarted:      Sun, 17 Mar 2024 19:00:14 +0000Ready:          TrueRestart Count:  0Limits:cpu:     2memory:  2000MiRequests:cpu:     1memory:  1000MiStartup:   http-get http://:8080/actuator/info delay=20s timeout=5s period=5s #success=1 #failure=3Environment:JVM_OPTS:  -Xms128m -Xmx2048mMounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-j2bpc (ro)
...

看下log, 的确可以看出appVersionInfo的接口被调用了

root@k8s-master:~# kubectl logs bq-api-demo.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::               (v2.7.18)2024-03-17 19:00:15.371  INFO 1 --- [           main] com.home.Application                     : Starting Application v1.1.1 using Java 11.0.16 on bq-api-demo with PID 1 (/app/app.jar started by root in /app)
2024-03-17 19:00:15.375  INFO 1 --- [           main] com.home.Application                     : No active profile set, falling back to 1 default profile: "default"
2024-03-17 19:00:16.601  INFO 1 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2024-03-17 19:00:16.618  INFO 1 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2024-03-17 19:00:17.151  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2024-03-17 19:00:17.160  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-03-17 19:00:17.160  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.83]
2024-03-17 19:00:17.238  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-03-17 19:00:17.238  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1759 ms
2024-03-17 19:00:17.587  INFO 1 --- [           main] o.s.c.g.a.c.GcpContextAutoConfiguration  : The default project ID is jason-hsbc
2024-03-17 19:00:17.609  INFO 1 --- [           main] o.s.c.g.core.DefaultCredentialsProvider  : Default credentials provider for Google Compute Engine.
2024-03-17 19:00:17.609  INFO 1 --- [           main] o.s.c.g.core.DefaultCredentialsProvider  : Scopes in use by default credentials: [https://www.googleapis.com/auth/pubsub, https://www.googleapis.com/auth/spanner.admin, https://www.googleapis.com/auth/spanner.data, https://www.googleapis.com/auth/datastore, https://www.googleapis.com/auth/sqlservice.admin, https://www.googleapis.com/auth/devstorage.read_only, https://www.googleapis.com/auth/devstorage.read_write, https://www.googleapis.com/auth/cloudruntimeconfig, https://www.googleapis.com/auth/trace.append, https://www.googleapis.com/auth/cloud-platform, https://www.googleapis.com/auth/cloud-vision, https://www.googleapis.com/auth/bigquery, https://www.googleapis.com/auth/monitoring.write]
2024-03-17 19:00:17.704  INFO 1 --- [           main] com.home.api.config.MyInitializer        : Application started...
2024-03-17 19:00:17.705  INFO 1 --- [           main] com.home.api.config.MyInitializer        : https.proxyHost: null
2024-03-17 19:00:17.705  INFO 1 --- [           main] com.home.api.config.MyInitializer        : https.proxyPort: null
2024-03-17 19:00:18.370  INFO 1 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 4 endpoint(s) beneath base path '/actuator'
2024-03-17 19:00:18.510  INFO 1 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2024-03-17 19:00:18.510  INFO 1 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'application.errorChannel' has 1 subscriber(s).
2024-03-17 19:00:18.511  INFO 1 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started bean '_org.springframework.integration.errorLogger'
2024-03-17 19:00:18.547  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2024-03-17 19:00:18.562  INFO 1 --- [           main] com.home.Application                     : Started Application in 3.869 seconds (JVM running for 4.353)
2024-03-17 19:00:18.598  INFO 1 --- [           main] com.home.Application                     : customParam: null
2024-03-17 19:00:38.644  INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-03-17 19:00:38.644  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-03-17 19:00:38.646  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
2024-03-17 19:00:38.681  INFO 1 --- [nio-8080-exec-1] c.h.api.monitor.endpoint.AppVersionInfo  : AppVersionInfo: contribute ...



模拟失败的case

首先创建1个新的yaml file, 规则接口选择/test/hello/fail 这个接口的return code 永远是500

    startupProbe:httpGet: # Responses within the range of 200 to 399 code will be considered successfulpath: /test/hello/fail # alway return 500..port: 8080initialDelaySeconds: 20 # prode 20 seconds to the service before check the statup statusfailureThreshold: 3 # Only when there are three consecutive failed attempts, it is considered a startup failureperiodSeconds: 5 # Retry every 5 seconds (after a failure).timeoutSeconds: 5 # If the API does not return within 5 seconds, it is considered a failure

然后重新部署

root@k8s-master:~/k8s-s/pods# bash redeployPod.sh bq-api-demo bq-api-service-startup-probe-fail.yaml 
pod "bq-api-demo" deleted
Waiting for the pod to be deleted...
pod/bq-api-demo created

这次启动失败了 , 重试了3次

root@k8s-master:~# kubectl get pods -o wide
NAME          READY   STATUS    RESTARTS     AGE   IP            NODE        NOMINATED NODE   READINESS GATES
bq-api-demo   0/1     Running   3 (1s ago)   96s   10.244.3.16   k8s-node3   <none>           <none>

从下面的信息也知道是因为startup 接口return 了500

root@k8s-master:~# kubectl describe pod bq-api-demo
Name:         bq-api-demo
Namespace:    default
Priority:     0
Node:         k8s-node3/192.168.0.45
Start Time:   Sun, 17 Mar 2024 20:11:49 +0000
Labels:       pod-type=apppod-version=v1.0.1
Annotations:  <none>
Status:       Running
IP:           10.244.3.16
IPs:IP:  10.244.3.16
Containers:bq-api-service:Container ID:   docker://9a95ed5837917f3b527c8f65ec85cec17661ffa5e4ef4e4a6161b2c4cc2dc329Image:          europe-west2-docker.pkg.dev/jason-hsbc/my-docker-repo/bq-api-service:1.1.1Image ID:       docker-pullable://europe-west2-docker.pkg.dev/jason-hsbc/my-docker-repo/bq-api-service@sha256:30fb2cebd2bf82863608037ce41048114c061acbf1182261a748dadefff2372fPort:           8080/TCPHost Port:      0/TCPState:          RunningStarted:      Sun, 17 Mar 2024 20:11:50 +0000Ready:          FalseRestart Count:  0Limits:cpu:     2memory:  2000MiRequests:cpu:     1memory:  1000MiStartup:   http-get http://:8080/test/hello/fail delay=20s timeout=5s period=5s #success=1 #failure=3Environment:JVM_OPTS:  -Xms128m -Xmx2048mMounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-xf7gx (ro)
Conditions:Type              StatusInitialized       True Ready             False ContainersReady   False PodScheduled      True 
Volumes:kube-api-access-xf7gx:Type:                    Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds:  3607ConfigMapName:           kube-root-ca.crtConfigMapOptional:       <nil>DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:Type     Reason     Age               From               Message----     ------     ----              ----               -------Normal   Scheduled  35s               default-scheduler  Successfully assigned default/bq-api-demo to k8s-node3Normal   Pulled     34s               kubelet            Container image "europe-west2-docker.pkg.dev/jason-hsbc/my-docker-repo/bq-api-service:1.1.1" already present on machineNormal   Created    34s               kubelet            Created container bq-api-serviceNormal   Started    34s               kubelet            Started container bq-api-serviceWarning  Unhealthy  5s (x2 over 10s)  kubelet            Startup probe failed: HTTP probe failed with statuscode: 500

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

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

相关文章

数字创新的引擎:探索Web3的前沿科技和商业模式

随着数字化时代的不断发展&#xff0c;Web3作为下一代互联网的重要组成部分&#xff0c;正逐渐成为数字创新的引擎。本文将深入探讨Web3的前沿科技和商业模式&#xff0c;揭示其在数字创新领域的重要作用和潜力。 1. 区块链技术的革命性 Web3的核心是区块链技术&#xff0c;它…

Memcached-分布式内存对象缓存系统

目录 一、NoSQL 介绍 二、Memcached 1、Memcached 介绍 1.1 Memcached 概念 1.2 Memcached 特性 1.3 Memcached 和 Redis 区别 1.4 Memcached 工作机制 1.4.1 内存分配机制 1.4.2 懒惰期 Lazy Expiration 1.4.3 LRU&#xff08;最近最少使用算法&#xff09; 1.4.4…

Apache Doris 如何基于自增列满足高效字典编码等典型场景需求

自增列&#xff08;auto_increment&#xff09;是数据库中常见的一项功能&#xff0c;它提供一种方便高效的方式为行分配唯一标识符&#xff0c;极大简化数据管理的复杂性。当新行插入到表中时&#xff0c;数据库系统会自动选取自增序列中的下一个可用值&#xff0c;并将其分配…

以太坊开发学习-solidity(二)值类型

文章目录 第一个Solidity程序编译并部署代码变量值类型1. 布尔型2. 整型3. 地址类型4. 定长字节数组 第一个Solidity程序 开发工具&#xff1a;remix 本教程中&#xff0c;我会用remix来跑solidity合约。remix是以太坊官方推荐的智能合约开发IDE&#xff08;集成开发环境&#…

SpringBoot如何优雅实现远程调用

微服务之间的通信方式 常见的方式有两种&#xff1a; RPC——代表-dubbo HTTP——代表-SpringCloud 在SpringCloud中&#xff0c;默认是使用http来进行微服务的通信&#xff0c;最常用的实现形式有两种&#xff1a; RestTemplate Feign

【Spring 篇】走进Java NIO的奇妙世界:解锁高效IO操作的魔法

欢迎来到Java NIO的神奇之旅&#xff01;在这个充满活力的世界里&#xff0c;我们将一起揭示Java NIO&#xff08;New I/O&#xff09;的奥秘&#xff0c;探索其在高效IO操作中的神奇魔法。无需担心&#xff0c;即使你是Java的小白&#xff0c;也能轻松领略这个强大而灵活的IO框…

el-upload的多个文件与单个文件上传

样式图&#xff1a; 场景多个&#xff1a; 使用el-upload上传多个文件 <el-upload class"upload-demo" :action"uploadUrl" :on-remove"handleRemove1":on-success"handleAvatarSuccess1" multiple :limit"5" :on-exc…

高通 8255 基本通信(QUP)Android侧控制方法说明

一&#xff1a;整体说明 高通8255芯片中&#xff0c;SPI IIC UART核心统一由QUP V3 进行控制 QUP V3为可编程模块&#xff0c;可以将不同通道配置为SPI IIC UART通路&#xff0c;此部分配置在QNX侧 QUP 资源可以直接被QNX使用&#xff0c;Android侧可以通过两种方法使用QUP资源…

uniapp+vue3+setup语法糖开发微信小程序时不能定义globalData的解决方法

在使用 uniapp 开发小程序的时候&#xff0c; 发现使用了setup 语法糖 &#xff0c;定义 globalData 时&#xff0c;要不是定义不了&#xff0c; 要不就是使用 getApp()取不到&#xff0c;后来想到一个不伦不类的方法解决了&#xff0c; 这个方法有点难看&#xff0c; 但是解决…

WPF连接MySqldemo

界面总要管理数据嘛,于是便学习了一下WPF与MySql的基本连接. 运行结果: 环境配置 需要下载安装Mysql,网上教程很多,不详说,创建的工程需要下载或者引入相关的包(MySql.Data) 连接的部分直接看具体的代码即可 xaml代码(只放置了一个按钮和文本框) <Grid><Button x:Name…

mybatis-plus 的saveBatch性能分析

Mybatis-Plus 的批量保存saveBatch 性能分析 目录 Mybatis-Plus 的批量保存saveBatch 性能分析背景批量保存的使用方案循环插入使用PreparedStatement 预编译优点&#xff1a;缺点&#xff1a; Mybatis-Plus 的saveBatchMybatis-Plus实现真正的批量插入自定义sql注入器定义通用…

【C语言】猜数字游戏

代码如下&#xff1a; #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h> #include <time.h> void game() {int r rand() % 100 1;int guess 0;while (1){printf("请猜数字>:");scanf("%d", &guess…

【神经网络 基本知识整理】(激活函数) (梯度+梯度下降+梯度消失+梯度爆炸)

神经网络 基本知识整理 激活函数sigmoidtanhsoftmaxRelu 梯度梯度的物理含义梯度下降梯度消失and梯度爆炸 激活函数 我们知道神经网络中前一层与后面一层的连接可以用y wx b表示&#xff0c;这其实就是一个线性表达&#xff0c;即便模型有无数的隐藏层&#xff0c;简化后依旧…

【目标检测】YOLOv2 网络结构(darknet-19 作为 backbone)

上一篇文章主要是写了一些 YOLOv1 的原版网络结构&#xff0c;这篇文章一样&#xff0c;目标是还原论文中原版的 YOLOv2 的网络结构&#xff0c;而不是后续各种魔改的版本。 YOLOv2 和 YOLOv1 不一样&#xff0c;开始使用 Darknet-19 来作为 backbone 了。论文中给出了 Darkne…

springboot280基于WEB的旅游推荐系统设计与实现

旅游推荐系统设计与实现 传统办法管理信息首先需要花费的时间比较多&#xff0c;其次数据出错率比较高&#xff0c;而且对错误的数据进行更改也比较困难&#xff0c;最后&#xff0c;检索数据费事费力。因此&#xff0c;在计算机上安装旅游推荐系统软件来发挥其高效地信息处理…

5-隐藏层:神经网络为什么working

声明 本文章基于哔哩哔哩付费课程《小白也能听懂的人工智能原理》。仅供学习记录、分享&#xff0c;严禁他用&#xff01;&#xff01;如有侵权&#xff0c;请联系删除 目录 一、知识引入 &#xff08;一&#xff09;隐藏层 &#xff08;二&#xff09;泛化 &#xff08;三…

java算法题每日多道

274. H 指数 题目 给你一个整数数组 citations &#xff0c;其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数。 根据维基百科上 h 指数的定义&#xff1a;h 代表“高引用次数” &#xff0c;一名科研人员的 h 指数 是指他&#xff08;…

鸿蒙Harmony应用开发—ArkTS声明式开发(绘制组件:Ellipse)

椭圆绘制组件。 说明&#xff1a; 该组件从API Version 7开始支持。后续版本如有新增内容&#xff0c;则采用上角标单独标记该内容的起始版本。 子组件 无 接口 Ellipse(options?: {width?: string | number, height?: string | number}) 从API version 9开始&#xff0…

数据结构知识Day1

数据结构是什么&#xff1f; 数据结构是计算机存储、组织数据的方式&#xff0c;它涉及相互之间存在一种或多种特定关系的数据元素的集合。数据结构反映了数据的内部构成&#xff0c;即数据由哪些成分数据构成&#xff0c;以何种方式构成&#xff0c;以及呈现何种结构。这种结…

LeetCode讲解算法1-排序算法(Python版)

文章目录 一、引言问题提出 二、排序算法1.选择排序&#xff08;Selection Sort&#xff09;2.冒泡排序3.插入排序&#xff08;Insertion Sort&#xff09;4.希尔排序&#xff08;Shell Sort&#xff09;5.归并排序&#xff08;Merge Sort&#xff09;6.快速排序&#xff08;Qu…