Google Cloud SDK をAmazon Linux 2環境にインストール(yum+サービスアカウントキーの利用)

2019.09.10

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

個人的Google Cloud SDK導入備忘録 第3弾。当エントリではAmazon Linux 2環境にGoogle Cloud SDKを導入する手順について見ていきたいと思います。Amazon Linux 2環境にyumで導入後、認証部分もファイルを利用する形で対応する方法を当エントリでは試します。

サービスアカウントキー(JSON)の作成

当エントリの手順では、まずはじめに『サービスアカウントキー』というものを作成します。

サービスアカウント及びサービスアカウントキーの詳細については以下をご参照ください。他のプラットフォームやオンプレミスなど GCP の外部でサービス アカウントを使用するための情報として『サービスアカウントキー』を作成し、JSONファイルとして作成します。

GCP管理コンソールにアクセスし、対象プロジェクトの[IAMと管理]から[サービスアカウント]を選択。

プロジェクト配下のサービスアカウント一覧が表示されます。一覧上部の[サービスアカウントを作成]を選択。

必要な情報を入力し、[作成]を押下。

サービスアカウントに付与する権限を設定します。[役割を選択]を押下し、

任意の役割を選択します。

ここは一旦そのまま[完了]押下で進めました。

サービスアカウントが作成されました。

対象となるサービスアカウントの[操作]から[鍵を作成]を選択。

[秘密鍵の作成]でキーのタイプをデフォルトの[JSON]のまま、[作成]を押下。

秘密鍵が作成・保存されました。このファイルは後述の手順で用います。

Google Cloud SDKインストール手順 in Amazon Linux 2(yum+検証コード利用):実践

そして次のステップ:導入環境へと進みます。今回はAmazon Linux 2を用意しました。

$ ssh -i gcp-xxxxxxxxxxxxx-key.pem ec2-user@xx.xxx.xx.xxx
$ sudo yum -y update
$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
$

Google Cloud SDKにはPython2.7.x環境が必要となります。Amazon Linux2の場合であればインスタンス起動時に既に利用可能な状態にあるのでそのまま次に進みます。

$ python --version
Python 2.7.16

導入に必要なリポジトリの追加設定を行います。以下コマンドで*.repoファイルを新規作成。

$ sudo tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM
[google-cloud-sdk]
name=Google Cloud SDK
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
       https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM

yumコマンドで対象ライブラリをインストールします...ですが、公式ドキュメントに則った形でyumコマンドを実行すると、以下のようにエラーとなってしまいました。

sudo yum -y install google-cloud-sdk
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
google-cloud-sdk/signature                                                                                           |  454 B  00:00:00     
Retrieving key from https://packages.cloud.google.com/yum/doc/yum-key.gpg
Importing GPG key 0xA7317B0F:
 Userid     : "Google Cloud Packages Automatic Signing Key <gc-team@google.com>"
 Fingerprint: d0bc 747f d8ca f711 7500 d6fa 3746 c208 a731 7b0f
 From       : https://packages.cloud.google.com/yum/doc/yum-key.gpg
Retrieving key from https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
google-cloud-sdk/signature                                                                                           | 1.4 kB  00:00:00 !!! 
https://packages.cloud.google.com/yum/repos/cloud-sdk-el7-x86_64/repodata/repomd.xml: [Errno -1] repomd.xml signature could not be verified for google-cloud-sdk
Trying other mirror.


 One of the configured repositories failed (Google Cloud SDK),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=google-cloud-sdk ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable google-cloud-sdk
        or
            subscription-manager repos --disable=google-cloud-sdk

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=google-cloud-sdk.skip_if_unavailable=true

failure: repodata/repomd.xml from google-cloud-sdk: [Errno 256] No more mirrors to try.
https://packages.cloud.google.com/yum/repos/cloud-sdk-el7-x86_64/repodata/repomd.xml: [Errno -1] repomd.xml signature could not be verified for google-cloud-sdk
$

エラーメッセージから解決策を探ったところ、以下の情報を発見。リポジトリ設定の幾つかが影響していたようです。

ページの内容に従い、以下の2行をコメントアウト。

$ sudo vi /etc/yum.repos.d/google-cloud-sdk.repo
[google-cloud-sdk]
name=Google Cloud SDK
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el7-x86_64
enabled=1
##gpgcheck=1
##repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
       https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg

保存後改めてyumアップデートを実行。今度は上手く行きました!

$ sudo yum -y install google-cloud-sdk
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                                           | 2.4 kB  00:00:00     
amzn2extra-docker                                                                                                    | 1.3 kB  00:00:00     
google-cloud-sdk                                                                                                     | 1.4 kB  00:00:00     
google-cloud-sdk/primary                                                                                             | 121 kB  00:00:00     
google-cloud-sdk                                                                                                                    864/864
Resolving Dependencies
--> Running transaction check
---> Package google-cloud-sdk.noarch 0:260.0.0-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
 Package                             Arch                      Version                            Repository                           Size
============================================================================================================================================
Installing:
 google-cloud-sdk                    noarch                    260.0.0-1.el7                      google-cloud-sdk                     34 M

Transaction Summary
============================================================================================================================================
Install  1 Package

Total download size: 34 M
Installed size: 166 M
Downloading packages:
warning: /var/cache/yum/x86_64/2/google-cloud-sdk/packages/6812XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXcb47-google-cloud-sdk-260.0.0-1.el7.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 3e1ba8d5: NOKEY
Public key for 6812XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXcb47-google-cloud-sdk-260.0.0-1.el7.noarch.rpm is not installed
6812XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXcb47-google-cloud-sdk-260.0.0-1.el7.noarch.rpm           |  34 MB  00:00:00     
Retrieving key from https://packages.cloud.google.com/yum/doc/yum-key.gpg
Importing GPG key 0xA7317B0F:
 Userid     : "Google Cloud Packages Automatic Signing Key <gc-team@google.com>"
 Fingerprint: d0bc 747f d8ca f711 7500 d6fa 3746 c208 a731 7b0f
 From       : https://packages.cloud.google.com/yum/doc/yum-key.gpg
Retrieving key from https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
Importing GPG key 0x3E1BA8D5:
 Userid     : "Google Cloud Packages RPM Signing Key <gc-team@google.com>"
 Fingerprint: 3749 e1ba 95a8 6ce0 5454 6ed2 f09c 394c 3e1b a8d5
 From       : https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : google-cloud-sdk-260.0.0-1.el7.noarch                                                                                    1/1 
  Verifying  : google-cloud-sdk-260.0.0-1.el7.noarch                                                                                    1/1 

Installed:
  google-cloud-sdk.noarch 0:260.0.0-1.el7                                                                                                   

Complete!
$

上記までの手順でコマンド自体は導入完了です。ですがこの段階ではまだアカウントログイン認証(の設定)が済んでいないため、コマンドを実行すると以下の様にエラーとなってしまいます。

$ exit
$ ssh -i gcp-xxxxxxxxxxxxxxxxxx.pem ec2-user@xxx.xx.xxx.xx
$ gcloud projects list 
ERROR: (gcloud.projects.list) You do not currently have an active account selected.
Please run:

  $ gcloud auth login

to obtain new credentials, or if you have already logged in with a
different account:

  $ gcloud config set account ACCOUNT

to select an already authenticated account to use.

ここで、前述の手順で作成したサービスアカウントキーの出番です。ファイルを配備した上でgcloud auth activate-service-accountコマンドを実行。認証操作がブラウザを介すること無く行うことが出来ました。

$ vi gcp-service-account-key.json
$ gcloud auth activate-service-account \
    gcp-sa-for-al2-20190903@xxxxxxxxxxxxxx.iam.gserviceaccount.com \
    --key-file /home/ec2-user/gcp-service-account-key.json \
    --project cm-xxxxxxxxxxxxxx
Activated service account credentials for: [gcp-sa-for-al2-20190903@cm-xxxxxxxxxx.iam.gserviceaccount.com]

認証、設定周りのコマンドで内容を確認してみます。ちゃんと出来てますね!

$ gcloud auth list
                         Credentialed Accounts
ACTIVE  ACCOUNT
*       gcp-sa-for-al2-20190903@cm-xxxxxxxxxxx.iam.gserviceaccount.com

To set the active account, run:
    $ gcloud config set account `ACCOUNT`
 
$ gcloud config list
[core]
account = gcp-sa-for-al2-20190903@cm-xxxxxxxxxxx.iam.gserviceaccount.com
disable_usage_reporting = True
project = cm-xxxxxxxxxxx

Your active configuration is: [default]
====

gcloud components updateコマンドでツールのアップデートを試みてみましたが、エラーが出てしまいました。この辺、yum実行時に設定ファイルを微調整した影響でしょうか。ひとまず代替コマンドは提供されているのでそれに従い処理を実行、アップデート作業自体は出来ました。

$ gcloud components update
ERROR: (gcloud.components.update) 
You cannot perform this action because the Cloud SDK component manager 
is disabled for this installation. You can run the following command 
to achieve the same result for this installation: 

sudo yum makecache && sudo yum update kubectl google-cloud-sdk google-cloud-sdk-app-engine-grpc google-cloud-sdk-pubsub-emulator google-cloud-sdk-app-engine-go google-cloud-sdk-firestore-emulator google-cloud-sdk-cloud-build-local google-cloud-sdk-datastore-emulator google-cloud-sdk-app-engine-python google-cloud-sdk-cbt google-cloud-sdk-bigtable-emulator google-cloud-sdk-datalab google-cloud-sdk-app-engine-java

$ sudo yum makecache && sudo yum update kubectl google-cloud-sdk google-cloud-sdk-app-engine-grpc google-cloud-sdk-pubsub-emulator google-cloud-sdk-app-engine-go google-cloud-sdk-firestore-emulator google-cloud-sdk-cloud-build-local google-cloud-sdk-datastore-emulator google-cloud-sdk-app-engine-python google-cloud-sdk-cbt google-cloud-sdk-bigtable-emulator google-cloud-sdk-datalab google-cloud-sdk-app-engine-java
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                                           | 2.4 kB  00:00:00     
amzn2extra-docker                                                                                                    | 1.3 kB  00:00:00     
google-cloud-sdk                                                                                                     | 1.4 kB  00:00:00     
(1/6): amzn2extra-docker/2/x86_64/filelists_db                                                                       | 9.3 kB  00:00:00     
(2/6): amzn2-core/2/x86_64/other_db                                                                                  |  504 B  00:00:00     
(3/6): amzn2extra-docker/2/x86_64/other_db                                                                           |  504 B  00:00:00     
(4/6): google-cloud-sdk/other                                                                                        |  38 kB  00:00:00     
(5/6): amzn2-core/2/x86_64/filelists_db                                                                              |  29 MB  00:00:01     
(6/6): google-cloud-sdk/filelists                                                                                    |  25 MB  00:00:01     
google-cloud-sdk                                                                                                                    878/878
google-cloud-sdk                                                                                                                    878/878
Metadata Cache Created
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Package(s) kubectl available, but not installed.
Package(s) google-cloud-sdk-app-engine-grpc available, but not installed.
Package(s) google-cloud-sdk-pubsub-emulator available, but not installed.
Package(s) google-cloud-sdk-app-engine-go available, but not installed.
Package(s) google-cloud-sdk-firestore-emulator available, but not installed.
Package(s) google-cloud-sdk-cloud-build-local available, but not installed.
Package(s) google-cloud-sdk-datastore-emulator available, but not installed.
Package(s) google-cloud-sdk-app-engine-python available, but not installed.
Package(s) google-cloud-sdk-cbt available, but not installed.
Package(s) google-cloud-sdk-bigtable-emulator available, but not installed.
Package(s) google-cloud-sdk-datalab available, but not installed.
Package(s) google-cloud-sdk-app-engine-java available, but not installed.
No packages marked for update
$ 
$ gcloud version
Google Cloud SDK 261.0.0
alpha 2019.08.30
beta 2019.08.30
bq 2.0.47
core 2019.08.30
gsutil 4.42
kubectl 2019.08.30

まとめ

というわけでGoogle Cloud SDKのインストール手順、Amazon Linux 2&サービスアカウントキー利用の場合の手順のご紹介でした。公式の手順に則って進めた部分で一部進まない部分があったのが気になりますが、一旦はここで区切りとしてまた別の機会に確認してみたいと思います。