找回密码
 立即注册
首页 业界区 业界 探针
林鱼 2025-8-2 16:25:30
一、探针

1、为什么会有这个呢?


  • 虽然pod的状态是running,但是了里面的文件丢失了,导致了上层的应用不能运行了
  • 就有一个探针,探测这个pod是否是正常的工作
2、存活指针


  • 就是出现了问题,重启即可
  • 也就是删除这个容器,重新再次创建
  • 一些条件
  • 就是删除重建
1、exec方式探测


  • 使用命令行方式探测
  1. # 创建一个文件,然后删除,然后探测这个文件是否还存在,不存在就一直执行策略
  2. [root@k-master 8-2]# cat pod3.yaml
  3. apiVersion: v1
  4. kind: Pod
  5. metadata:
  6.   creationTimestamp: null
  7.   labels:
  8.     run: pod3
  9.   name: pod3
  10. spec:
  11.   containers:
  12.   - image: busybox
  13.     imagePullPolicy: IfNotPresent
  14.     name: pod3
  15.     args:
  16.     - /bin/sh
  17.     - -c
  18.     - touch /tmp/healthy; sleep30; rm -f /tmp/healthy; sleep 6000
  19.     resources: {}
  20.     livenessProbe:
  21.       exec:
  22.          command:
  23.          - cat
  24.          - /tmp/healthy
  25.       initialDelaySeconds: 5  # 启动后5秒之内不探测
  26.       periodSeconds: 5  # 每隔5秒探测一次
  27.   dnsPolicy: ClusterFirst
  28.   restartPolicy: Always
  29. status: {}
  30. # 另一个终端查看状态
  31. # 会触发存活探针策略
  32. # 删除了容器之后,再次创建容器,创建文件,然后删除文件,探针检测不到文件,就再次删除容器
  33. [root@k-master 8-2]# kubectl get pod -w
  34. NAME   READY   STATUS    RESTARTS   AGE
  35. pod3   1/1     Running   0          6s
  36. pod3   1/1     Running   1 (2s ago)   51s
  37. pod3   1/1     Running   2 (2s ago)   101s
  38. pod3   1/1     Running   3 (2s ago)   2m31s
  39. pod3   1/1     Running   4 (2s ago)   3m21s
  40. pod3   1/1     Running   5 (1s ago)   4m10s
复制代码
2、httpGet方式
  1. [root@k-master 8-2]# cat pod4.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   creationTimestamp: null
  6.   labels:
  7.     run: pod4
  8.   name: pod4
  9. spec:
  10.   containers:
  11.   - image: nginx
  12.     imagePullPolicy: IfNotPresent
  13.     name: pod4
  14.     resources: {}
  15.     livenessProbe:
  16.       httpGet:
  17.         path: /index.html  # 这个/不是linux的根,而是这个网站的根目录 /代表的是/usr/share/nginx/html/
  18.         port: 80  # 端口是容器的80端口
  19.       initialDelaySeconds: 5
  20.       periodSeconds: 5
  21.   dnsPolicy: ClusterFirst
  22.   restartPolicy: Always
  23. status: {}
  24. # 运行pod,然后删除这个index.html文件,测试
  25. root@pod4:/usr/share/nginx/html# ls
  26. 50x.html  index.html
  27. root@pod4:/usr/share/nginx/html# rm -rf index.html
  28. root@pod4:/usr/share/nginx/html#
  29. # 另一个终端查看状态,发现重启了
  30. [root@k-master 8-2]# kubectl get pod -w
  31. NAME   READY   STATUS    RESTARTS   AGE
  32. pod4   1/1     Running   0          97s
  33. pod4   1/1     Running   1 (1s ago)   2m51s
  34. # 自动退出了容器,删除重建
  35. root@pod4:/usr/share/nginx/html# command terminated with exit code 137
  36. [root@k-master 8-2]#
复制代码
3、tcpSocket方式


  • 指定监听一个端口
  1. [root@k-master 8-2]# cat pod4.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   creationTimestamp: null
  6.   labels:
  7.     run: pod4
  8.   name: pod4
  9. spec:
  10.   containers:
  11.   - image: nginx
  12.     imagePullPolicy: IfNotPresent
  13.     name: pod4
  14.     resources: {}
  15.     livenessProbe:
  16.       tcpSocket:
  17.         port: 81  # 监听的是81端口
  18.   dnsPolicy: ClusterFirst
  19.   restartPolicy: Always
  20. status: {}
  21. # tcp三次握手,因为连接不上80端口就会重启
复制代码
3、就绪探针


  • readiness probe 检测到问题不重启,只是不将请求转发到这个pod
1、httpGet
  1. [root@k-master 8-2]# cat web1.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5.   creationTimestamp: null
  6.   labels:
  7.     app: web1
  8.   name: web1
  9. spec:
  10.   replicas: 3
  11.   selector:
  12.     matchLabels:
  13.       app: web1
  14.   strategy: {}
  15.   template:
  16.     metadata:
  17.       creationTimestamp: null
  18.       labels:
  19.         app: web1
  20.     spec:
  21.       containers:
  22.       - image: nginx
  23.         name: nginx
  24.         readinessProbe:
  25.           httpGet:
  26.             port: 80
  27.             path: /index.html
  28.         resources: {}
  29. status: {}
  30. # 创建一个deployment和svc
  31. [root@k-master 8-2]# kubectl get deployments.apps,svc
  32. NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
  33. deployment.apps/web1   3/3     3            3           51s
  34. NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
  35. service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   13d
  36. service/web1         ClusterIP   10.111.145.122   <none>        80/TCP    33s
  37. [root@k-master 8-2]#
复制代码

  • 访问这个svc的ip和端口会将请求转发到关联的pod
  • 这个svc相当于是一个负载均衡器
  1. # 访问svc
  2. # 相当于是一个负载均衡器
  3. [root@k-master 8-2]# curl 10.111.145.122
  4. 02
  5. [root@k-master 8-2]# curl 10.111.145.122
  6. 01
  7. [root@k-master 8-2]# curl 10.111.145.122
  8. 03
复制代码

  • 模拟错误
  1. # 重命名一个pod中的index.html文件
  2. # pod不会重启,只是不将请求发送给这个pod
  3. root@web1-7c9c8bdbf9-55wgp:/# mv /usr/share/nginx/html/index.html 11   
  4. # 探针检测到了
  5. [root@k-master 8-2]# kubectl get pod
  6. NAME                    READY   STATUS    RESTARTS   AGE
  7. web1-7c9c8bdbf9-55wgp   0/1     Running   0          20m
  8. web1-7c9c8bdbf9-l9ts4   1/1     Running   0          20m
  9. web1-7c9c8bdbf9-lmvq2   1/1     Running   0          20m
  10. # 0就是有问题
复制代码
来源:豆瓜网用户自行投稿发布,如果侵权,请联系站长删除

相关推荐

您需要登录后才可以回帖 登录 | 立即注册