POD-2高级用法
容器探测详解
所谓容器探测就是我们在里面设置了一些探针,或者传感器来获取相应的数据用来判断容器存活与否或者就绪与否的标准;
目前k8s支持的存活性探测方式和就绪性探测方式都是一样的,探针类型有三种:
ExecAction:
TCPSocketAction:
HTTPGetAction:
如果探针是针对容器存活性检测的,就是容器存活性探针
如果探针是针对容器就绪状态检测的,就是融容器就绪性探针
kubectl explain pods.spec.containers
可以看到如下:
livenessProbe(容器存活性探针):
readinessProbe (容器就绪性探针)
lifecycle(容器生命周期探针):主要是用来定义容器启动后和结束前的钩子的
kubectl explain pods.spec.containers.livenessProbe
kubectl explain pods.spec.containers.readinessProbe
livenessProbe <Object>
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
readinessProbe <Object>
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
lifecycle <Object>
Actions that the management system should take in response to container lifecycle events. Cannot be updated.
(1)livenessProbe定义详解:
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
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
periodSeconds <integer>
How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
value is 1.
successThreshold <integer>
认为探测成功的最小连续成功次数
失败之后。默认值为1。必须为1才能保持活力。最小值
为1。
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-
(2)readnessProbe定义详解:
kubectl explain pods.spec.containers.readinessProbe
KIND: Pod
VERSION: v1
RESOURCE: readinessProbe <Object>
DESCRIPTION:
FIELDS:
exec <Object>
failureThreshold <integer>
我们探测几次都失败了,才认为是失败的,我们不能够一锤定音,这样会导致误伤的,因此需要探测多次,默认是3次都探测失败,才认为是失败的
httpGet <Object>
initialDelaySeconds <integer>
初始化时的延迟等待时间,
periodSeconds <integer>
探测周期
successThreshold <integer>
认为探测成功的最小连续成功次数
失败之后。默认值为1。必须为1才能保持活力。最小值
为1。
tcpSocket <Object>
timeoutSeconds <integer>
每一次探测超时时间多长,
(3)查看livenessProbe下的exec用法
kubectl explain pods.spec.containers.livenessProbe.exec
KIND: Pod
VERSION: v1
RESOURCE: exec <Object>
DESCRIPTION:
One and only one of the following should be specified. Exec specifies the
action to take.
ExecAction describes a "run in container" action.
FIELDS:
command <[]string>
Command is the command line to execute inside the container, the working
directory for the command is root ('/') in the container's filesystem. The
command is simply exec'd, it is not run inside a shell, so traditional
shell instructions ('|', etc) won't work. To use a shell, you need to
explicitly call out to that shell. Exit status of 0 is treated as
live/healthy and non-zero is unhealthy.
例一:写一个简单的pod实例
健康探测使用的探针是exec
[root@master k8s-pod]# cat pod-liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
labels:
test: liveness-exec-pod-busybox
spec:
containers:
- name: liveness-exec-containers
image: docker.io/library/busybox:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/test;sleep 30;rm -rf /tmp/test;sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/test"]
initialDelaySeconds: 5
periodSeconds: 10
[root@master k8s-pod]# kubectl describe pod liveness-exec-pod
探测失败会重启
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 -rf /tmp/healthy;sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/healthy"]
initialDelaySeconds: 1
periodSeconds: 3
kubectl apply -f liveness-exec.yaml
kubectl get pods 可以看到liveness-exec-pod出入running状态了
例二:写一个简单的pod实例
健康探测使用的探针是httpGet
#查看httpGet这个探针的用法
kubectl explain pods.spec.containers.livenessProbe.httpGet
KIND: Pod
VERSION: v1
RESOURCE: httpGet <Object>
DESCRIPTION:
HTTPGet specifies the http request to perform.
HTTPGetAction describes an action based on HTTP Get requests.
FIELDS:
host <string>
httpHeaders <[]Object>
path <string>
port <string> -required-
scheme <string>
[root@master k8s-pod]# cat pod-liveness-httpGet.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
labels:
test: liveness-httpget-pod
spec:
containers:
- name: liveness-httpget-container
image: docker.io/library/nginx:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
[root@master k8s-pod]# kubectl describe pod liveness-httpget-pod
上面可以看到容器运行正常
连入到容器内部
kubectl exec -it liveness-httpget-pod – /bin/sh
手动删除这个容器里的index.html文件
rm -rf /usr/share/nginx/html/index.html
然后再次查看容器的详细信息
目前是检测到了异常
过30s,再查看容器信息
kubectl describe pods liveness-httpget-pod 发现正常了,因为容器探测的时间间隔是30s,过了30s探测失败,那么就会重启,重启之后配置文件重新加载,那么容器重新恢复正常
重新连接可以进入到容器了
kubectl exec -it liveness-httpget-pod – /bin/sh
kubectl get pods 显示如下:
ready下面对应值通过/连接,/右面表示pod内部有几个容器,/左侧表示有几个容器就绪了,这里的就绪表示容器一启动就是就绪的,但是实际容器启动的时候里面的程序不一定起来,比方说里面运行tomcat,tomcat启动可能需要一定时间。
2.readnessProbe(容器就绪性探测)
service给pod提供一个入口地址,service和pod关联是通过标签选择器,我们后端只要创建一个pod,那么就会根据标签选择器被service关联到,但是新创建的pod里面的应用程序可能没有启动,我们在通过service访问的时候,可能会访问到刚创建的pod,但是访问时失败的,这个在生产环境是不被允许的,所以需要做容器做就绪性探测(readlinessProbe)和容器存活性探测(livenessProbe),尤其是readnessProbe
[root@master k8s-pod]# cat pod-readinessProbe-httpGet.yaml
apiVersion: v1
kind: Pod
metadata:
name: rediness-httpdget-pod
namespace: default
spec:
containers:
- name: rediness-httpget-container
image: docker.io/library/nginx:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
删除index文件能进但是没有READY
kubectl describe pod rediness-httpdget-pod
Readiness probe failed: HTTP probe failed with statuscode: 404
kubectl exec -it rediness-httpdget-pod – /bin/bash
/usr/share/nginx/html# echo hello >> index.html
即可恢复
容器不会终止,显示会显示没有READY
3.lifecycle讲解
启动后的钩子和终止前的钩子,叫做lifecycle
创建资源对象时,可以使用lifecycle来管理容器在运行前和关闭前的一些动作。lifecycle有两种回调函数:
PostStart:容器创建成功后,运行前的任务,用于资源部署、环境准备等。PreStop:在容器被终止前的任务,用于优雅关闭应用程序、通知其他系统等等。
查看lifecycle用法
kubectl explain pods.spec.containers.lifecycle
container
FIELDS:
postStart <Object>
preStop <Object>
查看lifecycle的postStart用法
kubectl explain pods.spec.containers.lifecycle.postStart
FIELDS:
exec <Object>
httpGet <Object>
tcpSocket <Object>
上面可以看到容器启动后钩子,也是有三种探针,exec,httpGet,tcpSocket
kubectl explain pods.spec.containers.lifecycle.preStop 查看lifecycle的preStop用法
FIELDS:
exec <Object>
httpGet <Object>
tcpSocket <Object>
上面可以看到容器终止前钩子,也是有三种探针,exec,httpGet,tcpSocket
[root@master k8s-pod]# cat pod-lifecycle-poststart.yaml
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-poststart-nginx
namespace: default
labels:
test: lifecycle
spec:
containers:
- name: poststart-nginx-container
image: docker.io/library/nginx:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
lifecycle:
postStart:
exec:
command: ["/bin/bash","-c","echo this is postStart-nginx > /usr/share/nginx/html/index.html"]
[root@master k8s-pod]# kubectl exec -it lifecycle-poststart-nginx – curl localhost
this is postStart-nginx
*prestop例子:
PreStop在整个生命周期中比较有用,实用场景也比较多。 比如:
1.关闭前等待某一个状态完成;
2.关闭前同步一些状态(数据)到其他的地方;
3.关闭前通知某一个系统或者更新一个状态;
cat preStop-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: prestop-nginx
namespace: default
spec:
containers:
- name: prestop-nginx
image: nginx
imagePullPolicy: IfNotPresent
lifecycle:
preStop:
exec:
command: ["/usr/local/nginx/sbin/nginx","-s","quit"]
ports:
- name: http
containerPort: 80
通过kubectl apply更新yaml
kubectl apply -f poststart-nginx.yaml
停掉pod的时候,会先把pod里的nginx先停掉
#通过pod部署应用的时候,访问应用时需要经过的数据走向
pod ip:port–>container ip:port–>container容器里具体的服务(服务暴漏的端口跟我们请求pod ip:port保持一致)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)