Shell脚本如何配置容器存活探针

wen 实用脚本 1

本文目录导读:

Shell脚本如何配置容器存活探针

  1. 在Dockerfile中使用HEALTHCHECK指令
  2. 在Kubernetes中配置存活探针
  3. 完整的健康检查脚本示例
  4. Docker Compose配置示例
  5. 高级探针脚本(带重试和日志)
  6. 最佳实践建议

在Shell脚本中配置容器存活探针(Liveness Probe),通常是在Dockerfile或容器编排工具(如Kubernetes)中进行的,以下是几种常见场景的实现方法:

在Dockerfile中使用HEALTHCHECK指令

FROM alpine:latest
# 安装必要的工具
RUN apk add --no-cache curl
# 自定义健康检查脚本
COPY healthcheck.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/healthcheck.sh
# 配置HEALTHCHECK
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD /usr/local/bin/healthcheck.sh

healthcheck.sh示例:

#!/bin/sh
# 检查应用程序是否存活
# 检查进程是否存在
if pgrep -x "myapp" > /dev/null; then
    echo "应用程序运行正常"
    exit 0
fi
# 或者检查HTTP端点
# if curl -f http://localhost:8080/health; then
#     exit 0
# fi
echo "应用程序未运行"
exit 1

在Kubernetes中配置存活探针

使用shell命令探针:

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
spec:
  containers:
  - name: myapp
    image: myapp:latest
    livenessProbe:
      exec:
        command:
        - /bin/sh
        - -c
        - |
          # 检查进程是否存在
          if pgrep -x "myapp" > /dev/null; then
            exit 0
          fi
          # 或者检查文件锁
          if [ -f /tmp/app.lock ]; then
            exit 0
          fi
          exit 1
      initialDelaySeconds: 5
      periodSeconds: 10
      timeoutSeconds: 3
      failureThreshold: 3

使用外部脚本探针:

livenessProbe:
  exec:
    command:
    - /bin/sh
    - /scripts/healthcheck.sh
  initialDelaySeconds: 5
  periodSeconds: 10

完整的健康检查脚本示例

#!/bin/bash
# 配置
APP_NAME="myapp"
HEALTH_CHECK_URL="http://localhost:8080/health"
LOG_FILE="/var/log/healthcheck.log"
check_process() {
    if pgrep -x "$APP_NAME" > /dev/null; then
        return 0
    fi
    return 1
}
check_http() {
    if curl -sf "$HEALTH_CHECK_URL" > /dev/null 2>&1; then
        return 0
    fi
    return 1
}
check_disk_space() {
    local threshold=90
    local usage=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
    if [ "$usage" -lt "$threshold" ]; then
        return 0
    fi
    return 1
}
# 主检查逻辑
main() {
    # 记录检查时间
    echo "$(date): 开始健康检查" >> "$LOG_FILE"
    # 执行多个检查
    if check_process && check_http && check_disk_space; then
        echo "$(date): 健康检查通过" >> "$LOG_FILE"
        exit 0
    else
        echo "$(date): 健康检查失败" >> "$LOG_FILE"
        exit 1
    fi
}
main

Docker Compose配置示例

version: '3.8'
services:
  app:
    image: myapp:latest
    healthcheck:
      test: ["CMD-SHELL", "/usr/local/bin/healthcheck.sh"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

高级探针脚本(带重试和日志)

#!/bin/bash
# 配置
MAX_RETRIES=3
RETRY_INTERVAL=2
TIMEOUT=5
check_service() {
    local retries=0
    while [ $retries -lt $MAX_RETRIES ]; do
        # 尝试检查服务
        if timeout $TIMEOUT nc -z localhost 8080 2>/dev/null; then
            echo "服务正常"
            return 0
        fi
        retries=$((retries + 1))
        echo "检查失败,重试 $retries/$MAX_RETRIES"
        sleep $RETRY_INTERVAL
    done
    echo "服务检查失败"
    return 1
}
# 执行检查
if check_service; then
    exit 0
else
    exit 1
fi

最佳实践建议

  1. 探针不要太复杂:保持简单,只检查必要的存活条件
  2. 避免依赖外部服务:存活探针应该只检查本容器状态
  3. 合理设置超时和重试:避免误判
  4. 使用日志记录:便于问题排查
  5. 考虑资源消耗:探针执行频率不宜过高

选择哪种方式取决于你的部署环境和应用需求,Kubernetes中使用exec探针是最灵活的方式。

抱歉,评论功能暂时关闭!