You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.4 KiB

  1. Using Terraform
  2. ===============
  3. This is a generic `Terraform <https://www.terraform.io/>`__
  4. configuration that sets up DigitalOcean droplets. See the
  5. `terraform-digitalocean <https://github.com/tendermint/tools/tree/master/terraform-digitalocean>`__
  6. for the required files.
  7. Prerequisites
  8. -------------
  9. - Install `HashiCorp Terraform <https://www.terraform.io>`__ on a linux
  10. machine.
  11. - Create a `DigitalOcean API
  12. token <https://cloud.digitalocean.com/settings/api/tokens>`__ with
  13. read and write capability.
  14. - Create a private/public key pair for SSH. This is needed to log onto
  15. your droplets as well as by Ansible to connect for configuration
  16. changes.
  17. - Set up the public SSH key at the `DigitalOcean security
  18. page <https://cloud.digitalocean.com/settings/security>`__.
  19. `Here <https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets>`__'s
  20. a tutorial.
  21. - Find out your SSH key ID at DigitalOcean by querying the below
  22. command on your linux box:
  23. ::
  24. DO_API_TOKEN="<The API token received from DigitalOcean>"
  25. curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $DO_API_TOKEN" "https://api.digitalocean.com/v2/account/keys"
  26. Initialization
  27. --------------
  28. If this is your first time using terraform, you have to initialize it by
  29. running the below command. (Note: initialization can be run multiple
  30. times)
  31. ::
  32. terraform init
  33. After initialization it's good measure to create a new Terraform
  34. environment for the droplets so they are always managed together.
  35. ::
  36. TESTNET_NAME="testnet-servers"
  37. terraform env new "$TESTNET_NAME"
  38. Note this ``terraform env`` command is only available in terraform
  39. ``v0.9`` and up.
  40. Execution
  41. ---------
  42. The below command will create 4 nodes in DigitalOcean. They will be
  43. named ``testnet-servers-node0`` to ``testnet-servers-node3`` and they
  44. will be tagged as ``testnet-servers``.
  45. ::
  46. DO_API_TOKEN="<The API token received from DigitalOcean>"
  47. SSH_IDS="[ \"<The SSH ID received from the curl call above.>\" ]"
  48. terraform apply -var TESTNET_NAME="testnet-servers" -var servers=4 -var DO_API_TOKEN="$DO_API_TOKEN" -var ssh_keys="$SSH_IDS"
  49. Note: ``ssh_keys`` is a list of strings. You can add multiple keys. For
  50. example: ``["1234567","9876543"]``.
  51. Alternatively you can use the default settings. The number of default
  52. servers is 4 and the testnet name is ``tf-testnet1``. Variables can also
  53. be defined as environment variables instead of the command-line.
  54. Environment variables that start with ``TF_VAR_`` will be translated
  55. into the Terraform configuration. For example the number of servers can
  56. be overriden by setting the ``TF_VAR_servers`` variable.
  57. ::
  58. TF_VAR_DO_API_TOKEN="<The API token received from DigitalOcean>"
  59. TF_VAR_TESTNET_NAME="testnet-servers"
  60. terraform-apply
  61. Security
  62. --------
  63. DigitalOcean uses the root user by default on its droplets. This is fine
  64. as long as SSH keys are used. However some people still would like to
  65. disable root and use an alternative user to connect to the droplets -
  66. then ``sudo`` from there. Terraform can do this but it requires SSH
  67. agent running on the machine where terraform is run, with one of the SSH
  68. keys of the droplets added to the agent. (This will be neede for ansible
  69. too, so it's worth setting it up here. Check out the
  70. `ansible <https://github.com/tendermint/tools/tree/master/ansible>`__
  71. page for more information.) After setting up the SSH key, run
  72. ``terraform apply`` with ``-var noroot=true`` to create your droplets.
  73. Terraform will create a user called ``ec2-user`` and move the SSH keys
  74. over, this way disabling SSH login for root. It also adds the
  75. ``ec2-user`` to the sudoers file, so after logging in as ec2-user you
  76. can ``sudo`` to ``root``.
  77. DigitalOcean announced firewalls but the current version of Terraform
  78. (0.9.8 as of this writing) does not support it yet. Fortunately it is
  79. quite easy to set it up through the web interface (and not that bad
  80. through the `RESTful
  81. API <https://developers.digitalocean.com/documentation/v2/#firewalls>`__
  82. either). When adding droplets to a firewall rule, you can add tags. All
  83. droplets in a testnet are tagged with the testnet name so it's enough to
  84. define the testnet name in the firewall rule. It is not necessary to add
  85. the nodes one-by-one. Also, the firewall rule "remembers" the testnet
  86. name tag so if you change the servers but keep the name, the firewall
  87. rules will still apply.