其他分享
首页 > 其他分享> > 7.5 实现基于探针对pod中的访问的健康状态检查

7.5 实现基于探针对pod中的访问的健康状态检查

作者:互联网

1.pod的声明周期

取值 描述
Pending Pod 已被 Kubernetes 系统接受,但有一个或者多个容器尚未创建亦未运行。此阶段包括等待 Pod 被调度的时间和通过网络下载镜像的时间,
Running Pod 已经绑定到了某个节点,Pod 中所有的容器都已被创建。至少有一个容器仍在运行,或者正处于启动或重启状态。
Succeeded Pod 中的所有容器都已成功终止,并且不会再重启。
Failed Pod 中的所有容器都已终止,并且至少有一个容器是因为失败终止。也就是说,容器以非 0 状态退出或者被系统终止。
Unknown 因为某些原因无法取得 Pod 的状态。这种情况通常是因为与 Pod 所在主机通信失败。

2.容器的状态

Kubernetes 会跟踪 Pod 中每个容器的状态,就像它跟踪 Pod 总体上的阶段一样。
旦调度器将 Pod 分派给某个节点,kubelet 就通过 容器运行时 开始为 Pod 创建容器。 容器的状态有三种:Waiting(等待)、Running(运行中)和 Terminated(已终止)
每种状态都有特定的含义:

3.容器探针

每次探测都将获得以下三种结果之一:

针对运行中的容器,kubelet 可以选择是否执行以下三种探针,以及如何针对探测结果作出反应:

3.1 配置探针

Probe 有很多配置字段,可以使用这些字段精确的控制存活和就绪检测的行为:

3.2 探针配置实例

HTTPGetAction

# yaml
root@k8-deploy:~/k8s-yaml/probe# cat HTTPGetAction.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels: #rs or deployment
      app: ng-deploy-80
    #matchExpressions:
    #  - {key: app, operator: In, values: [ng-deploy-80,ng-rs-81]}
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx:1.21.1
        ports:
        - containerPort: 80
        livenessProbe:
          httpGet:
            path: /index.html
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 3
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /index.html
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 3
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3

---
apiVersion: v1
kind: Service
metadata:
  name: ng-deploy-80 
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
    nodePort: 30082
    protocol: TCP
  type: NodePort
  selector:
    app: ng-deploy-80

# 创建
root@k8-deploy:~/k8s-yaml/probe# kubectl apply -f HTTPGetAction.yaml 
deployment.apps/nginx-deployment created

root@k8-deploy:~/k8s-yaml/probe# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7c4dbc5c76-w4j8c   1/1     Running   0          7s

# 验证
root@k8-deploy:~/k8s-yaml/probe# kubectl get ep
NAME           ENDPOINTS                                               AGE
kubernetes     192.168.2.11:6443,192.168.2.12:6443,192.168.2.13:6443   33d
ng-deploy-80   10.100.172.231:80                                       17s

root@k8-deploy:~/k8s-yaml/probe# kubectl get svc
NAME           TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes     ClusterIP   10.0.0.1      <none>        443/TCP        33d
ng-deploy-80   NodePort    10.0.104.36   <none>        80:30082/TCP   21s

root@k8-deploy:~/k8s-yaml/probe# curl -I 192.168.2.17:30082
HTTP/1.1 200 OK
Server: nginx/1.21.1
Date: Thu, 21 Oct 2021 10:08:57 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 06 Jul 2021 14:59:17 GMT
Connection: keep-alive
ETag: "60e46fc5-264"
Accept-Ranges: bytes

TCPSocketAction

# 创建yaml
root@k8-deploy:~/k8s-yaml/probe# cat TCPSocketAction.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels: #rs or deployment
      app: ng-deploy-80
    #matchExpressions:
    #  - {key: app, operator: In, values: [ng-deploy-80,ng-rs-81]}
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx:1.21.1
        ports:
        - containerPort: 80
        livenessProbe:
          tcpSocket:
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 3
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3

        readinessProbe:
          tcpSocket:
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 3
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
      
---
apiVersion: v1
kind: Service
metadata:
  name: ng-deploy-80 
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
    nodePort: 30083
    protocol: TCP
  type: NodePort
  selector:
    app: ng-deploy-80

# 创建
root@k8-deploy:~/k8s-yaml/probe# kubectl apply -f TCPSocketAction.yaml 
deployment.apps/nginx-deployment created

# 验证
root@k8-deploy:~/k8s-yaml/probe# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7584b6d9f4-c6zxf   1/1     Running   0          8s

root@k8-deploy:~/k8s-yaml/probe# kubectl get svc
NAME           TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes     ClusterIP   10.0.0.1      <none>        443/TCP        33d
ng-deploy-80   NodePort    10.0.167.50   <none>        80:30083/TCP   16s

root@k8-deploy:~/k8s-yaml/probe# kubectl get ep
NAME           ENDPOINTS                                               AGE
kubernetes     192.168.2.11:6443,192.168.2.12:6443,192.168.2.13:6443   33d
ng-deploy-80   10.100.172.232:80                                       93s

ExecAction

# 编写yaml
root@k8-deploy:~/k8s-yaml/probe# cat ExecAction.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-deployment
spec:
  replicas: 1
  selector:
    matchLabels: #rs or deployment
      app: redis-deploy-6379
    #matchExpressions:
    #  - {key: app, operator: In, values: [redis-deploy-6379,ng-rs-81]}
  template:
    metadata:
      labels:
        app: redis-deploy-6379
    spec:
      containers:
      - name: redis-deploy-6379
        image: redis
        ports:
        - containerPort: 6379
        readinessProbe:
          exec:
            command:
            - /usr/local/bin/redis-cli
            - quit
          initialDelaySeconds: 5
          periodSeconds: 3
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3

        livenessProbe:
          exec:
            command:
            - /usr/local/bin/redis-cli
            - quit
          initialDelaySeconds: 5
          periodSeconds: 3
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
        readinessProbe:
          exec:
            command:
            - /usr/local/bin/redis-cli
            - quit
          initialDelaySeconds: 5
          periodSeconds: 3
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
---
apiVersion: v1
kind: Service
metadata:
  name: redis-deploy-6379 
spec:
  ports:
  - name: http
    port: 6379
    targetPort: 6379
    nodePort: 40016
    protocol: TCP
  type: NodePort
  selector:
    app: redis-deploy-6379

# 创建
root@k8-deploy:~/k8s-yaml/probe# kubectl apply -f ExecAction.yaml 
deployment.apps/redis-deployment created

# 验证
root@k8-deploy:~/k8s-yaml/probe# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
redis-deployment-775c65c67c-dwp7r   1/1     Running   0          2m12s

root@k8-deploy:~/k8s-yaml/probe# kubectl describe pod redis-deployment-775c65c67c-dwp7r
...
    Liveness:       exec [/usr/local/bin/redis-cli quit] delay=5s timeout=5s period=3s #success=1 #failure=3
    Readiness:      exec [/usr/local/bin/redis-cli quit] delay=5s timeout=5s period=3s #success=1 #failure=3
...

标签:容器,deploy,探针,ng,yaml,Pod,7.5,pod,80
来源: https://www.cnblogs.com/yanql/p/15434119.html