Harbor

repository·main·Indexed 10 days ago

https://github.com/goharbor/harbor

An open-source, cloud-native registry that stores and manages Docker and Helm images. It extends Docker Distribution with enterprise-grade features including Role Based Access Control (RBAC), policy-based replication, vulnerability scanning, identity management via LDAP/AD and OIDC, and image lifecycle management.

Tokens
27.5K
Snippets
89
Records
119
Agent score
98%

What's inside Harbor

  1. Use cases for the Job Service

    main

    The Job Service supports several job types and management actions:

    Job Types

    • Generic: Executed immediately if resources are available; executes only once.
    • Scheduled: Executed after a specified delay.
    • Periodic: Repeatedly executed based on a specified interval (cron-like).
    • Unique: Jobs submitted with a unique flag to prevent duplicate jobs with the same name/arguments from existing in the queue simultaneously.

    Management Actions

    • Stop: Stop a running job.
    • Cancel: Cancel a scheduled or running job.
    • Retry: Retry a failed job (must meet retry criteria).
    • Stats/Logs: Retrieve job statistics or execution logs (log availability depends on the logger implementation).
    • Health Check: Check the health status of the job service (no authentication required).
  2. Harbor Core Features Overview

    main

    Harbor is a trusted cloud-native registry that extends Docker Distribution with enterprise-grade security and management features:

    • Cloud Native Registry: Supports both container images and Helm charts.
    • Role Based Access Control (RBAC): Manage permissions via 'projects' for images and charts.
    • Policy Based Replication: Synchronize images/charts between registries using filters (repository, tag, label) with automatic retries.
    • Vulnerability Scanning: Regular scanning and policy checks to prevent deployment of vulnerable images.
    • Identity Management: Supports LDAP/AD integration and OpenID Connect (OIDC) for Single Sign-On (SSO).
    • Image Lifecycle Management: Includes image deletion and garbage collection to free up space.
    • Content Trust: Supports Notary for signing images to guarantee authenticity.
    • Auditing: Tracks all repository operations through logs.
    • API & UI: Provides a RESTful API with Swagger UI and a graphical user portal.
  3. Track the Harbor project roadmap

    main
    The most up-to-date description of items in the Harbor release pipeline is maintained on the Harbor Project board. The board uses separate swim lanes for each release to track progress. Users and contributors should use this board as a reference to understand the project's direction and to ensure new contributions do not conflict with long-term plans.
  4. How to implement a Cancellable or Stoppable Job

    main

    By default, jobs run until completion or failure. To support stop and cancel actions, you must manually check for operation commands at specific execution points within your Run method logic.

    For Cancellable Jobs

    Check for the cancel signal and return errs.JobCancelledError() to exit gracefully:

    if cmd, ok := ctx.OPCommand(); ok {
        if cmd == opm.CtlCommandCancel {
            return errs.JobCancelledError()
        }
    }

    For Stoppable Jobs

    Check for the stop signal and return errs.JobStoppedError() to exit gracefully:

    if cmd, ok := ctx.OPCommand(); ok {
        if cmd == opm.CtlCommandStop {
            return errs.JobStoppedError()
        }
    }
    if cmd, ok := ctx.OPCommand(); ok {
        if cmd == opm.CtlCommandCancel {
            return errs.JobCancelledError()
        }
        if cmd == opm.CtlCommandStop {
            return errs.JobStoppedError()
        }
    }
  5. Important considerations for Harbor backups and restores

    main

    When using the contrib/backup-restore scripts, keep the following in mind:

    • Warning: These scripts are provided as-is in the contrib/backup-restore directory. They are not officially maintained or supported by the Harbor project. Use them at your own risk.
    • Backup Consistency: To ensure a consistent backup, stop the Harbor instance or ensure minimal write activity during the process.
    • Database Image Tag: In production, use a specific tag for the --db-image option in both backup and restore scripts to ensure consistency.
    • Custom Deployments: If your Harbor deployment uses non-default data paths, you must use the appropriate command-line options (e.g., --db-path, --registry-path) to point to the correct locations.
    • Testing: Always test the backup and restore process in a non-production environment before relying on it for critical data.
  6. How job execution and sub-jobs work

    main

    The Job Service tracks relationships between jobs using parent-child hierarchies. When a job launches a new job via the ctx.LaunchJob method, the new job is treated as a sub-job (execution) of the caller.

    Key tracking properties:

    • Parent Jobs: If a job has sub-jobs, its stats include an executions list containing the IDs of all sub-jobs and a multiple_executions flag set to true.
    • Sub-jobs (Executions): Each sub-job contains an upstream_job_id property that points back to the ID of its parent job.

    Job Kinds:

    • Generic: Standard jobs.
    • Scheduled: Jobs that run after a specific schedule_delay (in seconds).
    • Periodic: A job template that is not run directly. Instead, the service clones the template to create real running jobs. Each execution of a periodic job has a unique ID but links to the template via upstream_job_id.
    func (j *Job) Run(ctx job.Context, params job.Parameters) error{
        // Launching a sub-job (execution) of the current job
        subJob, err := ctx.LaunchJob(models.JobRequest{})
        // ...
        return nil
    }
  7. Use the harbor-backup script to back up Harbor

    main

    The harbor-backup script provides a way to back up Harbor components including the PostgreSQL database, Container Registry data, Chart Museum data, Redis data, Secret keys, and the harbor.yml configuration.

    Prerequisites:

    • Docker: The docker CLI must be installed and accessible.
    • Permissions: You need sudo or docker group permissions.
    • Stopped Harbor: You must stop your Harbor instance completely before running the backup to avoid data inconsistencies.

    Workflow:

    1. Make the script executable: chmod +x harbor-backup.
    2. Stop the Harbor instance.
    3. Run the script with desired options: ./harbor-backup [OPTIONS].

    By default, the backup is created in a directory named harbor_backup in the current working directory. If --no-archive is not used, a compressed harbor_backup.tar.gz will be created inside that directory.

    # Example: Backup with custom paths and no tarball archive
    ./harbor-backup \
      --db-path /custom/db/path \
      --registry-path /custom/registry/path \
      --backup-dir /mnt/backups/harbor_daily \
      --no-archive
  8. Install Cosign (v2.0+) for signature verification

    main

    To verify Harbor release artifacts, you must install Cosign (version 2.0 or later). Use the following commands based on your operating system:

    macOS

    Use Homebrew to install from the Sigstore tap.

    Linux

    Download the amd64 binary, make it executable, and move it to your path.

    Windows (PowerShell)

    Download the amd64 executable using Invoke-WebRequest.

    After installation, verify it by running cosign version.

    # macOS
    brew install sigstore/tap/cosign
    
    # Linux
    curl -LO https://github.com/sigstore/cosign/releases/latest/download/cosign-linux-amd64
    chmod +x cosign-linux-amd64
    sudo mv cosign-linux-amd64 /usr/local/bin/cosign
    
    # Windows (PowerShell)
    Invoke-WebRequest -Uri "https://github.com/sigstore/cosign/releases/latest/download/cosign-windows-amd64.exe" -OutFile "cosign.exe"
    
    # Verify installation
    cosign version
  9. Run the Chart Migrating Tool via Docker

    main

    To execute the migration, run the compiled Docker image using the following command structure. You must provide the location of your existing chart data, the Harbor CA certificate (if using HTTPS), the Harbor hostname, and the admin password.

    Required Parameters:

    • {{your_chart_data_location}}: The local path to your chart storage (typically the chart_storage directory inside Harbor's data_volume).
    • {{harbor_ca_cert_location}}: The path to the CA certificate required to connect to Harbor via HTTPS.
    • {{harbor_hostname}}: The hostname of your Harbor instance.
    • {{harbor_admin_password}}: The password for the Harbor admin user.
    docker run -it --rm \
      -v {{your_chart_data_location}}:/chart_storage \
      -v {{harbor_ca_cert_location}}:/usr/local/share/ca-certificates/harbor_ca.crt \
      goharbor/migrate-chart:0.1.0 \
      --hostname {{harbor_hostname}} \
      --password {{harbor_admin_password}}
  10. Configure Nginx proxy for Harbor UI backend

    main

    When deploying the Harbor UI via Nginx, you must modify the nginx.conf file to ensure that requests for the API, client, and chart repository are proxied to a valid backend server address.

    Update the following location block in your nginx.conf:

    location ~ ^/(api|c|chartrepo)/ {
       proxy_pass ${an available back-end server addr};
    }
  11. Deploy Harbor using Docker Machine

    main

    You can deploy Harbor to cloud providers (like DigitalOcean, AWS, or Azure) or on-premises environments using Docker Machine. This process involves creating a virtual machine, configuring DNS, setting up the Harbor configuration file, and using docker-compose to build and run the containers on the remote machine.

    Workflow Steps:

    1. Create the Virtual Machine: Use docker-machine create with your preferred driver and access token.
    2. Configure DNS: Retrieve the machine's IP address using docker-machine ip and create a DNS entry at your provider pointing to that IP.
    3. Prepare Configuration:
      • Copy Deploy/harbor.yml.tmp to Deploy/harbor.yml.
      • Update the hostname field in Deploy/harbor.yml to match your domain (e.g., harbor.mydomain.com).
      • Run the prepare script (as described in the Harbor Installation Guide).
    4. Activate Environment: Use eval $(docker-machine env <machine_name>) to point your local Docker CLI to the remote machine.
    5. Transfer Configuration Files: Create the directory structure on the remote machine and use docker-machine scp to copy the local config directory to the remote machine.
    6. Build and Run: Execute docker-compose build followed by docker-compose up -d from within the Deploy directory.
    # 1. Create the machine
    docker-machine create --driver digitalocean --digitalocean-access-token <youraccesstoken> harbor.mydomain.com
    
    # 2. Get IP for DNS configuration
    docker-machine ip harbor.mydomain.com
    
    # 3. Activate the machine environment
    eval $(docker-machine env harbor.mydomain.com)
    
    # 4. Transfer config files (example using remote path structure)
    docker-machine ssh harbor.mydomain.com 'mkdir -p /home/<yourusername>/src/harbor/Deploy/config'
    docker-machine scp -r ./config harbor.mydomain.com:$PWD
    
    # 5. Build and start
    docker-compose build
    docker-compose up -d