github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/terraform/gcp/README.md (about)

     1  # Provision a Nomad cluster on GCP
     2  
     3  [![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://ssh.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fhashicorp%2Fnomad&cloudshell_working_dir=terraform%2Fgcp&cloudshell_tutorial=README.md)
     4  
     5  To get started, you will need a GCP [account](https://cloud.google.com/free).
     6  
     7  ## Welcome
     8  
     9  This tutorial will teach you how to deploy [Nomad](https://www.nomadproject.io/) clusters to the Google Cloud Platform using [Packer](https://www.packer.io/) and [Terraform](https://www.terraform.io/).
    10  
    11  Includes:
    12  
    13  * Installing HashiCorp Tools (Nomad, Consul, Vault, Terraform, and Packer).
    14  * Installing the GCP SDK CLI Tools, if you're not using Cloud Shell.
    15  * Creating a new GCP project, along with a Terraform Service Account.
    16  * Building a golden image using Packer.
    17  * Deploying a cluster with Terraform.
    18  
    19  ## Install HashiCorp Tools
    20  
    21  ### Nomad
    22  
    23  Download the latest version of [Nomad](https://www.nomadproject.io/) from HashiCorp's website by copying and pasting this snippet in the terminal:
    24  
    25  ```console
    26  curl "https://releases.hashicorp.com/nomad/0.12.4/nomad_0.12.4_linux_amd64.zip" -o nomad.zip
    27  unzip nomad.zip
    28  sudo mv nomad /usr/local/bin
    29  nomad --version
    30  ```
    31  
    32  ### Consul
    33  
    34  Download the latest version of [Consul](https://www.consul.io/) from HashiCorp's website by copying and pasting this snippet in the terminal:
    35  
    36  ```console
    37  curl "https://releases.hashicorp.com/consul/1.8.3/consul_1.8.3_linux_amd64.zip" -o consul.zip
    38  unzip consul.zip
    39  sudo mv consul /usr/local/bin
    40  consul --version
    41  ```
    42  
    43  ### Vault
    44  
    45  Download the latest version of [Vault](https://www.vaultproject.io/) from HashiCorp's website by copying and pasting this snippet in the terminal:
    46  
    47  ```console
    48  curl "https://releases.hashicorp.com/vault/1.5.3/vault_1.5.3_linux_amd64.zip" -o vault.zip
    49  unzip vault.zip
    50  sudo mv vault /usr/local/bin
    51  vault --version
    52  ```
    53  
    54  ### Packer
    55  
    56  Download the latest version of [Packer](https://www.packer.io/) from HashiCorp's website by copying and pasting this snippet in the terminal:
    57  
    58  ```console
    59  curl "https://releases.hashicorp.com/packer/1.6.2/packer_1.6.2_linux_amd64.zip" -o packer.zip
    60  unzip packer.zip
    61  sudo mv packer /usr/local/bin
    62  packer --version
    63  ```
    64  
    65  ### Terraform
    66  
    67  Download the latest version of [Terraform](https://www.terraform.io/) from HashiCorp's website by copying and pasting this snippet in the terminal:
    68  
    69  ```console
    70  curl "https://releases.hashicorp.com/terraform/0.13.1/terraform_0.13.1_linux_amd64.zip" -o terraform.zip
    71  unzip terraform.zip
    72  sudo mv terraform /usr/local/bin
    73  terraform --version
    74  ```
    75  
    76  ### Install and Authenticate the GCP SDK Command Line Tools
    77  
    78  **If you are using [Google Cloud](https://cloud.google.com/shell), you already have `gcloud` set up, and you can safely skip this step.**
    79  
    80  To install the GCP SDK Command Line Tools, follow the installation instructions for your specific operating system: 
    81  
    82  * [Linux](https://cloud.google.com/sdk/docs/downloads-interactive#linux)
    83  * [MacOS](https://cloud.google.com/sdk/docs/downloads-interactive#mac)
    84  * [Windows](https://cloud.google.com/sdk/docs/downloads-interactive#windows)
    85  
    86  After installation, authenticate `gcloud` with the following command:
    87  
    88  ```console
    89  gcloud auth login
    90  ```
    91  
    92  ## Create a New Project
    93  
    94  Generate a project ID with the following command:
    95  
    96  ```console
    97  export GOOGLE_PROJECT="nomad-gcp-$(cat /dev/random | head -c 5 | xxd -p)"
    98  ```
    99  
   100  Using that project ID, create a new GCP [project](https://cloud.google.com/docs/overview#projects):
   101  
   102  ```console
   103  gcloud projects create $GOOGLE_PROJECT
   104  ```
   105  
   106  And then set your `gcloud` config to use that project:
   107  
   108  ```console
   109  gcloud config set project $GOOGLE_PROJECT
   110  ```
   111  
   112  ### Link Billing Account to Project
   113  
   114  Next, let's link a billing account to that project. To determine what billing accounts are available, run the following command:
   115  
   116  ```console
   117  gcloud alpha billing accounts list
   118  ```
   119  
   120  Locate the `ACCOUNT_ID` for the billing account you want to use, and set the `GOOGLE_BILLING_ACCOUNT` environment variable. Replace the `XXXXXXX` with the `ACCOUNT_ID` you located with the previous command output:
   121  
   122  ```console
   123  export GOOGLE_BILLING_ACCOUNT="XXXXXXX"
   124  ```
   125  
   126  So we can link the `GOOGLE_BILLING_ACCOUNT` with the previously created `GOOGLE_PROJECT`:
   127  
   128  ```console
   129  gcloud alpha billing projects link "$GOOGLE_PROJECT" --billing-account "$GOOGLE_BILLING_ACCOUNT"
   130  ```
   131  
   132  ### Enable Compute API
   133  
   134  In order to deploy VMs to the project, we need to enable the compute API:
   135  
   136  ```console
   137  gcloud services enable compute.googleapis.com
   138  ```
   139  
   140  ### Create Terraform Service Account
   141  
   142  Finally, let's create a Terraform Service Account user and its `account.json` credentials file:
   143  
   144  ```console
   145  gcloud iam service-accounts create terraform \
   146      --display-name "Terraform Service Account" \
   147      --description "Service account to use with Terraform"
   148  ```
   149  
   150  ```console
   151  gcloud projects add-iam-policy-binding "$GOOGLE_PROJECT" \
   152    --member serviceAccount:"terraform@$GOOGLE_PROJECT.iam.gserviceaccount.com" \
   153    --role roles/editor
   154  ```
   155  
   156  ```console
   157  gcloud iam service-accounts keys create account.json \
   158      --iam-account "terraform@$GOOGLE_PROJECT.iam.gserviceaccount.com"
   159  ```
   160  
   161  > ⚠️ **Warning**
   162  >
   163  > The `account.json` credentials gives privileged access to this GCP project. Be careful to avoid leaking these credentials by accidentally committing them to version control systems such as `git`, or storing them where they are visible to others. In general, storing these credentials on an individually operated, private computer (like your laptop) or in your own GCP cloud shell is acceptable for testing purposes. For production use, or for teams, use a secrets management system like HashiCorp [Vault](https://www.vaultproject.io/). For this tutorial's purposes, we'll be storing the `account.json` credentials on disk in the cloud shell.
   164  
   165  Now set the *full path* of the newly created `account.json` file as `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
   166  
   167  ```console
   168  export GOOGLE_APPLICATION_CREDENTIALS=$(realpath account.json)
   169  ```
   170  
   171  ### Ensure Required Environment Variables Are Set
   172  
   173  Before moving onto the next steps, ensure the following environment variables are set:
   174  
   175  * `GOOGLE_PROJECT` with your selected GCP project ID.
   176  * `GOOGLE_APPLICATION_CREDENTIALS` with the *full path* to the Terraform Service Account `account.json` credentials file created in the last step.
   177  
   178  ## Build HashiStack Golden Image with Packer
   179  
   180  [Packer](https://www.packer.io/intro/index.html) is HashiCorp's open source tool for creating identical machine images for multiple platforms from a single source configuration. The machine image created here can be customized through modifications to the [build configuration file](https://github.com/hashicorp/nomad/blob/master/terraform/gcp/packer.json) and the [shell script](https://github.com/hashicorp/nomad/blob/master/terraform/shared/scripts/setup.sh).
   181  
   182  Use the following command to build the machine image:
   183  
   184  ```console
   185  packer build packer.json
   186  ```
   187  
   188  ## Provision a cluster with Terraform
   189  
   190  Change into the `env/us-east` environment directory:
   191  
   192  ```console
   193  cd env/us-east
   194  ```
   195  
   196  Initialize Terraform:
   197  
   198  ```console
   199  terraform init
   200  ```
   201  
   202  Plan infrastructure changes with Terraform:
   203  
   204  ```console
   205  terraform plan -var="project=${GOOGLE_PROJECT}" -var="credentials=${GOOGLE_APPLICATION_CREDENTIALS}" 
   206  ```
   207  
   208  Apply infrastructure changes with Terraform:
   209  
   210  ```console
   211  terraform apply -auto-approve -var="project=${GOOGLE_PROJECT}" -var="credentials=${GOOGLE_APPLICATION_CREDENTIALS}" 
   212  ```
   213  
   214  ## Access the Cluster
   215  
   216  To access the Nomad, Consul, or Vault web UI inside the cluster, create an [SSH tunnel](https://cloud.google.com/community/tutorials/ssh-tunnel-on-gce) using `gcloud`. To open up tunnels to *all* of the UIs available in the cluster, run these commands which will start each SSH tunnel as a background process in your current shell:
   217  
   218  ```console
   219  gcloud compute ssh hashistack-server-0 --zone=us-east1-c --tunnel-through-iap -- -f -N -L 127.0.0.1:4646:127.0.0.1:4646
   220  gcloud compute ssh hashistack-server-0 --zone=us-east1-c --tunnel-through-iap -- -f -N -L 127.0.0.1:8200:127.0.0.1:8200
   221  gcloud compute ssh hashistack-server-0 --zone=us-east1-c --tunnel-through-iap -- -f -N -L 127.0.0.1:8500:127.0.0.1:8500
   222  ```
   223  
   224  After running those commands, you can now click any of the following links to open up a Web Preview using Cloud Shell:
   225  
   226  * [Nomad](https://ssh.cloud.google.com/devshell/proxy?authuser=0&port=4646&environment_id=default)
   227  * [Vault](https://ssh.cloud.google.com/devshell/proxy?authuser=0&port=8200&environment_id=default)
   228  * [Consul](https://ssh.cloud.google.com/devshell/proxy?authuser=0&port=8500&environment_id=default)
   229  
   230  If you're **not** using Cloud Shell, you can use any of these links:
   231  
   232  * [Nomad](http://127.0.0.1:4646)
   233  * [Vault](http://127.0.0.1:8200)
   234  * [Consul](http://127.0.0.1:8500)
   235  
   236  In case you want to try out any of the optional steps with the Vault CLI later on, set this helper variable: 
   237  
   238  ```
   239  export VAULT_ADDR=http://localhost:8200
   240  ```
   241  
   242  ## Next Steps
   243  
   244  You have deployed a Nomad cluster to GCP! 🎉
   245  
   246  Click [here](https://github.com/hashicorp/nomad/blob/master/terraform/README.md#test) for next steps.
   247  
   248  > ### After You Finish
   249  > Come back here when you're done exploring Nomad and the HashiCorp stack. In the next section, you'll learn how to clean up, and will destroy the demo infrastructure you've created. 
   250  
   251  ## Conclusion
   252  
   253  You have deployed a Nomad cluster to GCP! 
   254  
   255  ### Destroy Infrastructure
   256  
   257  To destroy all the demo infrastructure: 
   258  
   259  ```console
   260  terraform destroy -force -var="project=${GOOGLE_PROJECT}" -var="credentials=${GOOGLE_APPLICATION_CREDENTIALS}" 
   261  ```
   262  ### Delete the Project
   263  
   264  Finally, to completely delete the project: 
   265  
   266  gcloud projects delete $GOOGLE_PROJECT
   267  
   268  > ### Alternative: Use the GUI
   269  > 
   270  > If you prefer to delete the project using GCP's Cloud Console, follow this link to GCP's [Cloud Resource Manager](https://console.cloud.google.com/cloud-resource-manager).
   271