2016年8月28日日曜日

Google Compute Engine インスタンス間でやり取り(service account と mysql in Debian)

Google Compute Engine間でやり取りをするときの設定です。

gcloudを既存のインスタンスで使う

Service Accountsを作って、その認証情報をgcloudに設定します。

認証エラーが出ます

認証設定してない時は、このようなエラーが出ます。

$ gcloud compute instances list
ERROR: (gcloud.compute.instances.list) Some requests did not succeed:
 - Insufficient Permission

auth loginでセットアップしようとすると...

$ gcloud auth login

You are running on a Google Compute Engine virtual machine.
It is recommended that you use service accounts for authentication.

service accountsを使った方が良いと勧められます。

サービスアカウントを作成

今回は、コンソールからサービスアカウントを追加しました。 詳細手順は、公式ドキュメントのCONSOLEタブにあります。

Creating and Managing Service Accounts

gcloudの設定

上記で取得した、キーファイルをインスタンス上に設置して、アクティベイトします。

$ vi ~/.cred.json
$ chmod 400 ~/.cred.json
$ gcloud auth activate-service-account --key-file ~/.cred.json
$ gcloud compute instances list
...

参考: gcloud auth activate-service-account Creating and Enabling Service Accounts for Instances

MySQLをインスタンス間で接続する

MySQLを設定して、インスタンス間のネットワークを設定します。

とりあえずつなぐと疎通出来ませんでした。

$ telnet 10.240.0.4 3306
Trying 10.240.0.4...
telnet: Unable to connect to remote host: Connection refused

Compute Engine

  • インスタンスのネットワーク設定を確認する
  • デフォルトで、default-allow-internalがついてる。変えて無ければOK。

How to Set Up Remote Access to MySQL on Google Compute Engine Google Compute Engineのファイヤーウォールを理解する

mysqlの設定

クライアント側でmysql-clinetをインストール

sudo apt-get install mysql-client

Debianでは、セキュリティ上の理由から、MySQLサーバは、デフォルトでリモートアクセスを停止しています。

  • /etc/my.cnfbind-address=${instance-ip}に設定します
  • mysqlをリスタートします。

    $ sudo systemctl restart mysql
    $ systemctl status mysql

mysql server

あとは、ユーザの権限を確認します

select user, host from mysq.user

確認

クライアント側のtelnetで疎通確認

$ telnet XXX 3306
Trying XXX...
Connected to XXX.
Escape character is '^]'.

クライアント側のmysqlコマンドで接続

mysql -u xxx -p -h xxx

0 件のコメント:

コメントを投稿