Tối ưu hóa hiệu năng và bảo mật cho Harbor trong môi trường Multi-Cloud
Bắt đầu bằng việc cấu hình caching và CDN để giảm độ trễ khi pull image cho các vùng địa lý xa.
Mục tiêu là giảm tải cho registry chính và tăng tốc độ phân phối artifact cho người dùng cuối thông qua mạng phân phối nội dung (CDN) hoặc proxy cache.
Chúng ta sẽ triển khai một instance Harbor Proxy Cache hoặc cấu hình Nginx làm reverse proxy với module cache.
Sử dụng Harbor Proxy Cache (chế độ read-only) để đồng bộ artifact từ registry chính và phục vụ local.
Chạy container Harbor Proxy Cache với cấu hình cache backend là Redis hoặc local disk.
docker run -d \
--name harbor-proxy-cache \
-p 80:80 \
-v /opt/harbor/cache:/var/cache \
-e CACHE_MODE=readonly \
-e UPSTREAM_REGISTRY="https://registry.harbor-main.com" \
-e REGISTRY_USER="admin" \
-e REGISTRY_PASSWORD="HarborAdmin123" \
goharbor/harbor-proxy-cache:latest
Kết quả mong đợi: Container chạy thành công, log hiển thị kết nối thành công với upstream registry và bắt đầu đồng bộ manifest.
Để tăng tốc độ hơn nữa trong môi trường Multi-Cloud, cấu hình Nginx làm reverse proxy với caching layer trước Harbor Gateway.
Tạo file cấu hình Nginx cache tại đường dẫn /etc/nginx/conf.d/harbor-cache.conf.
upstream harbor_upstream {
server 10.0.1.50:8080; # Internal IP of Harbor Gateway
}
proxy_cache_path /var/cache/nginx/harbor levels=1:2 keys_zone=harbor_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name registry.multi-cloud.com;
location /v2/ {
proxy_pass https://harbor_upstream;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache harbor_cache;
proxy_cache_valid 200 10m;
proxy_cache_valid 404 1m;
add_header X-Cache-Status $upstream_cache_status;
}
# Bypass cache for login/management
location ~* ^/(api|account|login) {
proxy_pass https://harbor_upstream;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_no_cache 1;
proxy_cache_bypass 1;
}
}
Kết quả mong đợi: Nginx reload thành công, các request GET manifest được trả về với header X-Cache-Status: HIT từ cache, giảm tải server gốc.
Áp dụng VPC Peering và Private Link để giảm nguy cơ tấn công
Cấu hình mạng để registry chỉ truy cập được qua mạng riêng (Private) thay vì công khai qua Internet.
Mục đích là loại bỏ vector tấn công từ internet, chỉ cho phép các VPC trong hệ sinh thái Multi-Cloud hoặc on-premise truy cập.
Thực hiện VPC Peering giữa VPC chứa Harbor và VPC chứa các cluster Kubernetes người dùng.
Trên AWS, tạo kết nối peering giữa VPC nguồn (Harbor VPC) và VPC đích (K8s VPC).
aws ec2 create-vpc-peering-connection \
--vpc-id vpc-harbor-0123456789abcdef \
--vpc-id vpc-k8s-0987654321fedcba \
--region ap-southeast-1 \
--tags Key=Name,Value=HarborToK8sPeering
Kết quả mong đợi: Trạng thái peering chuyển sang active, cho phép traffic đi qua giữa hai VPC.
Cấu hình Private Link (VPC Endpoint) để truy cập Harbor qua AWS PrivateLink, tránh sử dụng public IP.
Tạo VPC Endpoint Service cho Harbor Gateway nếu chạy trên AWS hoặc sử dụng AWS PrivateLink để kết nối.
aws ec2 create-vpc-endpoint \
--vpc-id vpc-k8s-0987654321fedcba \
--service-name com.amazonaws.ap-southeast-1.elasticloadbalancing \
--vpc-endpoint-type Interface \
--private-dns-enabled \
--subnet-ids subnet-0123456789abcdef \
--security-group-ids sg-harbor-access \
--region ap-southeast-1
Kết quả mong đợi: DNS của Harbor resolve đến Private IP (10.x.x.x hoặc 172.x.x.x), truy cập registry từ cluster K8s không đi qua internet public.
Cấu hình Security Group của Harbor Gateway chỉ cho phép traffic từ Private IP của các VPC đã peering.
aws ec2 authorize-security-group-ingress \
--group-id sg-harbor-gateway \
--protocol tcp \
--port 443 \
--cidr 10.0.0.0/16
Kết quả mong đợi: Request từ public IP bị chặn, chỉ request từ VPC nội bộ mới được chấp nhận kết nối TCP port 443.
Tối ưu hóa cấu hình Database và Storage Backend
Điều chỉnh tham số Postgres và Storage để xử lý tải cao và tăng tốc độ I/O.
Mục tiêu là giảm thời gian truy vấn metadata và tăng thông lượng khi upload/pull image lớn.
Cấu hình Postgres trong file values.yaml của Harbor Helm Chart hoặc file harbor.yml tùy chỉnh.
Chỉnh sửa file /etc/harbor/harbor.yml (hoặc values.yaml nếu dùng Helm) để tăng bộ nhớ và CPU cho Postgres.
database:
max_connections: 500
idle_timeout: 300
statement_timeout: 30000
# Nếu dùng Helm, chỉnh trong values.yaml:
# postgres:
# resources:
# limits:
# cpu: 2
# memory: 4Gi
# requests:
# cpu: 1
# memory: 2Gi
Kết quả mong đợi: Database có thể xử lý 500 connection đồng thời, giảm lỗi timeout khi scan artifact hoặc sync replication.
Tối ưu hóa Postgres với lệnh trực tiếp trên database container nếu cần điều chỉnh runtime.
docker exec -it harbor-postgres psql -U harbor -c "ALTER SYSTEM SET shared_buffers = '256MB';"
docker exec -it harbor-postgres psql -U harbor -c "ALTER SYSTEM SET work_mem = '16MB';"
docker exec -it harbor-postgres psql -U harbor -c "ALTER SYSTEM SET maintenance_work_mem = '128MB';"
docker exec -it harbor-postgres psql -U harbor -c "ALTER SYSTEM SET effective_cache_size = '1GB';"
docker restart harbor-postgres
Kết quả mong đợi: Database restart thành công, các query scan repository chạy nhanh hơn đáng kể.
Cấu hình Storage Backend (S3/OSS/MinIO) để tối ưu I/O và giảm chi phí lưu trữ.
Chỉnh sửa phần storage trong harbor.yml để sử dụng S3 với multipart upload tối ưu.
storage:
type: s3
s3:
region: ap-southeast-1
bucket: harbor-artifacts-multi-cloud
endpoint: https://s3.ap-southeast-1.amazonaws.com
accesskey: YOUR_AWS_ACCESS_KEY
secretkey: YOUR_AWS_SECRET_KEY
v4auth: true
chunksize: 10485760 # 10MB chunk size for faster upload
Kết quả mong đợi: Upload image lớn (>2GB) thành công mà không bị timeout, tốc độ truyền tải ổn định.
Verify hiệu năng bằng cách chạy benchmark upload/pull image lớn.
docker pull alpine:latest
docker tag alpine:latest registry.multi-cloud.com/library/alpine:test-perf
docker push registry.multi-cloud.com/library/alpine:test-perf
time docker pull registry.multi-cloud.com/library/alpine:test-perf
Kết quả mong đợi: Thời gian pull giảm so với cấu hình mặc định, không có lỗi I/O timeout.
Cấu hình Rate Limiting để chống tấn công DDoS
Áp dụng giới hạn tốc độ request vào Harbor Gateway để ngăn chặn DDoS và brute force.
Mục tiêu là chặn các request bất thường từ một IP duy nhất trong khi vẫn cho phép CI/CD pipeline hoạt động mượt mà.
Sử dụng Nginx Ingress Controller hoặc Nginx upstream để cấu hình rate limiting.
Tạo file cấu hình rate limiting trong Nginx tại /etc/nginx/conf.d/rate-limit.conf.
limit_req_zone $binary_remote_addr zone=harbor_limit:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=harbor_conn:10m;
server {
listen 80;
server_name registry.multi-cloud.com;
# Apply rate limit for API and image pull
location /v2/ {
limit_req zone=harbor_limit burst=20 nodelay;
limit_conn harbor_conn 10;
proxy_pass https://harbor_upstream;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# Stricter limit for login
location /api/v2.0/users/ {
limit_req zone=harbor_limit burst=5 nodelay;
proxy_pass https://harbor_upstream;
}
}
Kết quả mong đợi: Nginx trả về lỗi 503 Service Temporarily Unavailable khi người dùng gửi request vượt quá 10 req/s.
Triển khai Redis để lưu trữ state của rate limiter nếu dùng Nginx Ingress trên Kubernetes (cần module ngx_http_limit_req_module với Redis backend).
Sử dụng Helm Values để cấu hình Nginx Ingress với Redis rate limiter.
controller:
config:
limit-rate: "10r/s"
limit-req-status-code: "429"
extraArgs:
enable-redis-rate-limit: "true"
redis:
enabled: true
replicaCount: 1
resources:
requests:
memory: 256Mi
cpu: 100m
Kết quả mong đợi: Rate limiter hoạt động phân tán trên nhiều pod Nginx, không bị reset khi pod restart.
Verify bằng cách tạo script gửi request nhanh và kiểm tra log Nginx.
for i in {1..50}; do curl -o /dev/null -s -w "%{http_code}\n" http://registry.multi-cloud.com/v2/_catalog; done | sort | uniq -c
Kết quả mong đợi: Xuất hiện mã 429 Too Many Requests hoặc 503 sau một vài request đầu tiên, log Nginx ghi nhận limiting requests.
Triển khai WAF (Web Application Firewall) trước Harbor Gateway
Lắp đặt WAF để lọc traffic độc hại, SQL Injection, XSS và các pattern tấn công đặc thù vào container registry.
Mục tiêu là tạo lớp bảo vệ ứng dụng (Layer 7) trước khi request đến Harbor.
Sử dụng ModSecurity với OWASP Core Rule Set (CRS) trên Nginx hoặc triển khai AWS WAF nếu dùng Public Endpoint.
Cài đặt ModSecurity module trên Nginx và cấu hình rule set.
apt-get update
apt-get install -y libmodsecurity-dev modsecurity-nginx
apt-get install -y wget
cd /usr/share/modsecurity-crs/
wget https://github.com/coreruleset/coreruleset/archive/refs/tags/3.3.3.tar.gz
tar -xzf coreruleset-3.3.3.tar.gz
mv coreruleset-3.3.3/* /usr/share/modsecurity-crs/
Kết quả mong đợi: ModSecurity module được cài đặt, rule set OWASP CRS có sẵn trong thư mục hệ thống.
Cấu hình file ModSecurity cho Harbor tại /etc/nginx/conf.d/modsecurity-harbor.conf.
load_module modsecurity.so;
modsecurity on;
ModSecurityConfig /etc/modsecurity/modsecurity.conf;
ModSecurityConfig /etc/modsecurity-crs/coreruleset.conf;
# Whitelist Harbor API paths to avoid false positives
SecRule REQUEST_URI "@streq /v2/_catalog" "id:10001,pass,nolog,ctl:ruleRemoveTargetById=942100;ctl:ruleRemoveTargetById=942110"
SecRule REQUEST_URI "@streq /v2/_catalog" "id:10002,pass,nolog,ctl:ruleRemoveById=920300"
# Block common registry attacks
SecRule REQUEST_METHOD "^(TRACE|TRACK)" "id:20001,deny,status:403,msg:'Disallowed HTTP Method'"
SecRule ARGS "@rx \x00" "id:20002,deny,status:403,msg:'Null Byte Injection'"
Kết quả mong đợi: Nginx load module thành công, log ModSecurity ghi nhận các request bị chặn hoặc pass qua whitelist.
Triển khai AWS WAF nếu Harbor Gateway nằm sau Application Load Balancer (ALB).
aws wafv2 create-web-acl \
--name Harbor-WAF-ACL \
--scope REGIONAL \
--default-action Allow \
--visibility-config SampledRequestsEnabled=true,CloudWatchMetricsEnabled=true,MetricName=HarborWAF \
--idempotency-token $(date +%s) \
--statement "SqliMatchStatement":{ "FieldToMatch":{ "Body":{} }, "TextTransformations":[{ "Priority":0, "Type":"URL_DECODE" }] } \
--region ap-southeast-1
Kết quả mong đợi: Web ACL được tạo, có thể attach vào ALB để lọc traffic SQL Injection vào body request.
Verify WAF bằng cách gửi payload tấn công giả lập.
curl -X POST http://registry.multi-cloud.com/v2/_catalog -d "SELECT * FROM users WHERE id=1 OR 1=1" -H "Content-Type: application/json"
Kết quả mong đợi: Request bị chặn với mã 403 Forbidden và log ModSecurity hoặc AWS WAF ghi nhận chặn rule SQL Injection.
Điều hướng series:
Mục lục: Series: Series: Xây dựng nền tảng Secure Multi-Cloud Container Registry với Harbor, Notary và Policy Engine để kiểm soát phân phối artifact an toàn
« Phần 6: Xây dựng Policy Enforcement để chặn phân phối artifact không an toàn
Phần 8: Xây dựng pipeline CI/CD tích hợp Secure Artifact Delivery »