赞
踩
生产环境中一定要给pod设置探针,不然pod内的应用发生异常时,K8s将不会重启pod。
需要遵循以下几个原则(本人自己总结,仅供参考):
探针尽量简单
,不要消耗过多资源。因为探针较为频繁的定期执行,过于复杂和消耗资源的探针对k8s和生产环境是不利的。尽量代表pod的健康状态
,而不是简单的返回。可以适当做一些pod健康性检查。特别要避免探针返回了健康状态,但实际pod处于异常状态。不要受外部组件影响
。比如pod连接了一个数据服务,如果数据服务异常,此时探针不要返回异常。不然k8s认为pod处于异常,不停重启pod,但这种操作对于修复异常没有任何帮助,反而不停拖累k8s。常用的探针有三种:
每种探针都有多个可配置的参数,可以帮助我们在生产环境中更细致地监控和管理 Pod。以下是一个详细的 Pod 配置示例,其中包含了所有三种探针及其常用参数:
apiVersion: v1 kind: Pod metadata: name: example-pod spec: containers: - name: example-container image: example-image:latest ports: - containerPort: 8080 # 存活探针(Liveness Probe)配置 livenessProbe: httpGet: path: /healthz port: 8080 httpHeaders: - name: Custom-Header value: Awesome initialDelaySeconds: 30 periodSeconds: 10 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 3 # 就绪探针(Readiness Probe)配置 readinessProbe: httpGet: path: /ready port: 8080 initialDelaySeconds: 5 periodSeconds: 10 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 3 # 启动探针(Startup Probe)配置 startupProbe: httpGet: path: /startup port: 8080 initialDelaySeconds: 0 periodSeconds: 10 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 30 # 其他探针类型(如TCP和命令执行)示例 livenessProbe: tcpSocket: port: 8080 initialDelaySeconds: 30 periodSeconds: 10 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 3 readinessProbe: exec: command: ["sh", "-c", "echo 'hello'"] initialDelaySeconds: 5 periodSeconds: 10 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 3
通过合理配置探针及其参数,可以有效地监控和管理 Pod 的状态,确保应用在生产环境中的高可用性和可靠性。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。