配置虚拟机以安装 RPM 软件包

本页面介绍了如何配置虚拟机以从 Artifact Registry Yum 代码库安装 RPM 软件包。Artifact Registry Yum 代码库支持 yumdnf 客户端。

准备工作

如果目标 Yum 代码库不存在,请创建新代码库。您可以创建私有代码库或无需身份验证的公共代码库。

准备虚拟机以访问 Yum 代码库

如需准备虚拟机以访问 Yum 代码库,请使用 yumdnf 客户端工具:

私有代码库

  1. 选择您将用于授予访问权限的服务账号。

    • 对于 Compute Engine 虚拟机,这可能是默认服务账号。对于使用服务账号的任何虚拟机,Artifact Registry 都会自动检索 Compute Engine 虚拟机服务账号凭据。
    • 对于其他虚拟机,请创建或选择一个服务账号来代表您的虚拟机执行操作。您需要服务账号密钥文件所在的位置,才能配置虚拟机。您可以在服务账号页面上查看和创建现有账号的密钥。
  2. 向您的服务账号授予代码库访问权限

  3. 准备虚拟机以访问代码库。

    Compute Engine 虚拟机

    1. 为虚拟机分配 cloud-platform API 访问权限范围。如需了解如何设置访问权限范围,请参阅更改实例的服务账号和访问权限范围

    2. 使用以下命令更新软件包缓存:

      sudo yum makecache
      
    3. 使用以下命令在虚拟机上安装 Artifact Registry 插件:

      sudo yum install FORMAT-plugin-artifact-registry
      

      替换以下内容:

      • FORMAT 替换为以下项之一:
        • yum,如果您的操作系统是 Enterprise Linux 7 或更早版本
        • dnf(如果您的操作系统是 Enterprise Linux 8 或更高版本)

    其他虚拟主机

    1. 使用以下命令安装 RPM 签名密钥:

      sudo rpm --import https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
      
    2. 创建一个名为 /etc/yum.repos.d/artifact-registry-plugin.repo 的文件,其中包含以下内容:

      [ar-plugin]
      name=Artifact Registry Plugin
      baseurl=https://packages.cloud.google.com/yum/repos/FORMAT-plugin-artifact-registry-DISTRO-stable
      enabled=1
      gpgcheck=1
      

      替换以下内容:

      • FORMAT 替换为以下项之一:
        • yum,如果您的操作系统是 Enterprise Linux 7 或更早版本
        • dnf(如果您的操作系统是 Enterprise Linux 8 或更高版本)
      • DISTRO,其中 elX 是 Red Hat Enterprise Linux 主要版本的格式,X 是版本号。有效的 DISTRO 值示例包括 el7el8
    3. 使用以下命令更新 Yum:

      sudo yum makecache
      
    4. 使用以下命令在虚拟机上安装 Artifact Registry 插件:

      yum install FORMAT-plugin-artifact-registry
      

      FORMAT 替换为以下项之一:

      • yum,如果您的操作系统是 Enterprise Linux 7 或更早版本
      • dnf(如果您的操作系统是 Enterprise Linux 8 或更高版本)
    5. artifact-registry.conf 文件中找到 #service_account_json = "/path/to/creds.json" 行,然后取消该行的注释并添加服务账号密钥的路径。

      • 对于 Enterprise Linux 7 或更早版本的操作系统,配置文件的路径为 /etc/yum/pluginconf.d/artifact-registry.conf
      • 对于 Enterprise Linux 8 或更高版本的操作系统,配置文件的路径为 /etc/dnf/plugins/artifact-registry.conf

      配置文件条目:

      service_account_json = "PATH_TO_SERVICE_ACCOUNT_KEY"
      

      PATH_TO_SERVICE_ACCOUNT_KEY 替换为服务账号密钥 JSON 文件的路径。

公开代码库

配置对代码库的公共访问权限

配置虚拟机以访问标准 Yum 代码库

如需将虚拟机配置为访问 Yum 代码库,请使用 yumdnf 客户端工具:

  1. 运行 gcloud beta artifacts print-settings yum 命令,生成虚拟机配置命令:

    gcloud beta artifacts print-settings yum \
        --repository=REPOSITORY \
        --location=LOCATION
    

    替换以下内容:

    • REPOSITORY 是 Artifact Registry 代码库的名称。
    • LOCATION 是代码库的区域位置

    输出类似于以下内容:

    # To configure your package manager with this repository:
    
    # Update Yum:
    sudo yum makecache
    
    # Install the Yum credential helper:
    sudo yum install yum-plugin-artifact-registry
    
    # Configure your VM to access Artifact Registry packages using the following
    # command:
    
    sudo tee -a /etc/yum.repos.d/artifact-registry.repo << EOL
    [REPOSITORY]
    name=REPOSITORY
    baseurl=https://LOCATION-yum.s3nsregistry.fr/projects/PROJECT/REPOSITORY
    enabled=1
    repo_gpgcheck=0
    gpgcheck=0
    EOL
    
    # Update Yum:
    sudo yum makecache
    
  2. 使用 print-settings 命令输出中的 tee 命令,将 yum 客户端工具配置为从您的代码库中提取软件包。

    例如,如需安装项目 my-project 中位置 u-france-east1 处的公共 my-repo 中的软件包,请使用以下命令:

    sudo tee -a /etc/yum.repos.d/artifact-registry.repo << EOL
    [my-repo]
    name=My Repository
    baseurl=https://u-france-east1-yum.s3nsregistry.fr/projects/my-project/my-repo
    enabled=1
    repo_gpgcheck=0
    gpgcheck=0
    EOL
    
  3. 使用以下命令更新代码库来源:

    sudo yum makecache
    

Yum 现在可以连接到代码库了。

如果您创建了其他 Yum 代码库,请在 /etc/yum.repos.d/ 中为每个代码库创建一个 .repo 文件。重新运行 sudo yum update 以更新代码库来源。

后续步骤