Vagrant Google Compute Engineプロバイダ
Vagrant本によるとAWSプロバイダがあるそうで、GCE版もあるかとググったところ、案の定、Vagrant GCE Providerが見つかった。プロバイダとはハイパーバイザやクラウドのように、仮想環境を実行するプラットフォームのことで、VirtualBox以外にVMWareやHyper-V、Docker、AWSなどに対応している。
GCEプロバイダはプラグインとして提供されているので、ホームページにしたがってインストールを進める。
$ vagrant plugin install vagrant-google Installing the 'vagrant-google' plugin. This can take a few minutes... Installed the plugin 'vagrant-google (0.1.3)'!
前準備として、GCEのDeveloper consoleの"API & Auth" -> CredentialsからClient IDを生成する。アプリケーションタイプはService account。生成が終わると、秘密鍵(*.p12)が自動的にダウンロードされ、パスワードがポップアップされる。これでプラグインの利用に必要なE-mailアドレスがページに表示されているはず。
続いてダミーのboxを追加する。
$ vagrant box add gce https://github.com/mitchellh/vagrant-google/raw/master/google.box ==> box: Adding box 'gce' (v0) for provider: box: Downloading: https://github.com/mitchellh/vagrant-google/raw/master/google.box ==> box: Successfully added box 'gce' (v0) for 'google'!
次にVagrantfileを編集する。試しに作ったのはこんな感じ。GCEだとユーザ名はVagrantではなく、ローカルユーザ名と同じになるので、それに合わせてoverrideする。Project IDなどは直接書くのは(gitなどで管理するには)セキュアじゃないので、環境変数で与えるようにしておく。
Vagrant.configure("2") do |config| config.vm.box = "gce" config.vm.provider :google do |google, override| google.google_project_id = ENV['GCE_PROJECT_ID'] google.google_client_email = ENV['GCE_CLIENT_EMAIL'] google.google_key_location = ENV['GCE_KEY_LOCATION'] google.image = "centos-7-v20140903" google.machine_type = "f1-micro" google.zone = "asia-east1-c" override.ssh.username = "oraccha" override.ssh.private_key_path = "~/.ssh/google_compute_engine" end end
vagrant upする。なんだか共有フォルダ(正確にはrsyncを使ったsynched folderだっけ)の作成でエラーが出ているけど、とりあえず無視する*1。
$ vagrant up --provider=google : Vagrant::Errors::VagrantError: The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed! mkdir -p '/vagrant' Stdout from the command: Stderr from the command: sudo: sorry, you must have a tty to run sudo
はい、ちゃんとSSHできた。
$ vagrant ssh [oraccha@i-2014091622 ~]$ sudo virt-what kvm
vagrant destroyでVMインスタンスは破棄される。あと、vagrant suspendやhaltには対応していないみたいね。
- 作者: Mitchell Hashimoto,Sky株式会社玉川竜司
- 出版社/メーカー: オライリージャパン
- 発売日: 2014/02/21
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (10件) を見る