gcloud 命令安装 Google 云组件错误

Install Google Cloud components error from gcloud command

我正在尝试从 gcloud command-line tool 安装多个 GCP 组件,但总是出现相同的错误:

$ gcloud components list

Your current Cloud SDK version is: 146.0.0
The latest available version is: 146.0.0

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                  Components                                                 │
├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤
│     Status    │                         Name                         │            ID            │    Size   │
├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤
│ Not Installed │ App Engine Go Extensions                             │ app-engine-go            │  47.9 MiB │
│ Not Installed │ Bigtable Command Line Tool                           │ cbt                      │   3.8 MiB │
│ Not Installed │ Cloud Datalab Command Line Tool                      │ datalab                  │   < 1 MiB │
│ Not Installed │ Cloud Datastore Emulator                             │ cloud-datastore-emulator │  15.4 MiB │
│ Not Installed │ Cloud Datastore Emulator (Legacy)                    │ gcd-emulator             │  38.1 MiB │
│ Not Installed │ Cloud Pub/Sub Emulator                               │ pubsub-emulator          │  21.0 MiB │
│ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr    │   3.3 MiB │
│ Not Installed │ gcloud app Java Extensions                           │ app-engine-java          │ 128.3 MiB │
│ Not Installed │ gcloud app Python Extensions                         │ app-engine-python        │   7.2 MiB │
│ Not Installed │ kubectl                                              │ kubectl                  │  11.5 MiB │
│ Installed     │ BigQuery Command Line Tool                           │ bq                       │   < 1 MiB │
│ Installed     │ Cloud SDK Core Libraries                             │ core                     │   5.7 MiB │
│ Installed     │ Cloud Storage Command Line Tool                      │ gsutil                   │   2.8 MiB │
│ Installed     │ Default set of gcloud commands                       │ gcloud                   │           │
│ Installed     │ gcloud Alpha Commands                                │ alpha                    │   < 1 MiB │
│ Installed     │ gcloud Beta Commands                                 │ beta                     │   < 1 MiB │
└───────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘

然后尝试安装 kubectl

$ gcloud components install kubectl

You cannot perform this action because this Cloud SDK installation is 
managed by an external package manager.  If you would like to get the 
latest version, please see our main download page at:
https://cloud.google.com/sdk/
ERROR: (gcloud.components.install) The component manager is disabled for this installation

知道为什么会出现这个错误吗?

Cloud SDK 组件管理器仅在您不通过其他包管理器安装 SDK 时才有效。如果您想使用组件管理器,您可以使用以下方法之一进行安装:

https://cloud.google.com/sdk/downloads#versioned

https://cloud.google.com/sdk/downloads#interactive

我们的 deb 和 yum 存储库中提供了其他软件包,因此所有相同的组件都可用,您只需使用现有的软件包管理器来安装它们:

https://cloud.google.com/sdk/downloads#apt-get

https://cloud.google.com/sdk/downloads#yum

要为 Ubuntu OS 的此答案添加更多上下文,这些是我在使用 apt-get 安装 google-cloud-sdk 时采取的步骤包管理器

  1. 删除现有安装使用

    sudo apt-get remove google-cloud-sdk

  2. 导航到 https://cloud.google.com/sdk/docs/quickstart-linux 并按照步骤为您的系统下载正确的 tar.gz 软件包

  3. 导航到下载目录并使用

    解压缩存档

    tar -zxf google-cloud-sdk-*

  4. 使用

    安装SDK

    ./google-cloud-sdk/install.sh

  5. 使 gcloud 命令可用:

    • 运行 source ~/.bashrc
    • 关闭当前终端会话并打开一个新终端会话

使用此安装,我可以使用以下命令更新和安装 kubectl

gcloud components update
gcloud components install kubectl

如果 aptapt-get 无法卸载 gcloud,请尝试 snap remove google-cloud-sdk

如果您使用较新的 Ubuntu 版本(例如 LTS 18.04),可能会出现这种情况。

我的问题

我想在现有的 GCE 实例 (Ubuntu Xenial) 上升级 gcloud CLI 版本,运行ning gcloud components update 结果如下:

# gcloud components update
ERROR: (gcloud.components.update) You cannot perform this action because this Cloud SDK installation is managed by an external package manager.
Please consider using a separate installation of the Cloud SDK created through the default mechanism described at: https://cloud.google.com/sdk/

我的解决方案

我发现 SDK 可执行文件安装在 /usr/lib/google-cloud-sdk 中,并通过 /usr/bin:

中的符号链接包含在 PATH 中
# ll /usr/bin | grep google-cloud
lrwxrwxrwx  1 root   root          30 Aug 20  2018 bq -> ../lib/google-cloud-sdk/bin/bq*
lrwxrwxrwx  1 root   root          34 Aug 20  2018 gcloud -> ../lib/google-cloud-sdk/bin/gcloud*
lrwxrwxrwx  1 root   root          52 Aug 20  2018 git-credential-gcloud.sh -> ../lib/google-cloud-sdk/bin/git-credential-gcloud.sh*
lrwxrwxrwx  1 root   root          34 Aug 20  2018 gsutil -> ../lib/google-cloud-sdk/bin/gsutil*

因此,要安装最新版本,我按照 https://cloud.google.com/sdk/docs/quickstart-linux 中的步骤 3 和 4 进行操作:

  1. Download the archive file best suited to your operating system.
# curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-247.0.0-linux-x86_64.tar.gz
  1. Extract the archive to any location on your file system; preferably, your Home folder.
# tar zxvf google-cloud-sdk-247.0.0-linux-x86_64.tar.gz google-cloud-sdk

然后用新目录替换旧的 /usr/lib/google-cloud-sdk 目录:

# mv /usr/lib/google-cloud-sdk /usr/lib/google-cloud-sdk-ORIG
# mv google-cloud-sdk /usr/lib/

这允许 PATH 上已有的符号链接继续工作,保持现有 auth/config 不变,并且似乎支持将来通过 gcloud components update 升级:

# gcloud components update

All components are up to date.

有用的故障排除命令

升级前后运行这些可能会有帮助gcloud

gcloud 在您的 PATH 中的什么位置:

$ which gcloud
/usr/bin/gcloud

您使用的 gcloud 是什么版本:

$ gcloud version
Google Cloud SDK 247.0.0
bq 2.0.43
core 2019.05.17
gsutil 4.38

gcloud 使用什么凭据(默认):

$ gcloud auth list

gcloud 使用什么配置(默认):

$ gcloud config list
ERROR: (gcloud.components.update) You cannot perform this action because this Cloud SDK installation is managed by an external package manager.
Please consider using a separate installation of the Cloud SDK created through the default mechanism described at: https://cloud.google.com/sdk/

我无法安装所需的 kubectl gcloud 组件 这背后的原因是我使用了 snap 管理器来安装 GCloud SDK,所以我还需要通过 snap 安装 kubectl。

sudo snap install kubectl --classic

它适用于我的情况。

来自 google 支持的回答。

This can occur when Google Cloud SDK is installed via package manager like apt-get or yum.
If you would like to enable Google Cloud CLI compoenent manager, you can reinstall Cloud SDK using installer[1] after uninstall existing package-manager-installed Cloud SDK by following this document[2].
However installing 'docker-credential-gcr' is not supported by Cloud SDK so please consider to use gcloud credential helper[3] or install it via curl command[4].

[1] https://cloud.google.com/sdk/docs/install#installation_instructions

[2] https://cloud.google.com/sdk/docs/uninstall-cloud-sdk