首页 > 其他分享 >kubernetes探针

kubernetes探针

时间:2023-02-09 17:44:05浏览次数:51  
标签:httpget kubernetes exec demo liveness 探针 probe root

LivenessProbe:周期性检测,检测未通过时,kubelet会根据restartPolicy的定义来决定是否会重启该容器;未定义时,kubelet认为容器未终止,即为健康;

ReadnessProbe:周期性检测,检测未通过时,与该Pod关联的Service,会将该Pod从Service的后段可用端点列表中删除;直到再次就绪再重新添加回来,未定义时,只要容器未终止,即为就绪;

StartupProbe:便于用户使用同livenessProbe不同参数或阈值;

 

三种探针:

  ExecAction:直接执行命令,命令成功返回表示探测成功;

  TCPSocketAction:端口正常打开,即成功;

  HTTPGetAction:向指定的path发送HTTP请求,2xx,3xx的响应码表示成功;

 

命令探测

[root@master probe]# kubectl apply -f liveness-exec-demo.yaml 
[root@master probe]# cat liveness-exec-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-demo
  namespace: default
spec:
  containers:
  - name: demo
    image: ikubernetes/demoapp:v1.0
    imagePullPolicy: IfNotPresent
    livenessProbe:
      exec:
        command: ['/bin/sh', '-c', '[ "$(curl -s 127.0.0.1/livez)" == "OK" ]']
      initialDelaySeconds: 5  #5秒后探测
      timeoutSeconds: 1       #延迟时间1秒
      periodSeconds: 5        #间隔时间5秒

进入到Pod将livez改成FAIL,容器会自动重启

[root@master probe]# kubectl  exec -it liveness-exec-demo  /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.

[root@liveness-exec-demo /]# curl 10.244.166.154/livez
OK
[root@liveness-exec-demo /]# curl -XPOST -d 'livez=FAIL' 10.244.166.154/livez
[root@liveness-exec-demo /]# curl 10.244.166.154/livez

TCP探测

[root@master probe]# kubectl apply -f  liveness-tcpsocket-demo.yaml 
[root@master probe]# cat liveness-tcpsocket-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-tcpsocket-demo
  namespace: default
spec:
  containers:
  - name: demo
    image: ikubernetes/demoapp:v1.0
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    securityContext:
      capabilities:
        add:
        - NET_ADMIN
    livenessProbe:
      tcpSocket:
        port: http
      periodSeconds: 5
      initialDelaySeconds: 5
[root@master probe]# kubectl get pods
NAME                      READY   STATUS    RESTARTS       AGE
liveness-exec-demo        1/1     Running   1 (143m ago)   62m
liveness-tcpsocket-demo   1/1     Running   0              3s

 在容器内把80端口拒绝掉就会探测失败

[root@master probe]# kubectl exec liveness-tcpsocket-demo  -- iptables -A INPUT -p tcp --dport 80 -j REJECT

http探测

[root@master probe]# kubectl apply -f liveness-httpget-demo.yaml 
[root@master probe]# cat liveness-httpget-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-demo
  namespace: default
spec:
  containers:
  - name: demo
    image: ikubernetes/demoapp:v1.0
    imagePullPolicy: IfNotPresent
    livenessProbe:
      httpGet:
        path: '/livez'
        port: 80
        scheme: HTTP
      initialDelaySeconds: 5
[root@master probe]# kubectl get pods
NAME                      READY   STATUS             RESTARTS       AGE
liveness-exec-demo        1/1     Running            1 (152m ago)   71m
liveness-httpget-demo     1/1     Running            0              3m42s
liveness-tcpsocket-demo   0/1     CrashLoopBackOff   6 (57s ago)    8m33s
[root@master probe]# kubectl  exec -it liveness-httpget-demo -- /bin/sh
[root@liveness-httpget-demo /]# 
[root@liveness-httpget-demo /]# curl 127.0.0.1/livez
OK[root@liveness-httpget-demo /]# 
[root@liveness-httpget-demo /]# curl -XPOST -d 'livez=FAIL' 127.0.0.1/livez
[root@liveness-httpget-demo /]# curl -I 127.0.0.1/livez
HTTP/1.0 506 VARIANT ALSO NEGOTIATES
Content-Type: text/html; charset=utf-8
Content-Length: 4
Server: Werkzeug/1.0.0 Python/3.8.2
Date: Thu, 09 Feb 2023 09:07:16 GMT

 

 

Readnessprobe http探针

[root@master probe]# kubectl apply -f readiness-httpget-demo.yaml 
[root@master probe]# cat readiness-httpget-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-demo
  namespace: default
spec:
  containers:
  - name: demo
    image: ikubernetes/demoapp:v1.0
    imagePullPolicy: IfNotPresent
    readinessProbe:
      httpGet:
        path: 'readyz'
        port: 80
        scheme: HTTP
      initialDelaySeconds: 15
      timeoutSeconds: 2
      periodSeconds: 5
      failureThreshold: 3
  restartPolicy: Always
[root@readiness-httpget-demo /]# curl http://10.244.166.155:80/readyz
OK
[root@readiness-httpget-demo /]# curl -XPOST -d 'readyz=FAIL' 10.244.166.155:80/readyz
[root@readiness-httpget-demo /]# curl -I 10.244.166.155:80/readyz
HTTP/1.0 507 INSUFFICIENT STORAGE
Content-Type: text/html; charset=utf-8
Content-Length: 4
Server: Werkzeug/1.0.0 Python/3.8.2
Date: Thu, 09 Feb 2023 07:35:16 GMT

标签:httpget,kubernetes,exec,demo,liveness,探针,probe,root
From: https://www.cnblogs.com/zyyang1993/p/17104178.html

相关文章

  • 使用Function定时启用/停止Azure Kubernetes群集
    最近在做AKS相关的测试,测试的过程中发现,虽然在Azure中的AKS群集对用户免费提供,但是多个WorkNode一起运行其实也是一笔不小的费用。因为我只有在工作时间进行测试,所以就开始......
  • kubernetes top查看内存CPU使用情况
    1.top简介k8stop命令我们通常可以基于top命令来查看节点上的资源使用情况,可以带两个参数nodes和pods,通过这个命令分别用于查看节点和pods的资源使用情况,这对于我们快速......
  • 基于 Kubernetes 的企业级大数据平台,EMR on ACK 技术初探
    作者:容器服务团队云上大数据的Kubernetes技术路线当前,大数据与机器学习领域颇为关注存储与计算分离架构,逐渐向云原生演进。以Spark为例,云下或自有服务器可以选择Hado......
  • Kubernetes(k8s)控制器(三):ReplicationController
    目录一.系统环境二.前言三.ReplicationController概览四.ReplicationController工作机制五.创建ReplicationController六.扩展replicationcontroller副本数一.系统环境......
  • Kubernetes中PV&PVC访问模式
    问题为什么PV的accessmode设置的访问模式是ReadWriteOnce,但是其他节点还是可以读写。为什么PV的accessmode设置的模式为ReadOnlyMany,其他节点还是可以读写。前言这是明......
  • API对象--Ingress(chrono《kubernetes入门实战课》笔记整理)
    【概念说明】Service的功能和运行机制,主要由kube-proxy控制的四层负载均衡,即根据IP、PORT来做负载均衡。而很多应用都是在7层做均衡更为合理,比如根据主机名、URI、请求......
  • jenkins 添加kubernetes-k8s
                   ......
  • Kubernetes(k8s)控制器(一):deployment
    目录一.系统环境二.前言三.Kubernetes控制器四.Deployment概览五.创建deployment六.修改deploy副本数6.1kubectleditdeploy修改副本数6.2修改yaml文件更改副本数6.3......
  • Kubernetes(K8S)概述
    一、K8S概述1、K8S的作用(1)K8S的全称为Kubernetes。(2)用于自动部署、不熟和管理“容器化(containerized)应用程序”的开源系统,可以理解成K8S是负责自动化运维管理多个容器化......
  • Jenkins 和 Kubernetes 云上的神秘代理
    最近我们构建和部署服务的方式与原来相比简直就是突飞猛进,像那种笨拙的、单一的、用于构建单体式应用程序的方式已经是过去式了。我们努力了这么久,终于达到了现在的效果。现......