探针

一、探针

1、为什么会有这个呢?

  • 虽然pod的状态是running,但是了里面的文件丢失了,导致了上层的应用不能运行了

  • 就有一个探针,探测这个pod是否是正常的工作

2、存活指针

  • 就是出现了问题,重启即可

  • 也就是删除这个容器,重新再次创建

  • 一些条件

  • 就是删除重建

1、exec方式探测

  • 使用命令行方式探测
bash 复制代码
# 创建一个文件,然后删除,然后探测这个文件是否还存在,不存在就一直执行策略

[root@k-master 8-2]# cat pod3.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: pod3
  name: pod3
spec:
  containers:
  - image: busybox
    imagePullPolicy: IfNotPresent
    name: pod3
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep30; rm -f /tmp/healthy; sleep 6000
    resources: {}
    livenessProbe:
      exec:
         command:
         - cat 
         - /tmp/healthy
      initialDelaySeconds: 5  # 启动后5秒之内不探测
      periodSeconds: 5  # 每隔5秒探测一次
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

# 另一个终端查看状态

# 会触发存活探针策略
# 删除了容器之后,再次创建容器,创建文件,然后删除文件,探针检测不到文件,就再次删除容器
[root@k-master 8-2]# kubectl get pod -w
NAME   READY   STATUS    RESTARTS   AGE
pod3   1/1     Running   0          6s




pod3   1/1     Running   1 (2s ago)   51s
pod3   1/1     Running   2 (2s ago)   101s
pod3   1/1     Running   3 (2s ago)   2m31s
pod3   1/1     Running   4 (2s ago)   3m21s
pod3   1/1     Running   5 (1s ago)   4m10s

2、httpGet方式

bash 复制代码
[root@k-master 8-2]# cat pod4.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: pod4
  name: pod4
spec:
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: pod4
    resources: {}
    livenessProbe:
      httpGet:
        path: /index.html  # 这个/不是linux的根,而是这个网站的根目录 /代表的是/usr/share/nginx/html/ 
        port: 80  # 端口是容器的80端口
      initialDelaySeconds: 5
      periodSeconds: 5

  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}


# 运行pod,然后删除这个index.html文件,测试

root@pod4:/usr/share/nginx/html# ls 
50x.html  index.html
root@pod4:/usr/share/nginx/html# rm -rf index.html 
root@pod4:/usr/share/nginx/html# 

# 另一个终端查看状态,发现重启了
[root@k-master 8-2]# kubectl get pod -w 
NAME   READY   STATUS    RESTARTS   AGE
pod4   1/1     Running   0          97s


pod4   1/1     Running   1 (1s ago)   2m51s

# 自动退出了容器,删除重建
root@pod4:/usr/share/nginx/html# command terminated with exit code 137
[root@k-master 8-2]# 

3、tcpSocket方式

  • 指定监听一个端口
bash 复制代码
[root@k-master 8-2]# cat pod4.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: pod4
  name: pod4
spec:
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: pod4
    resources: {}
    livenessProbe:
      tcpSocket:
        port: 81  # 监听的是81端口
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

# tcp三次握手,因为连接不上80端口就会重启

3、就绪探针

  • readiness probe 检测到问题不重启,只是不将请求转发到这个pod

1、httpGet

bash 复制代码
[root@k-master 8-2]# cat web1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
    spec:
      containers:
      - image: nginx
        name: nginx
        readinessProbe:
          httpGet:
            port: 80
            path: /index.html

        resources: {}
status: {}


# 创建一个deployment和svc
[root@k-master 8-2]# kubectl get deployments.apps,svc
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/web1   3/3     3            3           51s

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   13d
service/web1         ClusterIP   10.111.145.122   <none>        80/TCP    33s
[root@k-master 8-2]# 
  • 访问这个svc的ip和端口会将请求转发到关联的pod

  • 这个svc相当于是一个负载均衡器

bash 复制代码
# 访问svc
# 相当于是一个负载均衡器
[root@k-master 8-2]# curl 10.111.145.122
02
[root@k-master 8-2]# curl 10.111.145.122
01
[root@k-master 8-2]# curl 10.111.145.122
03
  • 模拟错误
bash 复制代码
# 重命名一个pod中的index.html文件
# pod不会重启,只是不将请求发送给这个pod

root@web1-7c9c8bdbf9-55wgp:/# mv /usr/share/nginx/html/index.html 11   

# 探针检测到了
[root@k-master 8-2]# kubectl get pod 
NAME                    READY   STATUS    RESTARTS   AGE
web1-7c9c8bdbf9-55wgp   0/1     Running   0          20m
web1-7c9c8bdbf9-l9ts4   1/1     Running   0          20m
web1-7c9c8bdbf9-lmvq2   1/1     Running   0          20m

# 0就是有问题