健康状态检测

[root@k8s-master01 ~]# kubectl  explain pods.spec.containers.lifecycle.postStart.tcpSocket   #健康主机的端口sock

livenessProbe 检测容器健康状态 一旦发生异常会重启容器
 许多应用程序经过长时间运行,最终过渡到无法运行的状态,除了重启,无法恢复。通常情况下,K8S会发现应用程序已经终止,然后重启应用程序/pod。

 有时应用程序可能因为某些原因(后端服务故障等)导致暂时无法对外提供服务,但应用软件没有终止,导致K8S无法隔离有故障的pod,调用者可能会访问到有故障的pod,导致业务不稳定。K8S提供livenessProbe来检测应用程序是否正常运行,并且对相应状况进行相应的补救措施。
[root@k8s-master01 ~]# kubectl  explain pods.spec.containers.livenessProbe

.
  exec 在用户容器内执行一次命令,如果命令执行的退出码为0,则认为应用程序正常运行,其他任务应用程序运行不正常。
  httpGet   调用容器内Web应用的web hook,如果返回的HTTP状态码在200和399之间,则认为应用程序正常运行,否则认为应用程序运行不正常。每进行一次HTTP健康检查都会访问一次指定的URL。
  initialDelaySeconds:容器启动后第一次执行探测是需要等待多少秒。
  periodSeconds: 执行探测的频率。默认是10秒,最小1秒。
  timeoutSeconds:探测超时时间。默认1秒,最小1秒。
  successThreshold:探测失败后,最少连续探测成功多少次才被认定为成功。默认是1。对于liveness必须是1。最小值是1。
  failureThreshold:探测成功后,最少连续探测失败多少次才被认定为失败。默认是3。最小值是1。
  TCPSocket: 将会尝试打开一个用户容器的Socket连接(就是IP地址:端口)。如果能够建立这条连接,则认为应用程序正常运行,否则认为应用程序运行不正常。

exec语法

[root@k8s-master01 ~]# cat busybox_LivenesProbe.yaml
   apiVersion: v1
   kind: Pod
   metadata:
     labels: 
      test: liveness-exec
     name: liveness-exec
   spec:
     containers:
     - name: liveness-demo
       image: busybox
       args:
       - /bin/sh
       - -c
       - touch /tmp/healthy; sleep 30;rm -fr /tmp/healthy;sleep 600
       livenessProbe:
        exec: 
         command:
         - test
         - -e
         - /tmp/healthy
 
	[root@k8s-master01 ~]# kubectl  describe  pods/liveness-exec	 

.
等待一段时间 发现重启了一次 状态发生过更改

随着时间更改 会重启多次

httpGet方式
[root@k8s-master01 ~]# cat  http_get_nginx.yaml 
      apiVersion: v1
      kind: Pod
      metadata:
        labels: 
          test: liveness
        name: liveness-httpget
        namespace: prod
      spec:
        containers:
        - name: liveness-nginx
          image: nginx
          ports: 
          - name: http
            containerPort: 80
          lifecycle:
            postStart:
              exec:
                command:
                - /bin/sh
                - -c
                - 'echo Healty > /usr/share/nginx/html/healthz'
          livenessProbe:
            httpGet:
             path: /healthz
             port: http
             scheme: HTTP
            periodSeconds: 2       #每隔2秒检测一次
            failureThreshold: 2    #失败2次 证明此节点是失败了
            timeoutSeconds: 2      #超时时间2秒 代表失败 
            initialDelaySeconds: 3 #容器启动开始延迟3秒开始检测
[root@k8s-master01 ~]# kubectl  apply -f http_get_nginx.yaml
[root@k8s-master01 ~]# kubectl  get pods -n prod
[root@k8s-master01 ~]# kubectl  get pods -n prod -w
	 -w 等同于shell的 watch
	打印详细信息
	[root@k8s-master01 ~]# kubectl  describe pods -n prod

.
 手动使其检测失败
 [root@k8s-master01 ~]# kubectl  exec -it liveness-httpget -n prod  -- /bin/bash
 root@liveness-httpget:/# cd /usr/share/nginx/html/
 root@liveness-httpget:/usr/share/nginx/html# mv healthz  healthz_back
	 再次查看详细信息
[root@k8s-master01 ~]# kubectl  describe pods -n prod

会发现又重启了一次,在等登录查看 发现healthz 再次生成,并且值为:Healty

httpGet方式参数
[root@k8s-master01 ~]# kubectl  explain pods.spec.containers.livenessProbe.httpGet
   host               #要连接的主机名,默认为pod IP,配合httpHeaders使用
   httpHeaders   #要在请求中设置的自定义头。HTTP允许重复报头。 比如post 请求
   path              #访问HTTP服务器的路径
   port              #要在容器上访问的端口的名称或编号。数字必须在范围1到65535。名称必须是IANA_SVC_NAME
   scheme  用于连接到主机的方案。默认为HTTP

容器就绪检测 readinessProbe

检测容器是否启动完毕  不具有重启容器的权限,如果容器启动完毕 但是容器内服务没有就绪,会删掉移除此容器
  [root@k8s-master01 ~]# kubectl  explain pods.spec.containers.readinessProbe	
	语法使用方式参考:https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probe
exec	<Object>
failureThreshold	<integer>
httpGet	<Object>
initialDelaySeconds	<integer>
periodSeconds	<integer>
successThreshold	<integer>   探测失败后,最少连续探测成功多少次才被认定为成功。默认是1,最小值为1。
tcpSocket	<Object>
timeoutSeconds	<integer>

例子

[root@k8s-master01 ~]# vim readinessProbe_nginx.yaml
	
   apiVersion: v1
   kind: Pod
   metadata:
     labels:
       test: readinessProbe
     name: readinessprobe-exec
     namespace: prod
   spec:
     containers:
     - name: readiness-probe-busybox
       image: busybox
       args: ["/bin/sh","-c","while true; do rm -f /tmp/ready; sleep 30; touch /tmp/ready;sleep 300; done"]
       readinessProbe:
         exec:
           command: ["test","-e","/tmp/ready"]
         initialDelaySeconds: 5
         periodSeconds: 

.
上面的图可以看出 虽然容器已经启动了,但是容器里面的服务器还是没有启动状态, 每隔5秒检测一次 检测5次,当这个文件存在的时候 容器和服务启动完毕

你可人为的连接容器 删除 /tmp/ready 此文件 会发现容器会处于 未就绪状态

手动创建此文件