(三)k8s集群pod存活性和就绪性探测
文章目录1 livenessProbe存活性探测1.1 exec探针1.2 httpGet探针2 readinessProbe就绪性探测1 livenessProbe存活性探测liveness存活性探测,可以通过我们自定义的命令或者一些内置的探测方法来探测pod中容器是否存活,如若探测到其状态或属性不符合我们定义的内容,那么就会使该容器重启来使之恢复正常。[root@master1 mani...
·
1 livenessProbe存活性探测
liveness存活性探测,可以通过我们自定义的命令或者一些内置的探测方法来探测pod中容器是否存活,如若探测到其状态或属性不符合我们定义的内容,那么就会使该容器重启来使之恢复正常。
[root@master1 manifests]# kubectl explain pods.spec.containers.livenessProbe
KIND: Pod
VERSION: v1
RESOURCE: livenessProbe <Object>
DESCRIPTION:
Periodic probe of container liveness. Container will be restarted if the
probe fails. Cannot be updated. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
Probe describes a health check to be performed against a container to
determine whether it is alive or ready to receive traffic.
FIELDS:
exec <Object>
One and only one of the following should be specified. Exec specifies the
action to take.
failureThreshold <integer>
Minimum consecutive failures for the probe to be considered failed after
having succeeded. Defaults to 3. Minimum value is 1.
httpGet <Object>
HTTPGet specifies the http request to perform.
initialDelaySeconds <integer> 初始化探测,指定初始化时间
Number of seconds after the container has started before liveness probes
are initiated. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
periodSeconds <integer>
How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
value is 1.
successThreshold <integer>
Minimum consecutive successes for the probe to be considered successful
after having failed. Defaults to 1. Must be 1 for liveness. Minimum value
is 1.
tcpSocket <Object>
TCPSocket specifies an action involving a TCP port. TCP hooks not yet
supported
timeoutSeconds <integer>
Number of seconds after which the probe times out. Defaults to 1 second.
Minimum value is 1. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
1.1 exec探针
顾名思义,即可以指定命令,来定义我们要探测的命令
[root@master1 manifests]# cat liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-container
image: busybox:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/healthy"]
initialDelaySeconds: 2
periodSeconds: 3
[root@master1 manifests]# kubectl create -f liveness-exec.yaml
[root@master1 manifests]# kubectl get pods liveness-exec-pod
NAME READY STATUS RESTARTS AGE
liveness-exec-pod 1/1 Running 0 7s
[root@master1 manifests]# kubectl get pods liveness-exec-pod
NAME READY STATUS RESTARTS AGE
liveness-exec-pod 1/1 Running 0 17s
[root@master1 manifests]# kubectl get pods liveness-exec-pod
NAME READY STATUS RESTARTS AGE
liveness-exec-pod 1/1 Running 1 70s
[root@master1 manifests]# kubectl get pods liveness-exec-pod
NAME READY STATUS RESTARTS AGE
liveness-exec-pod 1/1 Running 2 2m38s
通过不停get pods,可以发现该pod会不断重启容器使之工作正常
1.2 httpGet探针
通过指定http端口及路径,来探测该服务是否处于正常状态
[root@master1 manifests]# cat liveness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: nginx:1.14-alpine
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 2
periodSeconds: 3
periodSeconds
:代表每次探测时间间隔initialDelaySeconds
:代表初始化延迟时间,即在一个容器启动后如果直接开始探测那么很有可能会直接探测失败,需要给一个系统初始化的时间
[root@master1 manifests]# kubectl create -f liveness-httpget.yaml
[root@master1 manifests]# kubectl get pods liveness-httpget-pod
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 0 8s
# 手动删除index页面查看状态,会发现pod被重启,然后index页面又被创建
[root@master1 manifests]# kubectl exec -it liveness-httpget-pod -- /bin/sh
/ # rm -f /usr/share/nginx/html/index.html
/ #
[root@master1 manifests]# kubectl describe pods liveness-httpget-pod
......
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Fri, 21 Dec 2018 10:11:58 +0800
Finished: Fri, 21 Dec 2018 10:14:12 +0800
Ready: True
Restart Count: 1
......
[root@master1 manifests]# kubectl get pods liveness-httpget-pod
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 1 3m21s
[root@master1 manifests]# kubectl exec -it liveness-httpget-pod -- /bin/sh
/ # ls /usr/share/nginx/html/
50x.html index.html
/ # exit
2 readinessProbe就绪性探测
当一个pod中的容器被创建后,正常工作情况下状态会转变至Running
,在kubectl get pods
的READY
列的左侧
数字会加1,代表该容器已然就绪,然实际上并不一定就绪,比如一个java服务,服务启动并运行起来可能需要一些时间,若在这个时间段内service把调度请求发给此pod的容器,这个容器又不能响应,就会存在问题,这个时候就要进行就绪性探测,来探测服务是否正常,正常之后kubectl get pods的READY状态左侧数字才会加1,此容器才会被service正常调度。
[root@master1 manifests]# kubectl explain pods.spec.containers.readinessProbe
KIND: Pod
VERSION: v1
RESOURCE: readinessProbe <Object>
DESCRIPTION:
Periodic probe of container service readiness. Container will be removed
from service endpoints if the probe fails. Cannot be updated. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
Probe describes a health check to be performed against a container to
determine whether it is alive or ready to receive traffic.
FIELDS:
exec <Object>
One and only one of the following should be specified. Exec specifies the
action to take.
failureThreshold <integer>
Minimum consecutive failures for the probe to be considered failed after
having succeeded. Defaults to 3. Minimum value is 1.
httpGet <Object>
HTTPGet specifies the http request to perform.
initialDelaySeconds <integer>
Number of seconds after the container has started before liveness probes
are initiated. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
periodSeconds <integer>
How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
value is 1.
successThreshold <integer>
Minimum consecutive successes for the probe to be considered successful
after having failed. Defaults to 1. Must be 1 for liveness. Minimum value
is 1.
tcpSocket <Object>
TCPSocket specifies an action involving a TCP port. TCP hooks not yet
supported
timeoutSeconds <integer>
Number of seconds after which the probe times out. Defaults to 1 second.
Minimum value is 1. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
[root@master1 manifests]# vim rediness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
namespace: default
spec:
containers:
- name: readiness-httpget-container
image: nginx:1.14-alpine
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
[root@master1 manifests]# kubectl create -f rediness-httpget.yaml
pod/readiness-httpget-pod created
# 现在已然处于就绪状态
[root@master1 manifests]# kubectl get pods readiness-httpget-pod
NAME READY STATUS RESTARTS AGE
readiness-httpget-pod 1/1 Running 0 35s
# 通过删除和创建index文件来观察pod就绪性状态
[root@master1 manifests]# kubectl exec -it readiness-httpget-pod -- /bin/sh
/ # rm -f /usr/share/nginx/html/index.html 删除index.html文件
/ # echo "hi" >> /usr/share/nginx/html/index.html 创建index.html文件
# 实时观察readiness-httpget-pod就绪性
[root@master1 manifests]# kubectl get pods readiness-httpget-pod -w
NAME READY STATUS RESTARTS AGE
readiness-httpget-pod 0/1 Running 0 117s
readiness-httpget-pod 1/1 Running 0 2m27s
在进行存活性和就绪性探测的时候,每次只需要定义其中一个探针即可
更多推荐
已为社区贡献13条内容
所有评论(0)