Introduction to the use of Kubernetes probes

Introduction to the use of Kubernetes probes

1. Basic introduction

When we run the application on K8s, we are more concerned about whether the application is running normally, but if we only check the running status of the application, it is difficult to judge whether the application is running; because at some point, The normal operation of the container does not mean that the application is healthy , so we can use the probe provided by Kubernetes.

Use probes to determine whether applications running in containers are running properly. official documentation


There are three types of probes for Kubernetes:

  • Readiness Probe: Determines whether the container is ready, if not, the container will be not ready.
  • Liveness Probe: Determines whether the application in the container is normal. If it is not normal, K8s will restart the container.
  • Startup Probe: Determine whether the application in the container has been started successfully (the readiness probe and survival probe will not be executed until the startup probe is successfully judged)

Probe method:

  • exec: By executing the specified command in the container, the status code returned when the command exits is judged. If it is 0, it means normal .
  • httpGet: Send a GET request to the container's IP address, port, and URL path; if the response status code is between 200 and 399, it means normal .
  • tcpSocket: Perform a TCP check on the container's IP address and specified port. If the port is open, it means normal .

Configuration item:

  • initialDelaySeconds: wait for the end start the probe check ;
  • periodSeconds: the interval ;
  • timeoutSeconds: The timeout , when it exceeds the time defined by us, it will be regarded as a failure;
  • successThreshold: The minimum ;
  • failureThreshold: the minimum ;

Below we use different probe methods for these three probes, mainly for the convenience of giving you a brief introduction, not a fixed way of writing; at the same time, these three probes can be used together; as a general comparison It is common to use the readiness probe and the survival probe together (the startup probe was added after the K8s 1.6 version)

2. Introduction to the use of K8s probes

1) Readiness probe:

[root@k8s-master01 ~]# vim tomcat-service.yaml
apiVersion: v1
kind: Pod
metadata:
  name: tomcat-service
spec:
  containers:
  - name: tomcat-service
    image: tomcat:8.5.32
    ports:
    - containerPort: 8080
    readinessProbe:
      failureThreshold: 3
      tcpSocket:
        port: 8080
      initialDelaySeconds: 20
      periodSeconds: 3
      successThreshold: 1
      timeoutSeconds: 2
[root@k8s-master01 ~]# kubectl create -f tomcat-service.yaml

insert image description here
We describecan view the information of the Pod through

[root@k8s-master01 ~]# kubectl describe pod tomcat-service

insert image description here
2) Survival probe:

[root@k8s-master01 ~]# vim tomcat-web-server.yaml
apiVersion: v1
kind: Pod
metadata:
  name: tomcat-web-server
spec:
  containers:
  - name: tomcat-web-server
    image: tomcat:8.5.32
    ports:
    - containerPort: 8080
    livenessProbe:
      failureThreshold: 3
      httpGet:
        path: /
        port: 8080
        scheme: HTTP						# 可以使用 HTTP  HTTPS 方式
      initialDelaySeconds: 20
      periodSeconds: 3
      successThreshold: 1
      timeoutSeconds: 2
[root@k8s-master01 ~]# kubectl create -f tomcat-web-server.yaml

insert image description here

  • The above restart is because when the survival probe or startup probe is unsuccessful, the container will be closed ; the restart strategy of the container will be involved later.
  • The default restart policy of the container is Always (that is, when the container exits, the container to be exited is restarted)

3) Start the probe:

[root@k8s-master01 ~]# vim tomcat-async-service.yaml
apiVersion: v1
kind: Pod
metadata:
  name: tomcat-async-service
spec:
  containers:
  - name: tomcat-async-service
    image: tomcat:8.5.32
    ports:
    - containerPort: 8080
    startupProbe:
      failureThreshold: 3
      exec:
        command: ['/bin/sh','-c','echo Hello World']
      initialDelaySeconds: 20
      periodSeconds: 3
      successThreshold: 1
      timeoutSeconds: 2
[root@k8s-master01 ~]# kubectl create -f tomcat-async-service.yaml

insert image description here

Guess you like

Origin blog.csdn.net/weixin_46902396/article/details/123379659