Today, I will continue to introduce the relevant knowledge of Linux operation and maintenance. The main content of this article is the actual application of Pod survivability probe.
In the above Kubernetes detailed explanation (16) - Pod container detection , we explained the probe of Pod container. Today, we are going to do the probes of the following Pod containers.
1. EXEC probe actual combat
First, we conduct EXEC probe combat, create a resource configuration list of liveness-probe.yaml, and write the following content:
apiVersion: v1
kind: Pod
metadata:
name: liveness-probe
namespace: default
labels:
probe: liveness
spec:
containers:
- name: liveness-probe-container
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/healthy;sleep 30;rm -rf /tmp/healthy","sleep 600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/healthy"]
initialDelaySeconds: 2
periodSeconds: 5
The completed resource configuration manifest file is as follows:
In the above configuration, we used the command's liveness probe, which will continuously detect the existence of the /tmp/healthy file. If the file does not exist, the probe will Exceptions will be detected. According to the configuration of the command in our image, after the container is started, the file will be deleted after running for 30 seconds, so the operation of the Pod will inevitably cause a probe exception. We only need to observe whether the Pod container is restarted after the probe is abnormal to determine whether the probe is working properly.
Then, we run the resource manifest configuration file, create the Pod container, and execute the command:
kubectl create -f liveness-probe.yaml
After the Pod container runs, it will look like this:
As you can see from the above figure, our Pod container runs successfully. Next, we wait for a while to test the effect of the probe.
After waiting for a period of time, the results of the Pod container are as follows:
As can be seen in the above figure, during the operation of the pod container, with the action of the command command, the probe detects that the container is abnormal, so Kubernetes will kill the container and restart it. Our command probe was configured successfully!
Second, the actual combat of HTTP probe
Next, we conduct HTTP probe combat, create a resource configuration list of http-probe.yaml, and write the following content:
apiVersion: v1
kind: Pod
metadata:
name: http-probe
namespace: default
labels:
probe: http
spec:
containers:
- name: http-probe
image: nginx:1.12
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
lifecycle:
postStart:
exec:
command: ["/bin/bash","-c","echo Http-Probe > /usr/share/nginx/html/ishealth.html"]
livenessProbe:
httpGet:
path: /ishealth.html
port: http
scheme: HTTP
The completed resource configuration manifest file is as follows:
In the above configuration, we define an HTTP probe, which will continuously detect the /ishealth.html file in the root directory of the website (this file is created by the command command in the container) ), if the file does not exist, the probe will detect the exception.
We run the resource manifest configuration file, create the Pod container, and execute the command:
kubectl create -f http-probe.yaml
After the container is running, we enter the container, delete the ishealth.html file, and observe the container status. The results are as follows:
It can be seen that when we delete the container, the probe detects an exception and causes the container to restart. Our HTTP probe was successful!
Three, TCP probe actual combat
Finally, we conduct the actual TCP probe combat, create a resource configuration list of tcp-probe.yaml, and write the following content:
apiVersion: v1
kind: Pod
metadata:
name: tcp-probe
namespace: default
labels:
probe: tcp
spec:
containers:
- name: tcp-rpobe
image: nginx:1.12
ports:
- name: http
containerPort: 80
livenessProbe:
tcpSocket:
port: http
The completed resource configuration manifest file is as follows:
In the above configuration, our probe will continuously probe port 80 of the container. If port 80 fails, the probe will detect an exception.
We run the resource manifest configuration file, create the Pod container, and execute the command:
kubectl create -f tcp-probe.yaml
Start the container.
After the container starts, we can execute the command:
kubectl describe pods/tcp-probe
Let's view the TCP probe settings of the Pod. The results are as follows:
From the above figure, we can also see the content of the TCP probe we configured.
Originality is not easy, please indicate the source for reprinting: https://blog.csdn.net/weixin_40228200