zoukankan      html  css  js  c++  java
  • kubernetes的三种探针startupprobe,ReadinessProbe,LivenessProbe记录

    kubernetes的三种探针

    startupprobe: k8s1.16版本后新加的探测方式,用于判断容器内应用程序是否已经启动,如果配置了startuprobe,就会先禁用其他的探测,直到它成功为止,成功后将不再进行探测。
    
    
    ReadinessProbe: 一般用于探测容器内的程序是否健康,它的返回值如果为success,那么就代表这个容器已经完成启动,并且程序已经是可以接受流量的状态.
    
    
    LivenessProbe:用于探测容器是否运行,如果探测失败,kubelet会根据配置的重启策略进行相应的处理,如果没有配置该探针,默认就是success!
    

    pod探针的检测方式

    startupProbe   启动检查
    livenessProbe  存活检查
    readinessProbe 就绪检查
    
    
    # startupProbe  启动检查
    ----------------------------------
    startupProbe:                     #健康检查方式:[readinessProbe,livenessProbe,StartupProbe]
      failureThreshold: 3             #检测失败3次表示未就绪
      httpGet:                        #请求方式
        path: /ready                  #请求路径
        port: 8182                    #请求端口
        scheme: HTTP                  #请求协议
      periodSeconds: 10               #检测间隔
      successThreshold: 1             #检查成功为2次表示就绪
      timeoutSeconds: 1               #检测失败1次表示未就绪
    ----------------------------------
    
    
    # livenessProbe 存活检查
    
    #案例1:
    ----------------------------------
    livenessProbe:                  #健康检查方式:[readinessProbe,livenessProbe,StartupProbe]
      failureThreshold: 5           #检测失败5次表示未就绪
      httpGet:                      #请求方式
        path: /health               #请求路径
        port: 8080                  #请求端口
        scheme: HTTP                #请求协议
      initialDelaySeconds: 60       #初始化时间
      periodSeconds: 10             #检测间隔
      successThreshold: 1           #检查成功为2次表示就绪
      timeoutSeconds: 5             #检测失败1次表示未就绪
      
      
    livenessProbe:                  #健康检查方式:[readinessProbe,livenessProbe,StartupProbe]
      failureThreshold: 5           #检测失败5次表示未就绪
      httpGet:                      #请求方式
        path: /health               #请求路径
        port: 8080                  #请求端口
      initialDelaySeconds: 60       #初始化时间
      periodSeconds: 10             #检测间隔
      successThreshold: 1           #检查成功为2次表示就绪
      timeoutSeconds: 5             #检测失败1次表示未就绪
      
    ----------------------------------
    
    案例2:
    ----------------------------------
    livenessProbe:
    httpGet:
      path: /healthz
      port: liveness-port
    failureThreshold: 1                
    periodSeconds: 60                  
    terminationGracePeriodSeconds: 60  #宽限时间,不能用于设置就绪态探针,它将被 API 服务器拒绝。
    ----------------------------------
    
    
    
    # readinessProbe  就绪检查
    ----------------------------------
    案例1[get方式]:
    readinessProbe:                   #健康检查方式:[readinessProbe,livenessProbe,StartupProbe]
      failureThreshold: 3             #检测失败3次表示未就绪
      httpGet:                        #请求方式
        path: /ready                  #请求路径
        port: 8181                    #请求端口
        scheme: HTTP                  #请求协议
      periodSeconds: 10               #检测间隔
      successThreshold: 1             #检查成功为2次表示就绪
      timeoutSeconds: 1               #检测失败1次表示未就绪
      
    案例2 [检查文件内容]:
    readinessProbe:                #检查方式
      exec:                        #使用命令检查
        command:                   #指令
        - cat                      #指令
        - /etc/hosts               #指令
      initialDelaySeconds: 5       #容器启动后要等待多少秒后存活和就绪探测器才被初始化,默认是 0 秒,最小值是 0。
      timeoutSeconds: 2            #检测失败1次表示未就绪
      successThreshold: 3          #检查成功为2次表示就绪
      failureThreshold: 2          #检测失败重试次数
      periodSeconds: 5             #检测间隔
    ----------------------------------
    
    initialDelaySeconds:容器启动后要等待多少秒后存活和就绪探测器才被初始化,默认是 0 秒,最小值是 0。
          periodSeconds:执行探测的时间间隔(单位是秒)。默认是 10 秒。最小值是 1。 
         timeoutSeconds:探测的超时后等待多少秒。默认值是 1 秒。最小值是 1。
       successThreshold:探测器在失败后,被视为成功的最小连续成功数。默认值是 1 存活和启动探测的这个值必须是1 最小值是 1
       failureThreshold:当探测失败时,Kubernetes 的重试次数。 存活探测情况下的放弃就意味着重新启动容器。 就绪探测情况下的放弃 Pod 会被打上未就绪的标签。默认值是 3。最小值是 1。
    
    
    #注意:
    配置了 startupProbe 之后,livenessProbe和readinessProbe参数将会被暂时禁用,直到程序被检测到启动完成了livenessProbe,readinessProbe才会被启用
    在程序启动较慢的时候可以配置startupProbe参数。
    

    启动案例

    StartupProbe案例[检测容器内进程是否完成启动]

    参考文档: https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/

    apiVersion: v1 # 必选,API的版本号
    kind: Pod       # 必选,类型Pod
    metadata:       # 必选,元数据
      name: nginx01   # 必选,符合RFC 1035规范的Pod名称
      labels:       # 可选,标签选择器,一般用于过滤和区分Pod
        app: nginx
        role: frontend # 可以写多个
      annotations:  # 可选,注释列表,可以写多个
        app: nginx
    spec:   # 必选,用于定义容器的详细信息
      containers:   # 必选,容器列表
      - name: nginx01 # 必选,符合RFC 1035规范的容器名称
        image: nginx:latest    # 必选,容器所用的镜像的地址
        imagePullPolicy: Always     # 可选,镜像拉取策略
        command: # 可选,容器启动执行的命令
        - nginx
        - -g
        - "daemon off;"
        workingDir: /usr/share/nginx/html       # 可选,容器的工作目录
        ports:  # 可选,容器需要暴露的端口号列表
        - name: http    # 端口名称
          containerPort: 80     # 端口号
          protocol: TCP # 端口协议,默认TCP
        env:    # 可选,环境变量配置列表
        - name: TZ      # 变量名
          value: Asia/Shanghai # 变量的值
        - name: LANG
          value: en_US.utf8
        startupProbe: # 可选,检测容器内进程是否完成启动。注意三种检查方式同时只能使用一种。
          httpGet:      # httpGet检测方式,生产环境建议使用httpGet实现接口级健康检查,健康检查由应用程序提供。
                path: /api/successStart # 检查路径
                port: 80
      restartPolicy: Always   # 可选,默认为Always
    
    root@k8s-master01[23:26:10]:~$ kubectl get pod
    NAME                 READY   STATUS    RESTARTS   AGE
    nginx-startupprobe   0/1     Running   1          79s
    
    创建后会无法启动,原因是无法检测到这个地址,通过日志可以看到:
    2021/06/25 23:26:02 [error] 7#7: *3 open() "/usr/share/nginx/html/api/successStart" failed (2: No such file or directory), client: 192.168.3.84, server: localhost, request: "GET /api/successStart HTTP/1.1", host: "172.17.125.25:80"
    

    ReadinessProbe案例 [可以提供服务的状态]

    参考文档: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/

    apiVersion: v1 # 必选,API的版本号
    kind: Pod       # 必选,类型Pod
    metadata:       # 必选,元数据
      name: nginx-read   # 必选,符合RFC 1035规范的Pod名称
      labels:       # 可选,标签选择器,一般用于过滤和区分Pod
        app: nginx
        role: frontend # 可以写多个
      annotations:  # 可选,注释列表,可以写多个
        app: nginx
    spec:   # 必选,用于定义容器的详细信息
      containers:   # 必选,容器列表
      - name: nginx-read # 必选,符合RFC 1035规范的容器名称
        image: nginx:latest    # 必选,容器所用的镜像的地址
        imagePullPolicy: Always     # 可选,镜像拉取策略
        command: # 可选,容器启动执行的命令
        - nginx
        - -g
        - "daemon off;"
        workingDir: /usr/share/nginx/html       # 可选,容器的工作目录
        ports:  # 可选,容器需要暴露的端口号列表
        - name: http    # 端口名称
          containerPort: 80     # 端口号
          protocol: TCP # 端口协议,默认TCP
        env:    # 可选,环境变量配置列表
        - name: TZ      # 变量名
          value: Asia/Shanghai # 变量的值
        - name: LANG
          value: en_US.utf8
        readinessProbe:
          httpGet:
                 path: /
                 port: 80
      restartPolicy: Always   # 可选,默认为Always
    
    kubectl apply -f  readinessProbe-pod.yaml 
    

    LivenessProbe检测容器中的应用是否正常运行

    参考文档:https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/

    apiVersion: v1 # 必选,API的版本号
    kind: Pod       # 必选,类型Pod
    metadata:       # 必选,元数据
      name: nginx-live   # 必选,符合RFC 1035规范的Pod名称
      labels:       # 可选,标签选择器,一般用于过滤和区分Pod
        app: nginx
        role: frontend # 可以写多个
      annotations:  # 可选,注释列表,可以写多个
        app: nginx
    spec:   # 必选,用于定义容器的详细信息
      containers:   # 必选,容器列表
      - name: nginx-live # 必选,符合RFC 1035规范的容器名称
        image: nginx:latest    # 必选,容器所用的镜像的地址
        imagePullPolicy: Always     # 可选,镜像拉取策略
        command: # 可选,容器启动执行的命令
        - nginx
        - -g
        - "daemon off;"
        workingDir: /usr/share/nginx/html       # 可选,容器的工作目录
        ports:  # 可选,容器需要暴露的端口号列表
        - name: http    # 端口名称
          containerPort: 80     # 端口号
          protocol: TCP # 端口协议,默认TCP
        env:    # 可选,环境变量配置列表
        - name: TZ      # 变量名
          value: Asia/Shanghai # 变量的值
        - name: LANG
          value: en_US.utf8
        livenessProbe:
          httpGet:
            path: /
            port: 80
    
    kubectl apply -f  livenessProbe.yaml
    
    #检查 nginx-live pod是否正常
    root@k8s-master01[23:41:31]:~$ kubectl get pod
    NAME                 READY   STATUS    RESTARTS   AGE
    nginx-live           1/1     Running   0          35s
    nginx-read           1/1     Running   0          10m
    nginx-startupprobe   0/1     Running   9          16m
    

    混合配置

    readinessProbe+livenessProbe案例

    apiVersion: v1 # 必选,API的版本号
    kind: Pod       # 必选,类型Pod
    metadata:       # 必选,元数据
      name: nginx-read   # 必选,符合RFC 1035规范的Pod名称
      labels:       # 可选,标签选择器,一般用于过滤和区分Pod
        app: nginx
        role: frontend # 可以写多个
      annotations:  # 可选,注释列表,可以写多个
        app: nginx
    spec:   # 必选,用于定义容器的详细信息
      containers:   # 必选,容器列表
      - name: nginx-read # 必选,符合RFC 1035规范的容器名称
        image: nginx:latest    # 必选,容器所用的镜像的地址
        imagePullPolicy: Always     # 可选,镜像拉取策略
        command: # 可选,容器启动执行的命令
        - nginx
        - -g
        - "daemon off;"
        workingDir: /usr/share/nginx/html       # 可选,容器的工作目录
        ports:  # 可选,容器需要暴露的端口号列表
        - name: http    # 端口名称
          containerPort: 80     # 端口号
          protocol: TCP # 端口协议,默认TCP
        env:    # 可选,环境变量配置列表
        - name: TZ      # 变量名
          value: Asia/Shanghai # 变量的值
        - name: LANG
          value: en_US.utf8
        readinessProbe:
          exec:
            command:
            - cat
            - /etc/hosts
          initialDelaySeconds: 5
          timeoutSeconds: 2
          successThreshold: 3
          failureThreshold: 2
          periodSeconds: 5
    
        livenessProbe:                  #健康检查方式:[readinessProbe,livenessProbe,StartupProbe]
          failureThreshold: 5           #检测失败5次表示未就绪
          httpGet:                      #请求方式
            path: /health               #请求路径
            port: 8080                  #请求端口
            scheme: HTTP                ##请求协议
          initialDelaySeconds: 60       #初始化时间
          periodSeconds: 10             #检测间隔
          successThreshold: 1           #检查成功为2次表示就绪
          timeoutSeconds: 5             #检测失败1次表示未就绪
    

    startupprobe+readinessProbe+ 混合案例

    apiVersion: v1                # 必选,API的版本号
    kind: Pod                     # 必选,类型Pod
    metadata:                     # 必选,元数据
      name: read-startup          # 必选,符合RFC 1035规范的Pod名称
      labels:                     # 可选,标签选择器,一般用于过滤和区分Pod
        app: nginx
        role: frontend             # 可以写多个
      annotations:                 # 可选,注释列表,可以写多个
        app: nginx
    spec:                           # 必选,用于定义容器的详细信息
      containers:                   # 必选,容器列表
      - name: read-startup          # 必选,符合RFC 1035规范的容器名称
        image: nginx:latest         # 必选,容器所用的镜像的地址
        imagePullPolicy: Always     # 可选,镜像拉取策略
        command: # 可选,容器启动执行的命令
        - nginx
        - -g
        - "daemon off;"
        workingDir: /usr/share/nginx/html       # 可选,容器的工作目录
        ports:                     # 可选,容器需要暴露的端口号列表
        - name: http               # 端口名称
          containerPort: 80        # 端口号
          protocol: TCP            # 端口协议,默认TCP
        env:                       # 可选,环境变量配置列表
        - name: TZ                 # 变量名
          value: Asia/Shanghai     # 变量的值
        - name: LANG
          value: en_US.utf8
        readinessProbe:
          exec:
            command:
            - cat
            - /etc/hosts
          initialDelaySeconds: 5
          timeoutSeconds: 2
          successThreshold: 3
          failureThreshold: 2
          periodSeconds: 5
    
        startupProbe:
          httpGet:
            path: /
            port: 80
          failureThreshold: 30
          periodSeconds: 10
    

    startupprobe+readinessProbe+ livenessProbe混合案例

    apiVersion: v1                # 必选,API的版本号
    kind: Pod                     # 必选,类型Pod
    metadata:                     # 必选,元数据
      name: read-startup          # 必选,符合RFC 1035规范的Pod名称
      labels:                     # 可选,标签选择器,一般用于过滤和区分Pod
        app: nginx
        role: frontend             # 可以写多个
      annotations:                 # 可选,注释列表,可以写多个
        app: nginx
    spec:                           # 必选,用于定义容器的详细信息
      containers:                   # 必选,容器列表
      - name: read-startup          # 必选,符合RFC 1035规范的容器名称
        image: nginx:latest         # 必选,容器所用的镜像的地址
        imagePullPolicy: Always     # 可选,镜像拉取策略
        command: # 可选,容器启动执行的命令
        - nginx
        - -g
        - "daemon off;"
        workingDir: /usr/share/nginx/html       # 可选,容器的工作目录
        ports:                     # 可选,容器需要暴露的端口号列表
        - name: http               # 端口名称
          containerPort: 80        # 端口号
          protocol: TCP            # 端口协议,默认TCP
        env:                       # 可选,环境变量配置列表
        - name: TZ                 # 变量名
          value: Asia/Shanghai     # 变量的值
        - name: LANG
          value: en_US.utf8
        readinessProbe:
          exec:
            command:
            - cat
            - /etc/hosts
          initialDelaySeconds: 5
          timeoutSeconds: 2
          successThreshold: 3
          failureThreshold: 2
          periodSeconds: 5
    
        startupProbe:
          httpGet:
            path: /
            port: 80
          failureThreshold: 30
          periodSeconds: 10
          
        livenessProbe:
          httpGet:
            path: /healthz
            port: 80
          failureThreshold: 1
          periodSeconds: 10  
    

    检测时间计算

    准确的时间计算:每次检查的间隔是10秒,最长超时时间是5秒,也就是单次检查应该是10 + 5 = 15秒(periodSeconds + timeoutSeconds),并不是10 * 5
    所以最长的重启时间为(10 + 5)* 5 
    (periodSeconds + timeoutSeconds) * failureThreshold
    
    此时又分为了两种情况:
    1.	首次启动时:最长重启时间需要加上initialDelaySeconds,因为需要等待initialDelaySeconds秒后才会执行健康检查。最长重启时间:(periodSeconds + timeoutSeconds) * failureThreshold + initialDelaySeconds
    2.	程序启动完成后:
        此时不需要计入initialDelaySeconds,最长重启时间:(periodSeconds + timeoutSeconds) * failureThreshold
    

    微信赞赏

    支付宝赞赏

  • 相关阅读:
    js四舍五入
    文本框只能输入整数,输入其他的自动不显示
    [转]关于C#程序部署到Android
    ajax在火狐中传中文出现乱码的解决方法
    Vue 记录 Cannot read property '_withTask' of undefined
    vs中 VMDebugger未能加载导致异常
    System.InvalidOperationException: 支持“XXX”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
    eclipse中将java项目转换成javaweb项目
    Android之SOAP协议与WebService服务器交互,解决超时的问题
    SymmetricDS 快速和灵活的数据库复制
  • 原文地址:https://www.cnblogs.com/superlinux/p/14933961.html
Copyright © 2011-2022 走看看