k8s-pod Health check

1 The most basic yaml file

Here's a basic list of resources , Each field must exist

metadata Below name yes pod Name ,labels Is the label

spec, Below name It's the name of the container ,image Is the image address of the download

2 initc The role of

  • init Containers always run until successful completion

  • every last init All containers must be in the next init Successfully completed before the container started

2.1 init Use cases

 

apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox
command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2;
done;']
- name: init-mydb
image: busybox
command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']

As shown in the figure

 

Create two service, Let's go to the top initc testing

kind: Service
apiVersion: v1
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
---
kind: Service
apiVersion: v1
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377

As shown in the figure ,

3 Probe detection

3.1 Two types of detection

  • LivenessProbe: Used to determine if the container is alive (running state ), If LivenessProbe The probe detected an unhealthy container , be kubelet Kill the container , And according to the container restart policy to do the corresponding processing . If a container does not contain LivenessProbe probe , be kubelet Think of the container LivenessProbe The value returned by the probe is always “Success”.

 

  • ReadinessProbe: To determine whether the container is started and completed (ready state ), Can receive requests . If ReadinessProbe The probe detected a failure , be Pod The state of is modified .Endpoint Controller Will be taken from Service Of Endpoint Deletes where the container is contained Pod Of Endpoint.

3.2 Three detection methods

  • 1.HTTP GET Probe to container ip Address ( Specify the port and path ) perform HTTP GET request . Response status code is 2xx or 3xx Then the detection is successful .

  • 2.TCP The socket probe tries to establish TCP Connect , Success builds success .

  • 3.Exec probe , Execute any command in the container , Check the exit status code of the command , yes 0 The successful , Other failures

 

3.3 Detection probe - Readiness test

readinessProbe-httpget

 

apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
  namespace: default
spec:
  containers:
  - name: readiness-httpget-container
    image: wangyanglinux/myapp:v1
    imagePullPolicy: IfNotPresent
    readinessProbe:
      httpGet:
        port: 80
        path: /index1.html
      initialDelaySeconds: 1 # Delay 1 second , Container start up 1 Seconds later , testing
      periodSeconds: 3 # cycle , every other 3 Seconds to re detect 

 

3.4 Detection probe - Survival tests

livenessProbe-exec

 

apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
  namespace: default
spec:
  containers:
  - name: liveness-exec-container
    image: hub.atguigu.com/library/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
      periodSeconds: 3

livenessProbe-httpget

 

apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
  - name: liveness-httpget-container
    image: hub.atguigu.com/library/myapp:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    livenessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
      timeoutSeconds: 10 # Timeout time 

livenessProbe-tcp

 

apiVersion: v1
kind: Pod
metadata:
  name: probe-tcp
spec:
  containers:
  - name: nginx
    image: hub.atguigu.com/library/myapp:v1
    livenessProbe:
      initialDelaySeconds: 5
      timeoutSeconds: 1
      tcpSocket:
        port: 80

 

3.5 Actions after startup and exit

 

apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
  image: nginx
  lifecycle: # Life cycle
    postStart:
      exec:
        command: ["/bin/sh", "-c", "echo Hello from the postStart handler >
/usr/share/message"]
    preStop:
      exec:
        command: ["/bin/sh", "-c", "echo Hello from the postStart handler >