健康检查

探针的种类

livenessProbe:健康状态检查,周期性检查服务是否存活,检查结果失败,将重启容器

readinessProbe:可用性检查,周期性检查服务是否可用,不可用将从service的endpoints中移除(在启动时检查,只要不通过就不会加入负载均衡)

探针的检查方法

  • exec:执行一段命令,如果命令的返回值是0,代表是好的
  • httpGet:检测某个 http 请求的返回状态码,
  • tcpSocket:测试某个端口是否能够连接,

liveness探针的exec使用

vi  nginx_pod_exec.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: exec
spec:
  containers:
    - name: nginx
      image: 10.0.0.11:5000/nginx:1.13
      ports:
        - containerPort: 80
      args:
        - /bin/sh
        - -c
        - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
      livenessProbe:
        exec:                                  ### 使用下面cat命令进行测试
          command:
            - cat
            - /tmp/healthy                  ### 有这个文件就代表OK
        initialDelaySeconds: 5       ### 启动5秒后进行检查
        periodSeconds: 5              ### 间隔5秒

liveness探针的httpGet使用

vi   nginx_pod_httpGet.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: httpget
spec:
  containers:
    - name: nginx
      image: 10.0.0.11:5000/nginx:1.13
      ports:
        - containerPort: 80
      livenessProbe:
        httpGet:
          path: /index.html               ### 请求这个页面查看返回结果
          port: 80
        initialDelaySeconds: 3
        periodSeconds: 3

liveness探针的tcpSocket使用

vi   nginx_pod_tcpSocket.yaml
apiVersion: v1
kind: Pod
metadata:
  name: tcpSocket
spec:
  containers:
    - name: nginx
      image: 10.0.0.11:5000/nginx:1.13
      ports:
        - containerPort: 80
      livenessProbe:
        tcpSocket:
          port: 80                                  ###是否能连接80端口
        initialDelaySeconds: 3
        periodSeconds: 3

readiness探针的httpGet使用

vi   nginx-rc-httpGet.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: readiness
spec:
  replicas: 2
  selector:
    app: readiness
  template:
    metadata:
      labels:
        app: readiness
    spec:
      containers:
      - name: readiness
        image: 10.0.0.11:5000/nginx:1.13
        ports:
        - containerPort: 80
        readinessProbe:
          httpGet:
            path: /xinge.html      ###容器里如果没有这个文件就代表没准备好不加入
            port: 80
          initialDelaySeconds: 3
          periodSeconds: 3

你可能感兴趣的:(健康检查)