This is the fourth day of my participation in Gwen Challenge

“This article has participated in the weekend learning plan, click to see details”

The probe

Probe is a periodic diagnosis of the container by Kubelet. Note that the probe targets the container, not the POD.

The probe can perform the following operations:

ExecAction

Executes the specified command in the container. If the command exits with a return code of 0, the command is considered successful

TCPSocketAction

The TCP check is performed on the IP+ port of the specified container, similar to Telnet. If the port is enabled, the TCP check succeeds

HTTPGetAction

If the HTTP response code is greater than or equal to 200 and less than 400, the GET request is considered successful

Each probe returns one of three results:

The container fails to pass the detection, that is, the detection itself fails and can be divided into the following two categories according to the detection mode

ReadinessProbe Ready probe indicating whether the container is ready for a service request. If it fails, the POD will be removed from all matching services. If the container does not provide a ready probe, Success is default. The ready state of the corresponding container.

LivenessProbe survival probe that indicates whether the container is running. If that fails, Kubelet kills the container and decides whether to restart it based on the restartPolicy. Succes defaults if the container does not provide a survival probe.

Probe instance operations The following example operations are performed for the two probes

The readinessProbe operation creates a POD with a ready probe using the yamL file test-Readiness -httpget. Yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-readiness-httpget
  namespace: default
  labels:
    app: myapp
    version: v1
spec:
  containers:
    - name: mynginx
      image: nginx
      imagePullPolicy: IfNotPresent
      readinessProbe:
        httpGet:
          port: 80
          path: /fake_index.html
        initialDelaySeconds: 1
        periodSeconds: 3

Copy the code

As you can see, the main container uses an Nginx image. If the return code for accessing pod_ip:80/fake_index.html is not between 200 and 400 then the container is not ready

Create the container and view its status

[root@k8s-master k8s-test]# kubectl apply -f test-readiness-httpget.yaml pod/test-readiness-httpget created [root@k8s-master k8s-test]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES curl -6bf6db5C4f-kljp4 1/1 Running 1 2d2h 10.244.1.2k8s-node1 <none> <none> HelloK8s 2/2 Running 0 31h 10.244.1.6 K8s-node1 <none> < None > test-init-main 1/1 Running 6 9h 10.244.1.8 k8S-node1 < None > <none> test-Readiness -httpget 0/1 Running 0 6s 10.244.1.10 k8s-node1 <none> <none>Copy the code

You can see that the POD state is running, but the container is not ready yet. Go inside to see the pod details

[root@k8s-master k8s-test]# kubectl describe pod test-readiness-httpget Name: test-readiness-httpget Namespace: Default Priority: 0 Node: k8s-node1/172.29.56.176 Start Time: Thu, 30 Apr 2020 18:48:41 +0800 Labels: app=myapp version=v1 Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"app":"myapp","version":"v1"},"name":"test-readin ess-httpget","name... Status: Running IP: 10.244.1.10 Containers: Mynginx: Container ID: docker://272b07aca3c7b9900f6fe91d7418c03315d5f079553c4108f20cd36aadc48f65 Image: nginx Image ID: docker-pullable://nginx@sha256:86ae264c3f4acb99b2dee4d0098c40cb8c46dcf9e1148f05d3a51c4df6758c12 Port: <none> Host Port: <none> State: Running Started: Thu, 30 Apr 2020 18:48:42 +0800 Ready: False Restart Count: 0 Readiness: http-get http://:80/fake_index.html delay=1s timeout=1s period=3s #success=1 #failure=3 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-hln8x (ro) Conditions: Type Status Initialized True Ready False ContainersReady False PodScheduled True Volumes: default-token-hln8x: Type: Secret (a volume populated by a Secret) SecretName: default-token-hln8x Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m33s default-scheduler Successfully assigned default/test-readiness-httpget to k8s-node1 Normal Pulled <invalid> kubelet, k8s-node1 Container image "nginx" already present on machine Normal Created <invalid> kubelet, k8s-node1 Created container mynginx Normal Started <invalid> kubelet, k8s-node1 Started container mynginx Warning Unhealthy <invalid> (x22 over <invalid>) kubelet, k8s-node1 Readiness probe failed: HTTP probe failed with statuscode: 404Copy the code

As you can see in the Events at the bottom, the ready probe failed because it returned 404, resulting in the container not being ready.

At this point, enter the container in the POD and artificially add an object file

[root@k8s-master k8s-test]# kubectl exec test-readiness-httpget -it -- /bin/bash
root@test-readiness-httpget:/# echo "hello fake index" > /usr/share/nginx/html/fake_index.html
root@test-readiness-httpget:/# exit
exit

Copy the code

Kubectl exec <pod_name> -c <container_name> it — if the pod contains only one container, omit the container name. Here you run bash inside the container and create an object file.

A second look shows that the container is ready, and you can also retrieve the contents of the file you just added