1. Deployment
Traffic Tool
  • Traffic Tool Docs
    • Tổng quan
      • Tổng Quan
      • Setup Guide
      • System Architecture
      • Code Structure
      • Logs and Monitoring
      • Environment & Configuration
      • Deployment
        • Local
        • Staging
        • Product
    • Bussiness | Logic
      • 1. Thống kê hàng ngày (Daily Statistics)
      • 2. Thống kê hệ thống (System Statistics)
      • 3. Tìm kiếm tự động (Automated Discovery)
      • 4. Trung tâm Báo cáo (Report Center)
      • 5. Quản lý tài khoản (Account Management)
      • 6. Quản lý Hệ thống (System Management)
      • 7. Quản lý tài khoản Google (Google Account Management)
    • Project
      • ADMIN
        • Tổng quan
        • Hướng dẫn sử dung
          • Đăng Nhập & Xác Thực Hai Bước
          • Thống Kê Hệ Thống
          • Thống Kê Hàng Ngày
          • Tìm Kiếm Tự Động
          • Trung Tâm Báo Cáo
          • Quản Lý Tài Khoản
          • Quản Lý Server
          • Quản Lý Proxy
          • Quản Lý Gói Proxy
          • Quản Lý Nhà Cung Cấp Proxy
          • Quản Lý Backup Proxy
          • Quản Lý Tài Khoản Google
          • User Guide
        • Architecture
          • System Architecture
          • Code Structure
          • Environment & Configuration
          • Logs and Monitoring
        • Deployment
          • Local
          • Staging
          • Production
      • API
        • Tổng quan
        • Architecture
          • Database Schema
          • System Architecture
          • Code Structure
          • Environment & Configuration
          • Logs and Monitoring
        • Deployment
          • Local
          • Staging
          • Production
        • API Interface
          • 🔑 Identity & Session
            • Đăng nhập hệ thống (Login)
            • Đăng ký tài khoản mới (Public)
            • Lấy danh sách người dùng (Phân trang)
            • Admin tạo người dùng mới
            • Lấy thông tin cá nhân hiện tại
            • Khởi tạo bảo mật 2FA
            • Xác thực mã OTP
            • Chi tiết người dùng theo ID
            • Cập nhật thông tin người dùng
            • Xóa tài khoản người dùng
            • Đổi mật khẩu
            • Kiểm tra Cấu hình Thông báo Telegram Toàn hệ thống
            • Bật/Tắt Thông báo Telegram Toàn cục
          • 📁 Campaign Management
            • Danh sách Chiến dịch Toàn cầu
            • Khởi tạo Chiến dịch Mới
            • Chi tiết Chiến dịch
            • Cập nhật Chiến dịch
            • Xóa Chiến dịch
            • Tắt/Mở Chiến dịch (Hàng loạt)
            • Dữ liệu Hình mẫu SEO (Negative SEO)
          • ⚙️ Project Mechanics
            • Danh sách Dự án (Projects List)
            • Khởi tạo Kịch bản Mô phỏng
            • Lấy chi tiết cấu hình Dự án
            • Cập nhật Kịch bản chạy
            • Xóa Dự án
            • Chỉnh sửa Hàng loạt (Bulk)
            • Bật/Tắt Dự án
          • 👤 Profile Management
            • Danh sách Vân tay số (Profiles Data)
            • Tạo Hồ sơ Đơn lẻ (Tạo Vân tay mới)
            • Nhập kho Tài khoản Email (Bulk Import)
            • Chi tiết Session/Cookies
            • Chỉnh sửa Hồ sơ/Ghi chú
            • Xóa Vân tay số và Dữ liệu Local
            • Mở khóa Captcha/Trạng thái Blocked
            • Tra cứu Kho Profile Khả dụng
            • Báo cáo Sức khỏe Kho Tài Khoản
          • 🦾 Worker Interface
            • Đăng ký Khởi tạo Node (Handshake)
            • Nhịp Tim Khảo Sát Tình Trạng (Heartbeat)
            • Nhận Cấu hình Bypass & Hệ thống
            • Kéo (PULL) Nhiệm vụ Traffic SEO
            • Báo cáo Sự cố Node (Crash Report)
          • 📊 Report: Execution
            • Kéo Công việc Báo cáo (Worker Pull)
            • Quản lý Giám sát Nhiệm vụ (Task Dashboard)
            • Bắn Lại Báo Cáo Thất Bại (Manual Retry)
          • 📊 Report: Discovery
            • Danh sách Tên miền Chờ Xử Lý (Discovery Pool)
            • Nhập Mục Tiêu Thủ Công (Manual Insert)
            • Kích hoạt Heuristic Scanner (Cào tự động)
            • Chi tiết Bằng Chứng (Evidence Data)
            • Dán nhãn Vi Phạm / Cập nhật Screenshot
            • Loại Bỏ Mục Tiêu
            • Duyệt Yêu Cầu (Approve to Execution)
          • 📊 Report: Platforms Configuration
            • Truy vấn Danh sách Nền tảng Đối tác (Vendor)
            • Tạo Nền tảng Vendor Báo cáo Mới
            • Bật/Tắt Trang báo cáo theo Loại
            • Cập nhật Metadata Trang
            • Xóa Trang Báo Cáo
            • Lấy Cài đặt LLM cho Vendor cụ thể
            • Cập nhật Cài đặt Tạo mẫu LLM
            • Lấy Cấu hình Schema Biểu mẫu cho Vendor
            • Cập nhật Yêu cầu Trường Biểu mẫu
          • 📊 Report: Email Automation
            • Danh sách SMTP/Mailer Server
            • Thêm kết nối SMTP Mới
            • Nhật ký Nhiệm vụ Gửi Mail (Email Log)
            • Phân tích Tỉ lệ Chuyển đổi (Email Delivery Analytics)
          • 🌐 Global Proxies
            • Danh sách Kho Proxy Phân trang (Proxy Pool)
            • Thêm mới Tuyến IP (Bulk Import)
            • Cập nhật Thông tin máy chủ Proxy
            • Xóa Proxy (Thu hồi tài nguyên)
            • Kích hoạt Xoay vòng (Rotate IP) Cưỡng bức
          • 📈 System Intelligence
            • Báo cáo Luồng Bảo mật Hợp nhất (Colossal Report)
            • Bản Đồ Lưu Lượng GeoIP (Heatmap)
          • 🪝 Webhook Integrations
            • Callback Hoàn tất Gói Lưu lượng SEO (Traffic Node)
            • Callback Thông báo Hoàn tất Bắn Report AI
      • Tool
        • Tổng quan
        • Architecture
          • System Architecture
          • Code Structure
          • Environment & Configuration
          • Logs And Monitoring
          • Database Schema
        • Deployment
          • Local
          • Staging
          • Production
        • API interface
          • Tasks
            • Lấy tasks có phân trang và lọc
            • Xóa tất cả tasks
            • Get all tasks
            • Cập nhật một task
            • Xóa một task theo ID
            • Get all tasks
          • Group task
            • Tạo nhiều task groups
            • Lấy tất cả task groups
            • Xóa tất cả task groups
            • Xóa một task group theo ID
            • Endpoint /api/task-traffics/
            • Test parse proxy string
            • Endpoint /api/task-traffics/
          • Test
            • Mock proxy endpoint cho development
            • Mock profile endpoint cho development
            • Endpoint /api/test
            • Endpoint /api/test/account-stats
            • Endpoint /api/test/session-recommendation
            • Endpoint /api/test/check-browser
            • Endpoint /api/test/clear-browser-sessions
          • Proxies
            • Lấy tất cả proxy records
            • Tạo proxy record mới
            • Xóa tất cả proxies
            • Get all proxies
            • Xóa một proxy theo ID
            • Reset proxy process counter
            • Get all proxies
            • Get all proxies
          • Report Platforms
            • Submit report to multiple platforms
            • Submit report to specific platform
            • Get available platforms
            • Get platform statistics
            • Health check
          • Reports
            • Create a new report
            • Create a new report
            • Get report by ID
            • Get report by ID
            • Get report statistics
            • Get report groups
            • Get report groups
            • Get report group
            • Add report to group
            • Bulk add reports to group
            • Get report group statistics
            • Get analytics data
            • Get platform success rates
            • Get reports by platform
            • Get reports by group
            • Create test reports in bulk
            • Tạo PC report CSV
            • Lấy PC report JSON
            • Lấy report tasks
            • Giám sát report tasks
            • Report tasks đang hoạt động
            • Chi tiết report task
            • Thống kê reports
            • Dữ liệu biểu đồ reports
            • Lấy danh sách report sites
            • Tạo report site
            • Thống kê sites
            • Toggle site enabled/disabled
            • Toggle tất cả sites
            • Toggle sites theo type
            • Cập nhật hàng loạt AI prompts
            • Cập nhật site
            • Xóa site
            • Lấy cấu hình đầy đủ của site
            • Lấy AI prompt của site
            • Cập nhật AI prompt của site
            • Lấy GPT config của site
            • Cập nhật GPT config
            • Lấy response config của site
            • Cập nhật response config
            • Lấy security vendors đang bật
            • Phân tích keywords
            • Top keywords
            • Thống kê analytics
            • PC report dashboard
            • Lấy report domains
            • Thêm report domain
            • Chi tiết report domain
            • Thống kê hàng ngày
            • Cập nhật hàng loạt reports
          • Test Report
            • Test Microsoft report submission
            • Test AdGuard report submission
            • Test Google Ads report submission
            • Test Spamhaus report submission
            • Test ESET report submission
            • Test multi-platform report submission
            • Get available platforms
          • ESET Test
            • Simple test
            • Minimal POST test
            • Generate ESET report content
            • Get sample test data
            • Test form filling
            • Test full form submission
          • Spamhaus Test
            • Simple test
            • Minimal POST test
            • Generate Spamhaus report content
            • Get sample test data
            • Test Spamhaus form filling
            • Test full Spamhaus form submission
          • Report Scheduler
            • Get report scheduler statistics
            • Process ad detection result
            • Submit a scheduled report
            • Start report scheduler
            • Stop report scheduler
            • Get scheduler status
            • Process scheduled reports
            • Test browser session (non-headless)
            • Test report submission (non-headless)
          • Comprehensive Analytics
            • Get comprehensive dashboard analytics
            • Get overview statistics
            • Get brand-wise statistics
            • Get daily statistics
            • Get platform success rates
            • Get account performance
            • Get summary report
          • Search
            • Tạo nhiều search tasks
            • Lấy tổng số search tasks
          • Report Tasks
            • Tạo report tasks cho một domain
            • Tạo report tasks trực tiếp (hỗ trợ profileCountry và domainType rõ ràng)
            • Tạo report tasks cho nhiều domains cùng lúc
            • Tạo email-based report tasks
            • Lấy report task PENDING tiếp theo
            • Cập nhật status của report task
            • Lấy danh sách platforms có sẵn
            • Thống kê report tasks
            • Kích hoạt thực thi thủ công
          • Monitor
            • Thông tin tài nguyên hệ thống
          • System
            • Hello World
            • Health check
            • Đọc file log từ xa
            • Monitor worker status
          • Auth
            • Đăng nhập
            • Đăng ký tài khoản
          • User Management
            • Danh sách users có phân trang
            • Tạo user mới (Admin only)
            • Lấy tất cả users (không phân trang)
            • Lấy thông tin profile của user hiện tại
            • Tạo QR code TOTP cho 2FA
            • Xác thực TOTP để bật 2FA
            • Lấy user theo ID
            • Cập nhật thông tin user
            • Xóa user
            • Đổi mật khẩu user
          • Campaign Management
            • Danh sách campaigns có phân trang
            • Tạo campaign mới
            • Lấy tất cả campaigns
            • Thống kê tổng hợp campaigns
            • Xuất campaigns ra Excel
            • Lấy dữ liệu Negative SEO
            • Xuất dữ liệu Negative SEO ra Excel
            • Xuất Negative SEO theo IDs
            • Lấy dữ liệu Home Traffic
            • Xuất Home Traffic ra Excel
            • Xuất Home Traffic theo IDs
            • Lấy báo cáo campaigns (Public)
            • Lấy tất cả báo cáo campaigns (Public)
            • Lấy chi tiết campaign
            • Cập nhật campaign
            • Xóa campaign
            • Lấy campaigns đã fired theo type
          • Server Management
            • Danh sách servers có phân trang
            • Tạo server mới
            • Lấy tất cả servers
            • Lấy servers từ MongoDB
            • Báo cáo hiệu suất server
            • Thống kê health check
            • Thống kê task monitor
            • Lịch sử lỗi server
            • Thống kê lỗi server
            • Thay đổi trạng thái server
            • Xóa nhiều servers
            • Lấy server theo ID
            • Cập nhật server
            • Xóa server
            • Báo cáo servers (Public)
          • Project Management
            • Lấy tất cả projects
            • Tạo project mới
            • Lấy projects theo campaign
            • Cập nhật dữ liệu traffic
            • Cập nhật nhiều projects
            • Xóa nhiều projects
            • Lấy project theo ID
            • Cập nhật project
            • Cập nhật state của project
            • Cập nhật status của project
            • Tính lại chi tiết project
          • Task Management
            • Worker yêu cầu tasks
            • Reset task creation
          • Proxy Management
            • Danh sách proxies
            • Tạo proxy mới
            • Xóa tất cả proxies
            • Xuất proxies ra Excel
            • Tải template Excel mẫu
            • Tạo nhiều proxies
            • Import proxies từ Excel
            • Import từ tất cả providers
            • Xóa nhiều proxies
            • Lấy proxy theo ID
            • Cập nhật proxy
            • Xóa proxy
            • Toggle trạng thái proxy
          • Backup Proxy
            • Lấy danh sách backup proxies
            • Tạo backup proxy
            • Xóa nhiều backup proxies
            • Import từ Proxifly
            • Chạy health check
            • Lấy backup proxy theo ID
            • Cập nhật backup proxy
            • Xóa backup proxy
            • Test backup proxy
          • Proxy Providers
            • Lấy danh sách proxy providers
            • Tạo proxy provider
            • Xóa nhiều providers
            • Lấy provider theo ID
            • Cập nhật provider
            • Xóa provider
          • Proxy Packages
            • Lấy danh sách proxy packages
            • Tạo proxy package
            • Xóa nhiều packages
            • Lấy package theo ID
            • Cập nhật package
            • Xóa package
          • Profiles
            • Lấy profiles khả dụng (Public)
            • Lấy profile ngẫu nhiên (Public)
            • Danh sách profiles có phân trang
            • Phân tích profiles
            • Health check profiles
            • Tạo profile session
            • Lấy profile session
            • Cập nhật profile session
            • Xóa profile session
            • Đánh dấu profile đã dùng
            • Cập nhật nhiều profiles
            • Tạo nhiều profiles
            • Dọn dẹp sessions hết hạn
            • Dọn dẹp toàn diện
            • Tạo profile mới
            • Lấy profile theo ID
            • Cập nhật profile
            • Xóa profile
            • Thống kê profile
            • Sessions của profile (phân trang)
            • Report tasks của profile
            • Domains đã report bởi profile
            • Tasks theo domain của profile
            • Reset cooldown của profile
            • Test kết nối profile
            • Reset số lần đăng nhập thất bại
            • Ghi nhận đăng nhập thất bại
          • Keywords
            • Danh sách keywords
            • Tạo keyword mới
            • Xuất keywords ra Excel
            • Xác nhận fire keyword
            • Reset tất cả keywords
            • Lấy keyword theo ID
            • Cập nhật keyword
            • Xóa keyword
            • Reset keyword
            • Cập nhật status keyword
            • Kết quả SERP của keyword
          • Dashboard
            • Dữ liệu tổng hợp dashboard
            • Thống kê hệ thống
            • Thống kê traffic theo ID
            • Dữ liệu bảng biểu đồ traffic
            • Xuất báo cáo ra Excel
            • Lấy dữ liệu báo cáo
            • Cập nhật hàng loạt dashboard
            • Tính lại chi tiết projects (Admin only)
          • System Settings
            • Toggle Telegram notifications
            • Trạng thái Telegram notification
          • Activity Logs
            • Lấy activity logs
          • Locations
            • Lấy tất cả locations
            • Tạo location mới
            • Lấy location theo ID
            • Cập nhật location
            • Xóa location
          • Evidence
            • Kiểm tra evidence tồn tại
            • Kiểm tra evidence hàng loạt
            • Lấy evidence mới nhất
          • Files
            • Upload file
            • Download file
          • Server Hourly Reports
            • Lấy server hourly reports
          • Task Logs
            • Lấy task logs
          • Webhooks - Proxy
            • Lấy proxy cho task (Worker webhook)
            • Lấy proxies có thể rotate (Worker webhook)
            • Báo cáo kết quả proxy rotation (Worker webhook)
            • Lấy backup proxy (Worker webhook)
          • Webhooks - Profile
            • Lấy profiles (admin view, webhook)
            • Lấy profile ngẫu nhiên (Worker webhook)
            • Profiles khả dụng (webhook)
            • Lấy profile session (webhook)
            • Tạo profile session (webhook)
            • Đánh dấu profile đã dùng (webhook)
            • Lấy profile tags (webhook)
            • Lấy tất cả profile sessions (webhook)
          • Webhooks - Task
            • Cập nhật kết quả thực thi task (Worker webhook)
          • Webhooks - Search
            • Báo cáo domains đã click (webhook)
            • Lấy clicked domains (webhook)
            • Báo cáo domains (webhook)
            • Third party callback (webhook)
          • Webhooks - Logs
            • Lấy file logs (webhook)
            • Xóa nội dung log (webhook)
            • Kiểm tra IP address (webhook)
          • Webhooks - Keyword
            • Gửi kết quả SERP keyword (webhook)
          • Webhooks - Report
            • Cập nhật trạng thái report task (webhook)
          • Webhooks - CocCoc Token
            • Lấy CocCoc tokens (webhook)
            • Lock CocCoc token (webhook)
            • Unlock CocCoc token (webhook)
    • Schemas
      • LoginRequest
      • UpdateTask
      • WorkerStatus
      • StandardResponse
      • UserResponseDto
      • CreateTaskGroupRequest
      • HealthResponse
      • UserModel
      • CreateProxy
      • ErrorResponse
      • Pagination
      • CreateUserRequest
      • ReportTaskCreation
      • ErrorResponseFull
      • BulkDeleteRequest
      • UpdateUserRequest
      • CreateEmail
      • MessageResponse
      • UserResponse
      • PagingInfo
      • CreateMultipleSearchRequest
      • TrafficType
      • CampaignResponse
      • ProfileModel
      • CreateMultipleReportRequest
      • ServerResponse
      • CreateProfileRequest
      • UpdateReportStatusRequest
      • Task
      • BulkImportProfileReq
      • TestProxyRequest
      • TaskGroup
      • ProxyResponse
      • UpdateProfileRequest
      • CreateTask
      • UpdateTaskBody
      • KeywordResponse
      • CampaignModel
      • CreateSearch
      • TaskPaginatedResponse
      • LocationResponse
      • CreateCampaignRequest
      • ProxyTestResult
      • UpdateCampaignRequest
      • CreateReportTaskBody
      • BulkUpdateCampaignStatusReq
      • CreateEmailReportTaskBody
      • NegativeSeoData
      • BatchReportTaskItem
      • CreateProjectRequest
      • CreateReportTaskResponse
      • UpdateProjectRequest
      • BatchReportTaskResponse
      • ProjectModel
      • BulkUpdateProjectRequest
      • ReportTask
      • WorkerHandshakeRequest
      • UpdateReportTaskStatusBody
      • WorkerHandshakeResponse
      • UpdateReportTaskStatusResponse
      • WorkerHeartbeatPayload
      • PlatformsListResponse
      • WorkerGlobalSettings
      • ReportTaskStatsResponse
      • TaskPullRequest
      • ExecuteReportTaskResponse
      • WorkerFatalLog
      • CreateSearchTask
      • TaskModel
      • Proxy
      • TaskUpdateDto
      • CreateProxyBody
      • ReportTaskModel
      • CreateProxyResponse
      • ReportTaskResult
      • ResetProcessResponse
      • PCReportResponse
      • MonitorResponse
      • ReportSiteItem
      • CreateReportSiteRequest
      • TestProxyResponse
      • GPTConfig
      • TestProfileResponse
      • ResponseConfig
      • ReportDomainsResponse
      • ReportDomainItem
      • AddReportDomainRequest
      • UpdateReportDomainRequest
      • BulkUpdateDashboardRequest
      • ProxyModel
      • CreateProxyRequest
      • UpdateProxyRequest
      • SMTPServerConfig
      • EmailTaskLog
      • GeoLocation
      • CampaignConfigs
      • ProjectAttribute
      • TaskTrafficType
      • ReportTaskItem
      • SummaryStats
      • DailyStats
      • BrandStats
      • ProfileStats
      • FieldConfig
  1. Deployment

Production

Tài Liệu Triển Khai Môi Trường Production — API Backend Traffic Tools V2#

Phiên bản: Go 1.22.4
Cập nhật lần cuối: 26/03/2026
Phạm vi: Quy trình triển khai Production của API Backend, bao gồm kiến trúc Build-Deploy tách biệt, quy trình CI/CD trên AWS và hướng dẫn rollback khẩn cấp.

1. Kiến Trúc Triển Khai Production#

1.1. Nguyên Tắc Thiết Kế#

Môi trường Production áp dụng kiến trúc tách biệt hoàn toàn giữa giai đoạn Build và giai đoạn Deploy:
Giai đoạn Build diễn ra trên Bastion Build Server — máy chủ trung gian chuyên dụng. Máy này có Go toolchain, Docker và quyền truy cập AWS ECR.
Giai đoạn Deploy diễn ra trên Production Server (AWS EC2). Máy này không có Go toolchain, không có mã nguồn, chỉ kéo Docker image từ ECR và chạy.
Lợi ích của kiến trúc này:
Production Server không bao giờ chứa mã nguồn Go hay Go toolchain, giảm thiểu bề mặt tấn công.
Docker image được build một lần và deploy nhiều lần, đảm bảo tính nhất quán.
Có thể rollback về bất kỳ phiên bản nào bằng cách chỉ định tag commit cụ thể.
Build server có thể được tắt khi không cần thiết để tiết kiệm chi phí.

1.2. Luồng Triển Khai Tổng Quan#

Khi developer merge code vào nhánh master:
GitLab CI kích hoạt pipeline với hai stage tuần tự: build-master và deploy-master.
Stage build-master kết nối SSH đến Bastion Build Server, pull code mới nhất, build Docker image đa kiến trúc (arm64 + amd64) và push lên AWS ECR.
Stage deploy-master kết nối SSH đến Production Server, kéo image mới từ ECR và khởi động lại container.

2. Giai Đoạn Build (build-master)#

2.1. Cấu Hình Pipeline#

2.2. Xác Thực SSH Đến Bastion Build Server#

BASTION_BUILD_KEY là SSH private key (base64 encoded) được lưu trong GitLab CI/CD Variables Protected. Key này chỉ có quyền truy cập Bastion Build Server.

2.3. Đăng Nhập AWS ECR#

AWS ECR token có hiệu lực 12 giờ. Lệnh này lấy token mới và đăng nhập Docker vào ECR registry.

2.4. Cập Nhật Mã Nguồn#

2.5. Build Docker Image Đa Kiến Trúc#

Giải thích các tham số quan trọng:
--cache-from $TAG_LATEST — Tận dụng layer cache từ image cũ. Các layer không thay đổi (Go base image, go mod download) được tái sử dụng, giảm thời gian build từ 10 phút xuống còn 2-3 phút.
--platform linux/arm64,linux/amd64 — Build cho cả chip ARM (AWS Graviton) và x86_64. Đảm bảo image chạy được trên mọi loại EC2 instance.
-t $TAG_LATEST — Tag latest để deploy nhanh.
-t $TAG_COMMIT — Tag theo commit hash ($CI_COMMIT_SHORT_SHA) để rollback về phiên bản cụ thể.
--push — Push image lên ECR ngay sau khi build xong.

2.6. Biến CI/CD Cho Build#

BiếnMô Tả
AWS_REGIONap-southeast-1 (Singapore)
AWS_ACCOUNT_ID211125536219
AWS_ECR$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
IMAGE_NAMEtraffic/api-prod
PLATFORMlinux/arm64,linux/amd64
TAG_LATEST$AWS_ECR/$IMAGE_NAME:latest
TAG_COMMIT$AWS_ECR/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA

3. Giai Đoạn Deploy (deploy-master)#

3.1. Cấu Hình Pipeline#

3.2. Lấy IP Production Server Động#

Kỹ thuật lấy IP qua AWS tag thay vì hardcode IP giúp pipeline hoạt động đúng ngay cả khi EC2 instance bị thay thế hoặc IP thay đổi.

3.3. Quy Trình Deploy Trên Production Server#

Giải thích tham số docker-compose up:
--force-recreate — Buộc tạo lại container ngay cả khi cấu hình không thay đổi. Đảm bảo container mới luôn sử dụng image mới nhất.
--remove-orphans — Xóa các container không còn được định nghĩa trong docker-compose.yml.

4. Quản Lý Image Trên AWS ECR#

4.1. Cấu Trúc Tag#

TagVí DụMô Tả
latest...amazonaws.com/traffic/api-prod:latestImage mới nhất, luôn được cập nhật sau mỗi lần build.
Commit hash...amazonaws.com/traffic/api-prod:b5f8r7w2Image của một commit cụ thể, dùng để rollback.

4.2. Xem Danh Sách Image Trên ECR#

4.3. Thông Tin ECR Repository#

Repository name: traffic/api-prod
Account ID: 211125536219
Region: ap-southeast-1
Full URI: 211125536219.dkr.ecr.ap-southeast-1.amazonaws.com/traffic/api-prod

5. Quy Trình Rollback Khẩn Cấp#

5.1. Khi Nào Cần Rollback#

Sau khi deploy, API trả về lỗi 500 liên tục.
Cron job không chạy hoặc chạy sai.
Kết nối database bị lỗi sau khi deploy.
Tính năng mới gây ra hành vi không mong muốn ảnh hưởng đến vận hành.

5.2. Quy Trình Rollback Nhanh#

Bước 1 — Xác định commit hash của phiên bản ổn định:
Bước 2 — Đăng nhập SSH vào Production Server:
Bước 3 — Cập nhật docker-compose.yml để dùng image cũ:
Mở file docker-compose.yml và thay đổi tag image từ latest sang commit hash cụ thể:
Bước 4 — Kéo image cũ và khởi động lại:
Bước 5 — Xác nhận rollback thành công:
Toàn bộ quy trình rollback có thể hoàn thành trong vòng 2 phút nếu image cũ đã có trong local cache của Production Server.

5.3. Rollback Qua GitLab Pipeline#

Cách rollback an toàn hơn là revert commit trên GitLab:
Pipeline sẽ tự động build và deploy phiên bản đã revert. Cách này mất nhiều thời gian hơn (5-10 phút) nhưng đảm bảo lịch sử Git sạch và có thể audit.

6. Giám Sát Sau Khi Deploy#

6.1. Kiểm Tra Ngay Sau Deploy#

Gọi GET / và nhận response {"message": "Hello, World!"}.
Đăng nhập với tài khoản test và nhận JWT token.
Gọi GET /api/user/profile với token và nhận thông tin user.
Kiểm tra log container không có lỗi panic.
Kiểm tra cron job đã khởi động: All cronjobs initialized successfully.

6.2. Theo Dõi Log Container#

6.3. Kiểm Tra Tài Nguyên Server#

6.4. Kiểm Tra Log Trong Container#


7. Bảo Mật Trong Môi Trường Production#

7.1. Quản Lý Secret#

Tất cả thông tin nhạy cảm được lưu trong GitLab CI/CD Variables với chế độ Protected và Masked:
SSH private keys được lưu dạng base64 encoded.
AWS credentials được lưu dưới dạng biến riêng biệt.
Không có secret nào được hardcode trong .gitlab-ci.yml hay Dockerfile.

7.2. Nguyên Tắc Least Privilege#

SSH key của Bastion Build Server chỉ có quyền truy cập Bastion, không có quyền truy cập Production Server.
SSH key của Production Deploy chỉ có quyền truy cập Production Server.
AWS IAM role của Bastion Build Server chỉ có quyền push lên ECR repository traffic/api-prod.
AWS IAM role của Production Server chỉ có quyền pull từ ECR repository traffic/api-prod.

7.3. Không Lưu Mã Nguồn Trên Production Server#

Production Server chỉ chứa file docker-compose.yml và Docker image từ ECR. Không có Go toolchain, không có mã nguồn. Ngay cả khi Production Server bị xâm phạm, kẻ tấn công cũng không thể đọc mã nguồn hay sửa đổi logic ứng dụng.

8. Biến CI/CD Cần Thiết#

8.1. Danh Sách Biến Bắt Buộc#

BiếnPhạm ViMô Tả
BASTION_BUILD_KEYProtectedSSH private key (base64) để kết nối Bastion Build Server.
BASTION_BUILD_USERProtectedUsername SSH của Bastion Build Server.
BASTION_BUILD_IPProtectedĐịa chỉ IP của Bastion Build Server.
BASTION_BUILD_PORTProtectedPort SSH của Bastion Build Server.
AWS_TRAFFIC_PROD_KEYProtectedSSH private key (base64) để kết nối Production Server.
AWS_TRAFFIC_PROD_USERProtectedUsername SSH của Production Server.
AWS_TRAFFIC_PROD_PORTProtectedPort SSH của Production Server.
AWS_ACCESS_KEY_IDProtected, MaskedAWS Access Key ID để đăng nhập ECR.
AWS_SECRET_ACCESS_KEYProtected, MaskedAWS Secret Access Key để đăng nhập ECR.
AWS_DEFAULT_REGIONProtectedAWS Region: ap-southeast-1.
TRAFFIC_DEV_77_37_47_88_KEYProtectedSSH key cho server Develop/Staging.
TRAFFIC_DEV_77_37_47_88_USERProtectedUsername SSH server Develop/Staging.
TRAFFIC_DEV_77_37_47_88_IPProtectedIP server Develop/Staging.
TRAFFIC_DEV_77_37_47_88_PORTProtectedPort SSH server Develop/Staging.
AWS_TRAFFIC_REL_KEYProtectedSSH key cho server Release.
AWS_TRAFFIC_REL_USERProtectedUsername SSH server Release.
AWS_TRAFFIC_REL_PORTProtectedPort SSH server Release.

9. Xử Lý Sự Cố Triển Khai Production#

9.1. Pipeline Build Thất Bại#

Lỗi: Permission denied (publickey) khi kết nối Bastion:
Kiểm tra biến BASTION_BUILD_KEY có được cấu hình đúng không.
Kiểm tra public key tương ứng có trong ~/.ssh/authorized_keys trên Bastion không.
Lỗi: no space left on device khi build Docker:
Lỗi: go mod download thất bại:

9.2. Pipeline Deploy Thất Bại#

Lỗi: Không lấy được IP Production Server:
Kiểm tra AWS credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) có đúng không.
Kiểm tra EC2 instance có đang chạy và có tag đúng không.
Kiểm tra IAM role có quyền ec2:DescribeInstances không.
Lỗi: docker pull thất bại:
Kiểm tra Production Server có quyền pull từ ECR không.
Kiểm tra image tag có tồn tại trên ECR không.

9.3. API Không Phản Hồi Sau Deploy#

9.4. Cron Job Không Chạy Sau Deploy#

Kiểm tra log khởi động:
Phải có dòng: All cronjobs initialized successfully.
Nếu không có, kiểm tra biến môi trường cron trong .env của container:

10. Thông Tin AWS Services#

ServiceMục ĐíchRegion
AWS ECRLưu trữ Docker imageap-southeast-1 (Singapore)
AWS EC2Chạy Production Serverap-southeast-1 (Singapore)
AWS IAMQuản lý quyền truy cậpGlobal
Tag EC2 instance Production được sử dụng để tự động lấy IP:
Name: traffic-social-search-api-tool-prod
Service_Traffic: traffic-api
Environment: production
Modified at 2026-03-26 10:44:41
Previous
Staging
Next
Đăng nhập hệ thống (Login)
Built with