• k8s--基础--12.6--pod--生命周期--案例


    k8s–基础–12.6–pod–生命周期–案例


    1、livenessProbe

    1.1、介绍

    1. 容器存活探针
    2. 判断容器是否存活。
      1. 如果存活探测失败
        1. kubelet 会杀死容器,并且容器将受到其重启策略的影响。
    3. 如果容器不提供存活探针,则默认状态为Success。

    1.2、查看定义

    kubectl explain pods.spec.containers.livenessProbe
    
    
    • 1
    • 2

    1.3、参数

    1.3.1、exec

    通过 指令方式 检查容器是否健康

    1.3.2、httpGet

    通过 http请求 检查容器是否健康

    1.3.3、tcpSocket

    通过 tcp请求 检查容器是否健康

    1.3.4、failureThreshold

    表示探测失败次数,默认是3,探测3次失败,才认为是真失败了;

    1.3.5、successThreshold

    表示探测成功次数,默认是1,探测1次成功,才认为是真成功了;

    1.3.6、initialDelaySeconds

    默认是容器一启动就开始探测,但是此时容器可能还没启动完,此时探测肯定是失败的,
    所以initialDelaySeconds表示容器启动多长时间后才开始探测.

    1.3.7、timeoutSeconds

    1. 探测多少秒后,就认为探测超时
    2. 默认为1秒。

    1.3.8、periodSeconds

    1. 探测间隔
    2. 单位为秒
    3. 默认10s探测一次

    1.4、案例:检查容器是否健康

    1.4.1、通过 指令方式

    在容器内部执行一个命令,如果返回码为0,则表示健康

    步骤01、创建脚本

    vim /root/test/livd_pod.yaml
    
    • 1

    内容

      
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod
      namespace: default
      labels:
        nginx: test
    spec:
      containers:
      - name: nginx1
        image: nginx
        imagePullPolicy: IfNotPresent
    	# 容器启动的时候,创建healthy.txt文件,休眠30秒后,删除healthy.txt文件,再休眠3600秒
        command: ["/bin/sh","-c","touch /tmp/healthy.txt; sleep 30; rm -f /tmp/healthy.txt; sleep 3600"]
    	# 健康检查
        livenessProbe:
          exec:
    	   # 如果healthy.txt文件存在,表示健康
    	   command: ["test","-e","/tmp/healthy.txt"] 
          initialDelaySeconds: 15
          timeoutSeconds: 1
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    步骤02、部署

    kubectl apply -f /root/test/livd_pod.yaml
    kubectl get pods
    
    • 1
    • 2

    在这里插入图片描述

    步骤03、查看

    
    kubectl describe  pods  nginx-pod
    
     
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    1.4.2、通过 http请求

    发送一个http Get请求,如果返回状态吗在200-400之间则表示健康

    
    
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod
      namespace: default
      labels:
        nginx: test
    spec:
      containers:
      - name: nginx1
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - name: http-port
          containerPort: 80
        # 健康检查
        livenessProbe:
          httpGet:
            # 请求不存在路径
            path: 127.0.0.1/index2.html
            port: http-port
          initialDelaySeconds: 15
          timeoutSeconds: 1
     
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述

    1.4.3、通过 tcp请求

    通过IP 和port ,如果能够和容器建立连接则表示容器健康

    
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod
      namespace: default
      labels:
        nginx: test
    spec:
      containers:
      - name: nginx1
        image: nginx
        imagePullPolicy: IfNotPresent
        livenessProbe:
          tcpSocket:
            host: 127.0.0.1
            port: 80
          initialDelaySeconds: 15
          timeoutSeconds: 1
     
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    2、readinessProbe

    2.1、介绍

    1. 容器就绪探针,用于判断容器是否启动完成。
      1. 如果就绪探测失败,端点控制器将从与 Pod 匹配的所有Service 的端点中删除该 Pod 的 IP 地址。
    2. 初始延迟之前的就绪状态默认为Failure。
    3. 如果容器不提供就绪探针,则默认状态为Success

    2.2、定义

     
    # readinessProbe定义
    kubectl explain pods.spec.containers.readinessProbe
    
    
    • 1
    • 2
    • 3
    • 4

    2.3、参数

    参考1.3

    2.4、案例

    参考1.4

    3、主容器钩子

    1. 主容器启动后钩子(postStart)
    2. 容器结束前钩子(preStop)

    3.1、查看定义

    # postStart定义
    kubectl explain  pods.spec.containers.lifecycle.postStart
    
    # preStop定义
    kubectl explain  pods.spec.containers.lifecycle.preStop
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.2、参数和案例

    1. 2个钩子的参数是一样的
    2. 这里就已postStart为案例。

    3.2.1、exec

    通过 指令方式 来执行钩子

    步骤01、创建脚本

    vim /root/test/lifecycle_pod.yaml
    
    • 1

    内容

    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod
      namespace: default
      labels:
        nginx: test
    spec:
      containers:
      - name: nginx1
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - name: http-port
          containerPort: 80
        # 钩子
        lifecycle:
           postStart:
                exec:
                  # 启动后创建一个healthy.txt文件
                  command: ["/bin/sh","-c","touch /tmp/healthy.txt"]
     
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    步骤02、部署

    kubectl apply -f /root/test/lifecycle_pod.yaml
    kubectl describe  pods  nginx-pod
    kubectl get pods
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    步骤03、查看

    
    # 进入容器
    kubectl exec  -it nginx-pod  -c  nginx1 -- /bin/sh
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    3.2.2、httpGet

    通过 http请求 来执行钩子
    参考:1.4.2

    3.2.3、tcpSocket

    通过 tcp请求 来执行钩子

    参考:1.4.3

  • 相关阅读:
    Spring概述
    数据库迁移-国产化-迁移建议-GreenPlum DB向GBase 8a 迁移
    【毕业设计】基于单片机的智能温控农业大棚系统 - 物联网 stm32
    常用概念-分布式系统
    985 博士真的会舍弃华为年薪接近 100 万 offer,去选择年薪 20 万的公务员吗?
    数据可视化项目(二)
    如何使用邮件和电话报-大数据培训
    JavaSE 第六章 面向对象基础-中(封装)
    Android解锁
    揭秘得物客服IM全链路通信过程
  • 原文地址:https://blog.csdn.net/zhou920786312/article/details/126232345