一、容器探测器

  1、所谓的容器探测无非就是我们在里面设置了一些探针,或者称之为传感器来获取相应的数据作为判定其存活与否或就绪与否的标准,目前k8s所支持的存活性和就绪性探测方式都是一样的。

  2、k8s的探针类型有三种

    1、ExecAction

    2、TCPSocketAction:TCPSocket探针

    3、HTTPGetAction : 如果对方是http服务那么直接向对方发http的get请求就可以了

  3、相应字段在  pods.spec.containers 之上

    a、livenessProbe <Object>

[root@k8smaster ~]# 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> #探测几次都失败才定义失败,默认为3,最小值为1
Minimum consecutive failures for the probe to be considered failed after
having succeeded. Defaults to . Minimum value is . 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> #默认每10秒钟探测一次
How often (in seconds) to perform the probe. Default to seconds. Minimum
value is . successThreshold <integer>
Minimum consecutive successes for the probe to be considered successful
after having failed. Defaults to . Must be for liveness. Minimum value
is . tcpSocket <Object>
TCPSocket specifies an action involving a TCP port. TCP hooks not yet
supported timeoutSeconds <integer> #探测超时时长,默认为1秒
Number of seconds after which the probe times out. Defaults to second.
Minimum value is . More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

    b、readinessProbe <Object>

    c、lifecycle <Object> #生命周期,定义启动后和终止前钩子的

  4、exec指针探测

[root@k8smaster ~]# 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.
[root@k8smaster manifests]# ls
liveness-exec.yaml pod-demo.yaml
[root@k8smaster 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: 1 #容器启动后等待1秒开始探测
periodSeconds: 3 #每隔3秒探测一次
restartPolicy: Always
[root@k8smaster manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-exec-pod 1/1 Running 6 9m
myapp-848b5b879b-5k4s4 1/1 Running 0 4d
myapp-848b5b879b-bzblz 1/1 Running 0 4d
myapp-848b5b879b-hzbf5 1/1 Running 0 4d
nginx-deploy-5b595999-d9lv5 1/1 Running 0 4d
pod-demo 2/2 Running 3 5h
[root@k8smaster manifests]# kubectl describe pod liveness-exec-pod
Name: liveness-exec-pod
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: k8snode2/192.168.10.12
Start Time: Thu, 09 May 2019 19:59:15 +0800
Labels: <none>
Annotations: <none>
Status: Running
IP: 10.244.2.17
Containers:
liveness-exec-container:
Container ID: docker://37b9faa3b66df5f74ce43943e20d414a6e0498b261b65e11e7c89ab26c633109
Image: busybox:latest
Image ID: docker-pullable://busybox@sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3d
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 3600
State: Running
Started: Thu, 09 May 2019 20:07:42 +0800
Last State: Terminated
Reason: Error
Exit Code: 137
Started: Thu, 09 May 2019 20:05:00 +0800
Finished: Thu, 09 May 2019 20:06:09 +0800
Ready: True
Restart Count: 6
Liveness: exec [test -e /tmp/healthy] delay=1s timeout=1s period=3s #success=1 #failure=3
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-jvtl7 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-jvtl7:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-jvtl7
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 Pulled 4d (x4 over 4d) kubelet, k8snode2 Container image "busybox:latest" already present on machine
Normal Created 4d (x4 over 4d) kubelet, k8snode2 Created container
Normal Started 4d (x4 over 4d) kubelet, k8snode2 Started container
Normal Killing 4d (x3 over 4d) kubelet, k8snode2 Killing container with id docker://liveness-exec-container:Container failed liveness probe.. Container will be killed and recreate
d. Warning Unhealthy 4d (x13 over 4d) kubelet, k8snode2 Liveness probe failed:
Normal Scheduled 9m default-scheduler Successfully assigned default/liveness-exec-pod to k8snode2

  5、基于tcpSocket探测

[root@k8smaster manifests]#  kubectl explain  pods.spec.containers.livenessProbe.tcpSocket
KIND: Pod
VERSION: v1 RESOURCE: tcpSocket <Object> DESCRIPTION:
TCPSocket specifies an action involving a TCP port. TCP hooks not yet
supported TCPSocketAction describes an action based on opening a socket FIELDS:
host <string> #基于主机,默认为pod自己的IP地址
Optional: Host name to connect to, defaults to the pod IP. port <string> -required- #基于端口
Number or name of the port to access on the container. Number must be in
the range to . Name must be an IANA_SVC_NAME.

  6、httpGet探测

[root@k8smaster manifests]#  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>
Host name to connect to, defaults to the pod IP. You probably want to set
"Host" in httpHeaders instead. httpHeaders <[]Object>
Custom headers to set in the request. HTTP allows repeated headers. path <string> #指定地址指定端口的url发送请求,如果响应码为200则ok
Path to access on the HTTP server. port <string> -required- #可以直接引用service的名称而不用端口号
Name or number of the port to access on the container. Number must be in
the range to . Name must be an IANA_SVC_NAME. scheme <string>
Scheme to use for connecting to the host. Defaults to HTTP.

    案例如下

[root@k8smaster manifests]# cat liveness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort:
livenessProbe:
httpGet:
port: http #也可以使用80
path: /index.html
initialDelaySeconds: #容器启动后等待1秒开始探测
periodSeconds: #每隔3秒探测一次
restartPolicy: Always

  7、就绪性探测,其与service调度有着重要的关联性。如果不做就绪性探测那么pod刚创建就立即被关联到service后端对象中,此时pod如果未就绪将造成服务无法被访问,因此几乎只要使用pod就必须做readinessProbe(就绪性检测)。

    其检测方式和探针与liveness一样,只是目标不一样,livenessProbe只是为了判断存活与否,而readinessProbe则是用来判断它就绪与否。因此只是探测命令可能会不一样。

[root@k8smaster manifests]# cat readiness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
namespace: default
spec:
containers:
- name: readiness-httpget-container
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort:
readinessProbe:
httpGet:
port: http #也可以使用80
path: /index.html
initialDelaySeconds: #容器启动后等待1秒开始探测
periodSeconds: #每隔3秒探测一次
restartPolicy: Always [root@k8smaster manifests]# kubectl create -f readiness-httpget.yaml
pod/readiness-httpget-pod created
[root@k8smaster manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod / Running 32m
myapp-848b5b879b-5k4s4 / Running 4d
myapp-848b5b879b-bzblz / Running 4d
myapp-848b5b879b-hzbf5 / Running 4d
nginx-deploy-5b595999-d9lv5 / Running 5d
pod-demo / Running 6h
readiness-httpget-pod / Running 6s
[root@k8smaster manifests]# kubectl describe pod readiness-httpget-pod
Name: readiness-httpget-pod
Namespace: default
Priority:
PriorityClassName: <none>
Node: k8snode2/192.168.10.12
Start Time: Thu, May :: +
Labels: <none>
Annotations: <none>
Status: Running
IP: 10.244.2.19
Containers:
readiness-httpget-container:
Container ID: docker://2972a892e1c91c2cfa6168f5729cbf1dae02e079f5bd1e8dc370e2ed56dcbf61
Image: ikubernetes/myapp:v1
Image ID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
Port: /TCP
Host Port: /TCP
State: Running
Started: Thu, May :: +
Ready: True
Restart Count:
Readiness: http-get http://:http/index.html delay=1s timeout=1s period=3s #success=1 #failure=3
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-jvtl7 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-jvtl7:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-jvtl7
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 Pulled 4d kubelet, k8snode2 Container image "ikubernetes/myapp:v1" already present on machine
Normal Created 4d kubelet, k8snode2 Created container
Normal Started 4d kubelet, k8snode2 Started container
Normal Scheduled 23s default-scheduler Successfully assigned default/readiness-httpget-pod to k8snode2 #进入容器删除index.html发现不再ready
[root@k8smaster manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod / Running 34m
myapp-848b5b879b-5k4s4 / Running 4d
myapp-848b5b879b-bzblz / Running 4d
myapp-848b5b879b-hzbf5 / Running 4d
nginx-deploy-5b595999-d9lv5 / Running 5d
pod-demo / Running 6h
readiness-httpget-pod / Running 2m

  8、lifecycle <Object> #生命周期,定义启动后和终止前钩子的

[root@k8smaster manifests]# kubectl explain  pods.spec.containers.lifecycle
KIND: Pod
VERSION: v1 RESOURCE: lifecycle <Object> DESCRIPTION:
Actions that the management system should take in response to container
lifecycle events. Cannot be updated. Lifecycle describes actions that the management system should take in
response to container lifecycle events. For the PostStart and PreStop
lifecycle handlers, management of the container blocks until the action is
complete, unless the container process fails, in which case the handler is
aborted. FIELDS:
postStart <Object> #容器启动后立即执行的操作
PostStart is called immediately after a container is created. If the
handler fails, the container is terminated and restarted according to its
restart policy. Other management of the container blocks until the hook
completes. More info:
https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks preStop <Object> #容器终止前执行的操作
PreStop is called immediately before a container is terminated. The
container is terminated after the handler completes. The reason for
termination is passed to the handler. Regardless of the outcome of the
handler, the container is eventually terminated. Other management of the
container blocks until the hook completes. More info:
https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks

    a、postStart ,默认会在容器的command命令运行完后再运行其定义的命令。

[root@k8smaster manifests]# more poststart-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: poststart-pod
namespace: default
spec:
containers:
- name: busybox-pod
image: busybox:latest
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ["mkdir","-p","/data/web/html"]
command: ["/bin/sh","-c"] #默认此命令+args执行完才会执行上面的postStart.exec.command中的命令
args: ["sleep 3600"]
[root@k8smaster manifests]# kubectl exec -it poststart-pod /bin/sh
/ # ls /data/web/html/
/ # exit
[root@k8smaster manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod / Running 1h
myapp-848b5b879b-5k4s4 / Running 4d
myapp-848b5b879b-bzblz / Running 4d
myapp-848b5b879b-hzbf5 / Running 4d
nginx-deploy-5b595999-d9lv5 / Running 5d
pod-demo / Running 7h
poststart-pod / Running 1m
readiness-httpget-pod / Running 58m

    b、preStop ,和postStart类似

 

Kubernetes 学习7 Pod控制器应用进阶2的更多相关文章

  1. Kubernetes 学习6 Pod控制器应用进阶

    一.资源配置清单 1.自主式Pod资源 2.资源的清单格式,大多数清单格式都遵循如下条件: a.一级字段:apiVersion(group/version),kind,metadata(name,na ...

  2. Kubernetes 学习8 Pod控制器

    一.回顾 1.Pod是标准的kubernetes资源,因此其遵循为其资源清单配置定义的基本格式,包含:apiVersion,kind,metadata,spec,status(只读) 2.spec的内 ...

  3. Kubernetes 学习9 Pod控制器

    一.Deployment 定义 1.简介 [root@k8smaster manifests]# kubectl explain deploy(也可以写作deployment) KIND: Deplo ...

  4. 6and7.Pod控制器应用进阶

    Pod控制器应用进阶:imagepullpolicy: 镜像获取策略 Always,Never,IfNoPresent 暴露端口: portslabels 标签可以后期添加修改. ========== ...

  5. kubernetes系列07—Pod控制器详解

    本文收录在容器技术学习系列文章总目录 1.Pod控制器 1.1 介绍 Pod控制器是用于实现管理pod的中间层,确保pod资源符合预期的状态,pod的资源出现故障时,会尝试 进行重启,当根据重启策略无 ...

  6. 05-kubernetes Pod控制器应用进阶

    目录 Pod 资源 标签 给资源打标签 标签选择器 Pod 生命周期 实际操作 livenessProbe 实战 livenessProbe exec 测试 livenessProbe httpGet ...

  7. 五,pod控制器应用进阶

    目录 Pod 资源 标签 给资源打标签 标签选择器 Pod 生命周期 pod状态探测 livenessProbe 状态探测 livenessProbe exec 测试 livenessProbe ht ...

  8. 【一】kubernetes学习笔记-Pod概念

    一.Pod 控制器类型 Pod概念 当一个 Pod 创建后,Pause 容器就会随着 Pod 启动,只要是有 Pod,Pause 容器就要被启动. 在同一个 Pod 里面的容器不能出现端口冲突,否则这 ...

  9. 6、Kubernetes Pod控制器应用进阶

    定义pod时,在spec字段中常用的定义字段有哪些? master ~]# kubectl explain pods.spec.containers KIND: Pod VERSION: v1 RES ...

随机推荐

  1. 『Go基础』第4节 VS Code配置Go语言开发环境

    VS Code 是微软开源的一款编辑器, 本文主要介绍如何使用VS Code搭建Go语言的开发环境. 下载与安装VS Code 官方下载地址: https://code.visualstudio.co ...

  2. SpringCloud入门使用

    目的: 1.springcloud简介 入门案例 2.注册中心eureka springcloud简介 推荐一个springcloud讲解详细的博客:https://blog.csdn.net/qq3 ...

  3. Js学习01--基础知识

    一. JavaScript有三种书写格式 1.行内式 <button onclick = 'alert('nice day!');'>Nice Day</button> 2. ...

  4. [NOI2008]志愿者招募 (费用流)

    大意: $n$天, 第$i$天要$a_i$个志愿者. $m$种志愿者, 每种无限多, 第$i$种工作时间$[s_i,t_i]$花费$c_i$, 求最少花费. 源点$S$连第一天, 容量$INF$ 第$ ...

  5. 二进制方式安装Kubernetes 1.14.2高可用详细步骤

    00.组件版本和配置策略 组件版本 Kubernetes 1.14.2 Docker 18.09.6-ce Etcd 3.3.13 Flanneld 0.11.0 插件: Coredns Dashbo ...

  6. idea多级目录不展开的问题

    遇见了一个坑,idea新建的包,和它的上级包重叠在了一起,无法形成树状结构 原因呢,还是因为自己的不细心了,解决方案很简单,下面的是原情况 解决方案,点击左侧栏右上角的设置图表,注意看红框内 把第一行 ...

  7. GOF 的23种JAVA常用设计模式总结 01 设计模式的概念分类和功能

    1.简介 软件设计模式(Software Design Pattern),又称设计模式,是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.它描述了在软件设计过程中的一些不断重复发生的 ...

  8. Unity - Profiler参数详解

    CPU Usage ​       ● GC Alloc - 记录了游戏运行时代码产生的堆内存分配.这会导致ManagedHeap增大,加速GC的到来.我们要尽可能避免不必要的堆内存分配,同时注意:1 ...

  9. nginx 开启 gzip 压缩

    现在使用vue或react开发的项目越来越多,纯js渲染,导致js体积越来越多,动辄就是几百上千kb,此时可以使用gzip的方式压缩js大小,减少请求时间与流量. 配置: http { gzip on ...

  10. Java面向对象程序设计----接口

    接口:接口是一套规范.一个比抽象类更抽象的类. 接口中只能写抽象方法.接口中没有构造函数接口中的变量:public Stratic final接口怎么来使用(implements)实现接口 接口语法: ...