云服务器Harbor私有镜像仓库搭建

一、环境准备

1. 系统配置

bash
# 安装Docker
apt update
apt install -y docker.io docker-compose

# 系统配置优化
cat >> /etc/sysctl.conf << EOF
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-iptables=1
EOF

sysctl -p

2. 证书配置

bash
# 生成SSL证书
mkdir -p /data/cert
cd /data/cert

openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 365 -out ca.crt \
-subj "/C=CN/ST=Beijing/L=Beijing/O=Company/CN=harbor.example.com"

# 配置Docker证书
mkdir -p /etc/docker/certs.d/harbor.example.com/
cp ca.crt /etc/docker/certs.d/harbor.example.com/

二、Harbor安装

1. 下载配置

bash
# 下载Harbor
wget https://github.com/goharbor/harbor/releases/download/v2.5.0/harbor-online-installer-v2.5.0.tgz
tar xvf harbor-online-installer-v2.5.0.tgz

# 配置Harbor
cp harbor.yml.tmpl harbor.yml

2. 基础配置

yaml
# harbor.yml
hostname: harbor.example.com
https:
certificate: /data/cert/ca.crt
private_key: /data/cert/ca.key
harbor_admin_password: Harbor12345
database:
password: root123
max_idle_conns: 100
max_open_conns: 900
data_volume: /data/harbor

三、安全配置

1. 身份认证

yaml
# harbor.yml
auth_mode: ldap
ldap_url: ldap://ldap.example.com
ldap_searchdn: cn=admin,dc=example,dc=com
ldap_search_pwd: password
ldap_base_dn: dc=example,dc=com
ldap_uid: uid

2. 项目安全

bash
# 配置项目访问级别
curl -X PUT -H "Content-Type: application/json" \
-u "admin:Harbor12345" \
"https://harbor.example.com/api/v2.0/projects/1" \
-d '{"metadata":{"public":"false"}}'

四、仓库管理

1. 仓库配置

bash
# 创建项目
curl -X POST -H "Content-Type: application/json" \
-u "admin:Harbor12345" \
"https://harbor.example.com/api/v2.0/projects" \
-d '{"project_name":"test-project","public":false}'

# 配置镜像清理
./harbor.sh garbage-collection

2. 复制策略

yaml
# 配置镜像复制
curl -X POST -H "Content-Type: application/json" \
-u "admin:Harbor12345" \
"https://harbor.example.com/api/v2.0/replication/policies" \
-d '{
"name": "sync-policy",
"src_registry": {"id": 1},
"dest_registry": {"id": 2},
"filters": [{
"type": "name",
"value": "test-project/**"
}]
}'

五、性能优化

1. Nginx配置

nginx
# nginx.conf
worker_processes auto;
worker_rlimit_nofile 65535;

events {
worker_connections 10240;
}

http {
client_max_body_size 500m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
}

2. 存储优化

yaml
# harbor.yml
storage:
filesystem:
maxthreads: 50
cache:
enabled: true
expiry: 24h

六、监控告警

1. 监控配置

yaml
# prometheus.yml
scrape_configs:
- job_name: 'harbor'
static_configs:
- targets: ['harbor.example.com']
metrics_path: '/metrics'

2. 告警规则

yaml
# alert.rules
groups:
- name: harbor_alerts
rules:
- alert: HarborDown
expr: up == 0
for: 5m
labels:
severity: critical

最佳实践建议

  1. 安全建议
  • 启用HTTPS
  • 配置访问控制
  • 定期更新密码
  • 启用镜像扫描
  1. 性能优化
  • 合理配置资源
  • 启用缓存机制
  • 定期清理空间
  • 优化网络配置
  1. 运维管理
  • 配置自动备份
  • 监控系统状态
  • 日志管理
  • 制定更新策略

本指南为您提供了在云服务器上搭建Harbor私有镜像仓库的完整方案。记住,镜像仓库的安全性和可靠性至关重要,需要持续的维护和优化。

重要提示:

  1. 定期更新Harbor版本
  2. 监控存储空间
  3. 备份关键数据
  4. 优化访问性能

对于生产环境的Harbor平台,建议建立完善的监控告警机制,确保服务的稳定运行。同时,要做好数据备份和灾难恢复预案,保证镜像数据的安全性。

实操指南知识库

云服务器SonarQube代码质量平台部署与配置

2024-12-17 17:09:28

实操指南知识库

云服务器Grafana+VictoriaMetrics监控方案

2024-12-18 13:52:43

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧