想学做网站,怎样做网站域名注册,网站的建设可以起到什么作用是什么,合肥好的app开发公司这里写目录标题 为什么要对容器做探测默认的健康检查 k8s 提供了三种来实现容器探测的方法三种探针Pod探针相关的属性#xff1a; 启动探测startupprobeexec模式tcpsocket模式httpget模式 存活性探测livenessProbeLivenessProbe 探针使用示例通过HTTP方式做健康探测httpGet探测… 这里写目录标题 为什么要对容器做探测默认的健康检查 k8s 提供了三种来实现容器探测的方法三种探针Pod探针相关的属性 启动探测startupprobeexec模式tcpsocket模式httpget模式 存活性探测livenessProbeLivenessProbe 探针使用示例通过HTTP方式做健康探测httpGet探测方式有如下可选的控制字段: 通过TCP方式做健康探测 就绪性探测readinessProbeReadinessProbe 探针使用示例 三种探测配合使用示例 为什么要对容器做探测 在 Kubernetes 中 Pod 是最小的计算单元而一个 Pod 又由多个容器组成相当于每个容器就是一个应用应用在运行期间可能因为某些意外情况致使程序挂掉。那么如何监控这些容器状态稳定性保证服务在运行期间不会发生问题发生问题后进行重启等机制就成为了重中之重的事情考虑到这点 kubernetes 推出了活性探针机制。有了存活性探针能保证程序在运行中如果挂掉能够自动重启但是还有个经常遇到的问题比如说在Kubernetes 中启动Pod显示明明Pod已经启动成功且能访问里面的端口但是却返回错误信息。还有就是在执行滚动更新时候总会出现一段时间Pod对外提供网络访问但是访问却发生404这两个原因都是因为Pod已经成功启动但是 Pod 的的容器中应用程序还在启动中导致考虑到这点Kubernetes推出了就绪性探针机制。 默认的健康检查 Kubernetes默认的健康检查机制 每个容器启动时都会执行一个进程 此进程由Dockerfile的CMD或ENTRYPOINT指定。 如果进程退出时返回码非零 则认为容器发生故障 Kubernetes就会根据restartPolicy策略决定是否重启容器。 cat check.yaml apiVersion: v1
kind: Pod
metadata:name: checknamespace: defaultlabels:app: check
spec:containers:- name: checkimage: busybox:1.28imagePullPolicy: IfNotPresentcommand:- /bin/sh- -c- sleep 10;exitkubectl get pods -wNAME READY STATUS RESTARTS AGE
check 0/1 Pending 0 0s
check 0/1 Pending 0 0s
check 0/1 ContainerCreating 0 0s
check 0/1 ContainerCreating 0 1s
check 1/1 Running 0 1s
check 0/1 Completed 0 11s
check 1/1 Running 1 (1s ago) 12s
check 0/1 Completed 1 (11s ago) 22s
check 0/1 CrashLoopBackOff 1 (13s ago) 34s
check 1/1 Running 2 (13s ago) 34s
check 0/1 Completed 2 (23s ago) 44s在上面的例子中 容器进程返回值非零 Kubernetes则认为容器发生故障 需要重启。 有不少情况是发生了故障 但进程并不会退出。 比如访问Web服务器时显示500内部错误 可能是系统超载 也可能是资源死锁 此时httpd进程并没有异常退出 在这种情况下重启容器可能是最直接、 最有效的解决方案。 k8s 提供了三种来实现容器探测的方法
startupProbe探测容器中的应用是否已经启动。如果提供了启动探测(startup probe)则禁用所有其他探测直到它成功为止。如果启动探测失败kubelet 将杀死容器容器服从其重启策略进行重启。如果容器没有提供启动探测则默认状态为成功Success。livenessprobe用指定的方式exec、tcp、http检测pod中的容器是否正常运行如果检测失败则认为容器不健康那么Kubelet将根据Pod中设置的 restartPolicy策略来判断Pod 是否要进行重启操作如果容器配置中没有配置 livenessProbeKubelet 将认为存活探针探测一直为success成功状态。readnessprobe就绪性探针用于检测容器中的应用是否可以接受请求当探测成功后才使Pod对外提供网络访问将容器标记为就绪状态可以加到pod前端负载如果探测失败则将容器标记为未就绪状态会把pod从前端负载移除。可以自定义在pod启动时是否执行这些检测如果不设置则检测结果均默认为通过如果设置则顺序为startupProbereadinessProbe和livenessProbereadinessProbe和livenessProbe是并发关系 真正的启动顺序官方文档Caution: Liveness probes do not wait for readiness probes to succeed. If you want to wait before executing a liveness probe you should use initialDelaySeconds or a startupProbe 也就是 Liveness probes 并不会等到 Readiness probes 成功之后才运行根据上面的官方文档Liveness 和 readiness 应该是某种并发的关系。 三种探针 目前LivenessProbe和ReadinessProbe、startupprobe探测都支持下面 exec在容器中执行指定的命令如果执行成功退出码为 0 则探测成功。TCPSocket通过容器的 IP 地址和端口号执行 TCP 检 查如果能够建立 TCP 连接则表明容器健康。HTTPGet通过容器的IP地址、端口号及路径调用 HTTP Get方法如果响应的状态码大于等于200且小于400则认为容器健康 探针探测结果有以下值 Success表示通过检测。Failure表示未通过检测。Unknown表示检测没有正常进行。
Pod探针相关的属性 探针(Probe)有许多可选字段可以用来更加精确的控制Liveness和Readiness两种探针的行为 initialDelaySeconds容器启动后要等待多少秒后探针开始工作单位“秒”默认是 0 秒最小值是 0periodSeconds 执行探测的时间间隔单位是秒默认为 10s单位“秒”最小值是1timeoutSeconds 探针执行检测请求后等待响应的超时时间默认为1单位“秒”。successThreshold连续探测几次成功才认为探测成功默认为 1在 Liveness 探针中必须为1最小值为1。failureThreshold 探测失败的重试次数重试一定次数后将认为失败在 readiness 探针中Pod会被标记为未就绪默认为 3最小值为 1 两种探针区别 ReadinessProbe 和 livenessProbe 可以使用相同探测方式只是对 Pod 的处置方式不同readinessProbe 当检测失败后将 Pod 的 IP:Port 从对应的 EndPoint 列表中删除。livenessProbe 当检测失败后将杀死容器并根据 Pod 的重启策略来决定作出对应的措施。
启动探测startupprobe
exec模式
cat startup-exec.yaml apiVersion: v1
kind: Pod
metadata:name: startupprobe
spec:containers:- name: startup
image: k8s/tomcat-8.5-jre8:v1
imagePullPolicy: IfNotPresentports:- containerPort: 8080startupProbe:exec:command:- /bin/sh- -c- aa ps aux | grep tomcatinitialDelaySeconds: 20 #容器启动后多久开始探测periodSeconds: 20 #执行探测的时间间隔timeoutSeconds: 10 #探针执行检测请求后等待响应的超时时间successThreshold: 1 #成功多少次才算成功failureThreshold: 3 #失败多少次才算失败tcpsocket模式
cat startup-tcpsocket.yaml apiVersion: v1
kind: Pod
metadata:name: startupprobe
spec:containers:- name: startup
image: k8s/tomcat-8.5-jre8:v1
imagePullPolicy: IfNotPresentports:- containerPort: 8080startupProbe:tcpSocket:port: 8080initialDelaySeconds: 20 #容器启动后多久开始探测periodSeconds: 20 #执行探测的时间间隔timeoutSeconds: 10 #探针执行检测请求后等待响应的超时时间successThreshold: 1 #成功多少次才算成功failureThreshold: 3 #失败多少次才算失败
httpget模式
cat startup-httpget.yaml apiVersion: v1
kind: Pod
metadata:name: startupprobe
spec:containers:- name: startup
image: k8s/tomcat-8.5-jre8:v1
imagePullPolicy: IfNotPresentports:- containerPort: 8080startupProbe:httpGet:path: /port: 8080initialDelaySeconds: 20 #容器启动后多久开始探测periodSeconds: 20 #执行探测的时间间隔timeoutSeconds: 10 #探针执行检测请求后等待响应的超时时间successThreshold: 1 #成功多少次才算成功failureThreshold: 3 #失败多少次才算失败
存活性探测livenessProbe 官网地址https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/ LivenessProbe 探针使用示例
cat liveness-exec.yamlapiVersion: v1
kind: Pod
metadata:name: liveness-execlabels:app: liveness
spec:containers:- name: liveness
image: busybox:1.28
imagePullPolicy: IfNotPresentargs: #创建测试探针探测的文件- /bin/sh- -c- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600livenessProbe:initialDelaySeconds: 10 #延迟检测时间periodSeconds: 5 #检测时间间隔exec:command:- cat- /tmp/healthy容器启动设置执行的命令 /bin/sh -c “touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600” 容器在初始化后首先创建一个 /tmp/healthy 文件然后执行睡眠命令睡眠 30 秒到时间后执行删除 /tmp/healthy 文件命令。而设置的存活探针检检测方式为执行 shell 命令用 cat 命令输出 healthy 文件的内容如果能成功执行这条命令存活探针就认为探测成功否则探测失败。在前 30 秒内由于文件存在所以存活探针探测时执行 cat /tmp/healthy 命令成功执行。30 秒后 healthy 文件被删除所以执行命令失败Kubernetes 会根据 Pod 设置的重启策略来判断是否重启 Pod。 通过HTTP方式做健康探测 上传springboot.tar.gz 至节点服务器 链接https://pan.baidu.com/s/1SvEeYsOUl8uU0E6uwLi73Q?pwdhplo 提取码hplo ctr -n k8s.io images import springboot.tar.gzcat liveness-http.yamlapiVersion: v1
kind: Pod
metadata:name: liveness-httplabels:test: liveness
spec:containers:- name: liveness
image: mydlqclub/springboot-helloworld:0.0.1
imagePullPolicy: IfNotPresentlivenessProbe:initialDelaySeconds: 20 #延迟加载时间periodSeconds: 5 #重试时间间隔timeoutSeconds: 10 #超时时间设置httpGet:scheme: HTTPport: 8081path: /actuator/health上面 Pod 中启动的容器是一个 SpringBoot 应用其中引用了 Actuator 组件提供了 /actuator/health 健康检查地址存活探针可以使用 HTTPGet 方式向服务发起请求请求 8081 端口的 /actuator/health 路径来进行存活判断 任何大于或等于200且小于400的代码表示探测成功。任何其他代码表示失败。如果探测失败则会杀死 Pod 进行重启操作。
httpGet探测方式有如下可选的控制字段:
scheme: 用于连接host的协议默认为HTTP。host要连接的主机名默认为Pod IP可以在http request head中设置host头部。port容器上要访问端口号或名称。pathhttp服务器上的访问URI。httpHeaders自定义HTTP请求headersHTTP允许重复headers。
通过TCP方式做健康探测
cat liveness-tcp.yamlapiVersion: v1
kind: Pod
metadata:name: liveness-tcplabels:app: liveness
spec:containers:- name: liveness
image: docker.io/k8s/nginx:v1
imagePullPolicy: IfNotPresentlivenessProbe:initialDelaySeconds: 15periodSeconds: 20tcpSocket:port: 80TCP 检查方式和 HTTP 检查方式非常相似在容器启动 initialDelaySeconds 参数设定的时间后kubelet 将发送第一个 livenessProbe 探针尝试连接容器的 80 端口如果连接失败则将杀死 Pod 重启容器。 就绪性探测readinessProbe
ReadinessProbe 探针使用示例 Pod 的ReadinessProbe 探针使用方式和 LivenessProbe 探针探测方法一样也是支持三种只是一个是用于探测应用的存活一个是判断是否对外提供流量的条件。这里用一个 Springboot 项目设置 ReadinessProbe 探测 SpringBoot 项目的 8081 端口下的 /actuator/health 接口如果探测成功则代表内部程序以及启动就开放对外提供接口访问否则内部应用没有成功启动暂不对外提供访问直到就绪探针探测成功。 cat readiness-exec.yamlapiVersion: v1
kind: Service
metadata:name: springbootlabels:app: springboot
spec:type: NodePortports:- name: serverport: 8080targetPort: 8080nodePort: 31180- name: managementport: 8081targetPort: 8081nodePort: 31181selector:app: springboot
---
apiVersion: v1
kind: Pod
metadata:name: springbootlabels:app: springboot
spec:containers:- name: springboot
image: mydlqclub/springboot-helloworld:0.0.1
imagePullPolicy: IfNotPresentports:- name: servercontainerPort: 8080- name: managementcontainerPort: 8081readinessProbe:initialDelaySeconds: 20 periodSeconds: 5 timeoutSeconds: 10 httpGet:scheme: HTTPport: 8081path: /actuator/health三种探测配合使用示例 一般程序中需要设置三种探针结合使用并且也要结合实际情况来配置初始化检查时间和检测间隔下面列一个简单的 SpringBoot 项目的例子。 cat start-read-live.yaml apiVersion: v1
kind: Service
metadata:name: springboot-livelabels:app: springboot
spec:type: NodePortports:- name: serverport: 8080targetPort: 8080nodePort: 31180- name: managementport: 8081targetPort: 8081nodePort: 31181selector:app: springboot
---
apiVersion: v1
kind: Pod
metadata:name: springboot-livelabels:app: springboot
spec:containers:- name: springbootimage: mydlqclub/springboot-helloworld:0.0.1imagePullPolicy: IfNotPresentports:- name: servercontainerPort: 8080- name: managementcontainerPort: 8081readinessProbe:initialDelaySeconds: 20 periodSeconds: 5 timeoutSeconds: 10 httpGet:scheme: HTTPport: 8081path: /actuator/healthlivenessProbe:initialDelaySeconds: 20periodSeconds: 5timeoutSeconds: 10httpGet:scheme: HTTPport: 8081path: /actuator/healthstartupProbe:initialDelaySeconds: 20periodSeconds: 5timeoutSeconds: 10httpGet:scheme: HTTPport: 8081path: /actuator/health