k8s学习笔记(一) 存活探针与就绪探针的行为解析

前言

Pod是kubernetes中应用程序执行的基本单元,它是kubernetes对象模型中创建或部署的最小和最简单的单元。一个Pod可以包括一个或多个容器,当一个Pod包含多个容器时,这些容器总是运行于同一个工作节点上,一个Pod绝不会跨越多个工作节点。

存活探针与就绪探针是检测应用是否存活及是否做好流量准备的重要手段。

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

存活探针 livenessProbe

存活探针检测应用是否存活。当存活探针检测到容器非存活状态时,会重启容器。

探针主要分为三种,exec,httpGet,tcpSocket。

exec

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness
    image: k8s.gcr.io/busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5

httpGet

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-http
spec:
  containers:
  - name: liveness
    image: k8s.gcr.io/liveness
    args:
    - /server
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
        httpHeaders:
        - name: Custom-Header
          value: Awesome
      initialDelaySeconds: 3
      periodSeconds: 3

tcpSocket

apiVersion: v1
kind: Pod
metadata:
  name: goproxy
  labels:
    app: goproxy
spec:
  containers:
  - name: goproxy
    image: k8s.gcr.io/goproxy:0.1
    ports:
    - containerPort: 8080
    readinessProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 10
    livenessProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 15
      periodSeconds: 20

就绪探针 readinessProbe

就绪探针检测应用是否就绪,即是否有能力开始接受请求流量。当就绪探针检测到容器非就绪状态时,会将容器标记为非ready状态,并从service的负载均衡器中移除。

混合使用

存活探针和就绪探针可以混合使用。

使用以下yaml代码定义liveness-readiness-pod-test。

apiVersion: v1
kind: Pod
metadata: 
  name: liveness-readiness-pod-test
  namespace: default
spec:
  containers:
  - name: liveness-readiness-container
    image:  nginx:1.16.1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    livenessProbe: 
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
      timeoutSeconds: 10
    readinessProbe:
      httpGet:
        port: http
        path: /index1.html
      initialDelaySeconds: 1
      timeoutSeconds: 10

存活探针检测默认的index.html,就绪探针检测待添加的index1.html。启动yaml文件后,由于index1.html文件不存在,就绪探针不通过。

在这里插入图片描述

运行以下命令

kubectl exec liveness-readiness-pod-test -it -- /bin/bash
echo "123" >> /usr/share/nginx/html/index1.html

就绪探针通过,程序进入ready状态。

在这里插入图片描述

运行以下命令

kubectl exec liveness-readiness-pod-test -it -- /bin/bash
rm   /usr/share/nginx/html/index.html

等待几秒后,状态变为not ready

在这里插入图片描述

这是因为存活探针检测index.html是否存在,失败后会重启应用,导致就绪探针需要的index1.html文件被重置,无法进入ready状态。

以上状态的变化涵盖了探针在容器生命周期中的主要用途。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值