Kubernetes必知必会(二)

转载请注明出处:https://hts0000.github.io/

欢迎与我联系:hts_0000@sina.com

Kubernetes应用存储和持久化

Volumes

Kubernetes观测性

Liveness

Liveness是存活指针,Kubernetes用其来探测Pod是否存活,如果不存活则会杀掉当前Pod,由该节点上的Kubelet根据Pod的重启策略(Always/OnFailure/Never),决定是否重新拉起该Pod。重启策略默认值是AlwaysLiveness不配置时,一直认为探活成功,这时Pod是否正常就托管给了Kubernetes来检测,而Kubernetes则是会在Pod生命周期状态改变为异常时,才会让Kubelet根据重启策略重启Pod。也就是说Liveness使得用户有了一种手段,可以自定义检测Pod是否正常,从而让Kubernetes来执行这个自定义的检测。

Liveness支持如下检测方式:

  • exec:shell命令行检测,返回0检测成功,返回非0检测失败
  • httpGet:http接口检测,返回200~399检测成功,其他检测失败
  • tcpSocket:tcpsock检测,连通则检测成功,无法连通则失败
  • grpc:需要应用支持GRPC健康检测协议

Liveness模板

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
apiVersion: v1
kind: Pod
metadata:
  name: liveness-pod
  labels:
    app: liveness-pod
spec:
  containers:
  - image: busybox
    name: liveness-pod
    command: ["sh", "-c", "sleep 1000000"]
    livenessProbe:
      exec:     # 使用shell的方式探测
        command: ["sh", "-c", "exit 1"]     # 让其直接检测失败
    # httpGet:      # 使用httpGet的方式探测
    #   path: /healthz  # 探测路径
    #   port: 8080      # http端口
    #   httpHeaders:    # 设置探测时带上的http header
    #   - name: Custom-Header
    #     value: Awesome
    # tcpSocket:    # 使用tcpSocket的方式探测
    #   port: 8080  # 探测端口
      initialDelaySeconds: 10   # pod启动后延迟10s再开始探测
      periodSeconds: 5     # 探测间隔

Readiness

Readiness是就绪探针,Kubernetes用其探测Pod是否就绪,探测成功后才将其加入ServiceEndpoint中,探测失败了则将其从Endpoint中删除。也就是说Readiness是和Service绑定使用的,探测失败了则认为该Pod无法处理请求,相当于做了集群故障节点剔除。

Readiness模板
就绪探针的配置和存活探针的配置相似。 唯一区别就是要使用readinessProbe字段,而不是livenessProbe字段。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
apiVersion: v1
kind: Pod
metadata:
  name: readiness-pod
  labels:
    app: readiness-pod
spec:
  containers:
  - image: busybox
    name: readiness-pod
    command: ["sh", "-c", "sleep 1000000"]
    readinessProbe:
      exec:     # 使用shell的方式探测
        command: ["sh", "-c", "exit 1"]     # 让其直接检测失败
    # httpGet:      # 使用httpGet的方式探测
    #   path: /healthz  # 探测路径
    #   port: 8080      # http端口
    #   httpHeaders:    # 设置探测时带上的http header
    #   - name: Custom-Header
    #     value: Awesome
    # tcpSocket:    # 使用tcpSocket的方式探测
    #   port: 8080  # 探测端口
      initialDelaySeconds: 10   # pod启动后延迟10s再开始探测
      periodSeconds: 5     # 探测间隔

Startup

Startup是启动探针,用于检测Pod是否启动成功。在指定时间期间内探测成功,才启动探活指针。


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: v1
kind: Pod
metadata:
  name: readiness-pod
  labels:
    app: readiness-pod
spec:
  containers:
  - image: busybox
    name: readiness-pod
    command: ["sh", "-c", "sleep 1000000"]
    startupProbe:
      httpGet:
        path: /healthz
        port: liveness-port
      failureThreshold: 30
      periodSeconds: 10

Kubernetes监控和日志