Kubernetes---容器探针

⒈含义

  探针是由各个节点的kubelet对容器执行的定期诊断。要执行诊断,kubelet 调用由容器实现的Handler【处理程序】。有三种类型的处理程序:
    >ExecAction:在容器内执行指定命令。如果命令退出时返回码为0则认为诊断成功。
    >TCPSocketAction:对指定端口上的容器的IP地址进行TCP检查。如果端口打开,则诊断被认为是成功的。
    >HTTPGetAction:对指定的端口和路径上的容器的IP地址执行HTTPGet请求。如果响应的状态码大于等于200且小于400【2xx:成功,3xx:跳转】,则诊断被认为是成功的
  每次探测都将获得以下三种结果之一:
    >成功:容器通过了诊断。
    >失败:容器未通过诊断。
    >未知:诊断失败,因此不会采取任何行动【这将导致容器挂死,因为探针不执行成功的话,容器会一直在等待】

⒉探测方式

  livenessProbe【存活指针或叫存活探针】:指示容器是否正在运行。如果存活探测失败,则kubelet会杀死容器,并且容器将受到其重启策略的影响。如果容器不提供存活探针,则默认状态为Success

  readinessProbe【就绪指针或叫就绪探针】:指示容器是否准备好服务请求。如果就绪探测失败,端点控制器将从与Pod 匹配的所有Service的端点中删除该Pod的IP地址。初始延迟之前的就绪状态默认为Failure。如果容器不提供就绪探针,则默认状态为Success

⒊示例

   就绪指针 or 就绪探针

   HttpGet方式

readinessProbe-httpget 
apiVersion: v1 
kind: Pod 
metadata:
  name:readiness-httpget-pod 
  namespace: default 
spec:
  containers:
  - name: readiness-httpget-container 
    image: coreqi/myapp:v1 
    imagePullPolicy: IfNotPresent #镜像下载策略,如果存在则不下载
    readinessProbe: #就绪探针
      httpGet:  #检测方案
        port: 80
        path: /index1.html 
      initialDelaySeconds: 1 #初始化检测延时(1秒以后)
      periodSeconds: 3  #重试的检测时间(3秒以后)

  存活指针 or 存活探针

     Exec方式

apiVersion: v1 
kind: Pod 
metadata:
  name: liveness-exec-pod 
  namespace: default 
spec:
  containers: 
  - name: liveness-exec-container 
    image: hub.coreqi.cn/1ibrary/busybox 
    imagePullPolicy: IfNotPresent 
    command: ["/bin/sh","-c","touch /tmp/live;sleep 60;rm -rf /tmp/live;sleep 3600"] 
    livenessProbe:  #探活指针
      exec: #执行命令
        command: ["test","-e","/tmp/live"] #检测文件是否存在
      initialDelaySeconds: 1  #容器启动1秒后执行
      periodSeconds: 3  #重试的循环时间为3秒

  HttpGet方式

apiVersion: v1 
kind: Pod 
metadata:
  name: liveness-httpget-pod 
  namespace: default 
spec:
  containers: 
  - name: liveness-httpget-container 
    image: hub.coreqi.cn/1ibrary/myapp:v1 
    imagePu11Policy: IfNotPresent 
    ports:
    - name: http 
      containerPort: 80 
    1ivenessProbe:
      httpGet:
        port: http 
        path: /index.html 
      initialDelaySeconds: 1  #延迟1秒以后开始检测
      periodSeconds: 3  #每3秒重复检测一次
      timeoutSeconds: 10  #每次访问时最大的超时时间

  Tcp方式

apiVersion: v1 
kind: Pod 
metadata:
  name: probe-tcp 
spec:
  containers:
  - name: nginx 
    image: hub.coreqi.cn/1ibrary/myapp:v1 
    livenessProbe:
      initialDelaySeconds: 5 
      timeoutSeconds: 1 
      tcpSocket:
        port: 80
      periodSeconds: 3

 

posted @ 2019-09-20 10:27  SpringCore  阅读(752)  评论(0编辑  收藏  举报