Files
macos-tools/harbor/scripts/enable-transparent-proxy.sh
gaoandClaude 8f47f4e9ea feat: 完善 DevOps 工具链配置和优化 Harbor 服务
- 更新 Gitea runner 配置,支持优化的 CI/CD 环境
- 完善 Harbor HTTPS 配置和远程访问支持
- 优化 Harbor 日志配置,改用 json-file 驱动
- 添加 Docker 构建加速和代理缓存脚本
- 新增完整的文档和使用指南
- 添加 AI 开发工具集成配置

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
2025-11-03 06:39:53 +08:00

256 lines
7.1 KiB
Bash
Executable File

#!/bin/bash
# Harbor 透明代理配置脚本
# 让标准镜像路径自动通过 Harbor 加速
set -e
HARBOR_REGISTRY="192.168.1.83:8443"
echo "🚀 配置 Harbor 透明代理"
echo "Harbor 地址: $HARBOR_REGISTRY"
echo ""
# 检测操作系统
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
SETUP_LINUX=true
SETUP_MACOS=false
elif [[ "$OSTYPE" == "darwin"* ]]; then
SETUP_MACOS=true
SETUP_LINUX=false
else
echo "❌ 不支持的操作系统: $OSTYPE"
exit 1
fi
# macOS 配置 (OrbStack)
setup_macos_docker() {
echo "🍎 配置 macOS Docker 透明代理"
echo ""
echo "⚠️ OrbStack 不支持 daemon.json 配置"
echo " 建议使用以下方案:"
echo ""
echo "1. 使用 Docker Desktop (如果可用)"
echo "2. 使用 Linux 虚拟机"
echo "3. 继续使用显式代理方案"
echo ""
echo "💡 当前最佳方案:"
echo " 使用 harbor-sync.sh 同步常用镜像到 Harbor"
echo " 然后使用 harbor-get.sh 快速拉取"
return 1
}
# Linux Docker 配置
setup_linux_docker() {
echo "🐧 配置 Linux Docker 镜像加速"
# 备份原配置
if [ -f /etc/docker/daemon.json ]; then
sudo cp /etc/docker/daemon.json /etc/docker/daemon.json.backup.$(date +%Y%m%d_%H%M%S)
echo "✅ 已备份原配置文件"
fi
# 创建新配置
sudo tee /etc/docker/daemon.json > /dev/null <<EOF
{
"registry-mirrors": [
"https://${HARBOR_REGISTRY}"
],
"insecure-registries": [
"${HARBOR_REGISTRY}"
],
"max-concurrent-downloads": 10,
"max-concurrent-uploads": 5,
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
},
"experimental": false,
"features": {
"buildkit": true
}
}
EOF
echo "✅ Docker daemon 配置完成"
# 重启 Docker 服务
echo "🔄 重启 Docker 服务..."
sudo systemctl restart docker
# 等待服务启动
echo "⏳ 等待 Docker 服务启动..."
sleep 5
# 验证配置
if docker info | grep -q "Registry Mirrors"; then
echo "✅ Docker 镜像加速配置成功"
docker info | grep -A 5 "Registry Mirrors"
else
echo "❌ Docker 镜像加速配置失败"
return 1
fi
}
# Containerd 配置 (K8s 节点)
setup_containerd() {
echo "📦 配置 Containerd (Kubernetes 支持)"
# 备份原配置
if [ -f /etc/containerd/config.toml ]; then
sudo cp /etc/containerd/config.toml /etc/containerd/config.toml.backup.$(date +%Y%m%d_%H%M%S)
echo "✅ 已备份 Containerd 配置"
fi
# 创建配置目录
sudo mkdir -p /etc/containerd
# 生成新配置
sudo tee /etc/containerd/config.toml > /dev/null <<'EOF'
# Containerd 配置文件 - Harbor 透明代理
version = 2
[plugins]
[plugins."io.containerd.grpc.v1.cri".registry]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://192.168.1.83:8443"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."ghcr.io"]
endpoint = ["https://192.168.1.83:8443"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."quay.io"]
endpoint = ["https://192.168.1.83:8443"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry.k8s.io"]
endpoint = ["https://192.168.1.83:8443"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"]
endpoint = ["https://192.168.1.83:8443"]
[plugins."io.containerd.grpc.v1.cri".registry.configs]
[plugins."io.containerd.grpc.v1.cri".registry.configs."192.168.1.83:8443".tls]
insecure_skip_verify = true
[plugins."io.containerd.grpc.v1.cri".registry.configs."192.168.1.83:8443".auth]
username = "admin"
password = "admin123"
[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "registry.k8s.io/pause:3.9"
EOF
echo "✅ Containerd 配置完成"
# 重启 Containerd
echo "🔄 重启 Containerd 服务..."
sudo systemctl restart containerd
# 等待服务启动
sleep 3
# 验证配置
if sudo systemctl is-active --quiet containerd; then
echo "✅ Containerd 服务运行正常"
else
echo "❌ Containerd 服务启动失败"
return 1
fi
}
# 测试透明代理
test_transparent_proxy() {
echo ""
echo "🧪 测试透明代理配置"
# 清理本地镜像缓存
echo "🗑️ 清理本地镜像缓存..."
docker system prune -f > /dev/null 2>&1 || true
# 测试 Docker Hub 镜像
echo "📦 测试 Docker Hub 镜像拉取..."
if docker pull alpine:latest > /dev/null 2>&1; then
echo "✅ alpine:latest 拉取成功"
# 检查是否通过 Harbor
if docker images | grep -q "${HARBOR_REGISTRY}"; then
echo "✅ 镜像来自 Harbor 缓存"
else
echo "⚠️ 镜像直接从外网拉取(首次正常)"
fi
else
echo "❌ alpine:latest 拉取失败"
return 1
fi
# 测试其他镜像源
echo "📦 测试其他镜像源..."
test_images=(
"nginx:alpine"
"redis:7-alpine"
"registry.k8s.io/pause:3.9"
)
for image in "${test_images[@]}"; do
echo " 测试: $image"
if docker pull "$image" > /dev/null 2>&1; then
echo " ✅ 成功"
else
echo " ❌ 失败"
fi
done
}
# 主执行流程
main() {
echo "开始配置 Harbor 透明代理..."
echo ""
success=true
if [ "$SETUP_LINUX" = true ]; then
# Linux Docker 配置
if command -v docker > /dev/null 2>&1 && systemctl is-active --quiet docker; then
setup_linux_docker || success=false
else
echo "⚠️ Docker 服务未运行,跳过 Docker 配置"
fi
# Containerd 配置
if command -v containerd > /dev/null 2>&1 && systemctl is-active --quiet containerd; then
setup_containerd || success=false
else
echo "⚠️ Containerd 服务未运行,跳过 Containerd 配置"
fi
# 测试配置
if [ "$success" = true ]; then
test_transparent_proxy
fi
elif [ "$SETUP_MACOS" = true ]; then
setup_macos_docker
fi
echo ""
if [ "$success" = true ]; then
echo "🎉 透明代理配置完成!"
echo ""
echo "💡 现在可以使用标准镜像路径:"
echo " docker pull nginx:latest # 自动通过 Harbor"
echo " docker pull redis:7-alpine # 自动通过 Harbor"
echo " kubectl apply -f deployment.yaml # K8s 自动通过 Harbor"
echo ""
echo "📋 查看 Harbor 缓存:"
echo " curl -k -u admin:admin123 https://192.168.1.83:8443/api/v2.0/projects"
else
echo "❌ 配置过程中遇到问题"
echo "💡 建议使用显式代理方案:"
echo " ./scripts/harbor-sync.sh nginx:alpine tools"
echo " ./scripts/harbor-get.sh tools/nginx:alpine"
fi
echo ""
echo "📚 更多信息: HARBOR_CACHE_OPTIMIZATION.md"
}
# 执行主函数
main "$@"