Last active
May 17, 2017 23:03
-
-
Save yamamoto-febc/f1546105016143d3478510e2e06ee795 to your computer and use it in GitHub Desktop.
Terraform for さくらのクラウドでRancherをセットアップ ref: http://qiita.com/yamamoto-febc/items/3a2a3154244cb90443e0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sudo ros os upgrade -f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sudo ros engine switch docker-1.12.6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| docker run -d --restart=unless-stopped -p 8080:8080 rancher/server |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # RancherOS自体のアップグレード | |
| provisioner "remote-exec" { | |
| inline = [ | |
| "sudo ros os upgrade -f" | |
| ] | |
| } | |
| # dockerのバージョン変更(kubernetesを利用する場合などで必要) & Rancherサーバ起動 | |
| provisioner "remote-exec" { | |
| inline = [ | |
| "sudo ros engine switch docker-1.12.6", | |
| "sleep 10", | |
| "docker run -d --restart=unless-stopped -p 8080:8080 rancher/server" | |
| ] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| resource "sakuracloud_server" "rancher_hosts" { | |
| # [...省略...] | |
| provisioner "remote-exec" { | |
| inline = [ | |
| "${rancher_registration_token.demo-token.command}" | |
| ] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| provider "rancher" { | |
| api_url = "http://<RancherサーバのIPアドレス>:8080" | |
| # APIキー(AccessKey) | |
| #access_key = "" // RANCHER_ACCESS_KEY環境変数でもOK | |
| # APIキー(SecretKey) | |
| #secret_key = "" // RANCHER_SECRET_KEY環境変数でもOK | |
| } | |
| # environment定義 | |
| resource "rancher_environment" "demo" { | |
| name = "rancher-on-sakuracloud-demo" | |
| description = "rancher on sakuracloud demo" | |
| # 注: terraform v0.9.4時点ではバグがあり、 | |
| # orchestration = cattle以外(swarmやmesos,kubernetesなど)は動作しません。 | |
| orchestration = "cattle" | |
| } | |
| # ホスト登録用のトークン定義 | |
| resource "rancher_registration_token" "demo-token" { | |
| environment_id = "${rancher_environment.demo.id}" | |
| name = "demo-token" | |
| description = "Host registration token" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 変数定義 | |
| variable server_spec { | |
| default = { | |
| # サーバの管理者パスワード | |
| password = "PUT_YOUR_PASSSWORD" | |
| # コア数 | |
| core = 2 | |
| # メモリ(GB) | |
| memory = 4 | |
| } | |
| } | |
| # さくらのクラウド プロバイダ設定 | |
| provider sakuracloud { | |
| # APIトークン: 環境変数SAKURACLOUD_ACCESS_TOKENを設定する、または以下の行で設定する | |
| # token = "YOUR_TOKEN_HERE" | |
| # APIシークレット: 環境変数SAKURACLOUD_ACCESS_TOKENを設定する、または以下の行で設定する | |
| # secret = "YOUR_SECRET_HERE" | |
| # 操作対象ゾーン | |
| zone = "tk1a" | |
| } | |
| # SSH公開鍵の定義(さくらのクラウド上でキーペアを生成する) | |
| resource "sakuracloud_ssh_key_gen" "key" { | |
| name = "for_rancher_key" | |
| # 作成後にローカルマシンに保存 | |
| provisioner "local-exec" { | |
| command = "echo \"${self.private_key}\" > id_rsa; chmod 0600 id_rsa" | |
| } | |
| # destroy時にローカルマシン上からも削除 | |
| provisioner "local-exec" { | |
| when = "destroy" | |
| command = "rm -f id_rsa" | |
| } | |
| } | |
| # RancherOSパブリックアーカイブ | |
| data sakuracloud_archive "rancher" { | |
| os_type = "rancheros" | |
| } | |
| # ディスクの定義 | |
| resource "sakuracloud_disk" "disk" { | |
| name = "rancher_server" | |
| # RancherOSを元にディスク作成 | |
| source_archive_id = "${data.sakuracloud_archive.rancher.id}" | |
| password = "${var.server_spec["password"]}" | |
| hostname = "rancher_server" | |
| # 生成したSSH公開鍵を登録しておく | |
| ssh_key_ids = ["${sakuracloud_ssh_key_gen.key.id}"] | |
| } | |
| # サーバの定義 | |
| resource "sakuracloud_server" "server" { | |
| name = "rancher_server" | |
| disks = ["${sakuracloud_disk.disk.id}"] | |
| tags = ["@virtio-net-pci"] | |
| core = "${var.server_spec["core"]}" | |
| memory = "${var.server_spec["memory"]}" | |
| # プロビジョニング定義 | |
| connection { | |
| user = "rancher" | |
| host = "${self.ipaddress}" | |
| private_key = "${sakuracloud_ssh_key_gen.key.private_key}" | |
| } | |
| # RancherOS自体のアップグレード | |
| provisioner "remote-exec" { | |
| inline = [ | |
| "sudo ros os upgrade -f" | |
| ] | |
| } | |
| # dockerのバージョン変更(kubernetesを利用する場合などで必要) & Rancherサーバ起動 | |
| provisioner "remote-exec" { | |
| inline = [ | |
| "sudo ros engine switch docker-1.12.6", | |
| "sleep 10", | |
| "docker run -d --restart=unless-stopped -p 8080:8080 rancher/server" | |
| ] | |
| } | |
| } | |
| # Rancherサーバへの接続用URL | |
| output rancher_server_url { | |
| value = "http://${sakuracloud_server.server.ipaddress}:8080/" | |
| } | |
| # RancherサーバへのSSH接続用コマンド | |
| output rancher_server_ssh { | |
| value = "ssh -i id_rsa rancher@${sakuracloud_server.server.ipaddress}" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ terraform apply | |
| [...省略...] | |
| Apply complete! Resources: 3 added, 0 changed, 0 destroyed. | |
| [...省略...] | |
| Outputs: | |
| rancher_server_ssh = ssh -i id_rsa [email protected] | |
| rancher_server_url = http://192.2.0.1:8080/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment