|
| 1 | +# Enable ACM features with Terraform - Part 1 |
| 2 | + |
| 3 | +This is Part1 of the tutorial to accompany a short series of blog articles explaining how to enable [Anthos Config Management (ACM)](https://cloud.google.com/anthos/config-management) with Terraform. |
| 4 | + |
| 5 | +In this tutorial, we'll explain how to use Teraform to create a cluster and manage its config from git via [Config Sync](https://cloud.google.com/anthos-config-management/docs/config-sync-overview). |
| 6 | + |
| 7 | +[Next part](../acm-terraform-blog-part2) will build on that to add guard rails for the cluster via [Policy Controller](https://cloud.google.com/anthos-config-management/docs/concepts/policy-controller). We will focus on enabling an ongoing audit of cluster resources using the Policy Controller's built in [Policy Library](http://cloud/anthos-config-management/docs/reference/constraint-template-library) and a bundle of constraints enforcings [CIS Kubernetes Benchmark v.1.5.1](https://cloud.google.com/kubernetes-engine/docs/concepts/cis-benchmarks). |
| 8 | + |
| 9 | +Subsequent articles will discuss other aspects of ACM to manage your GCP infrastrcuture. |
| 10 | + |
| 11 | +## Enable Config Sync on the cluster with Terraform |
| 12 | + |
| 13 | +1. Clone this repo |
| 14 | +1. Set variables that will be used in multiple commands: |
| 15 | + |
| 16 | + ```bash |
| 17 | + FOLDER_ID = [FOLDER] |
| 18 | + BILLING_ACCOUNT = [BILLING_ACCOUNT] |
| 19 | + PROJECT_ID = [PROJECT_ID] |
| 20 | + ``` |
| 21 | + |
| 22 | +1. Create project: |
| 23 | + |
| 24 | + ```bash |
| 25 | + gcloud auth login |
| 26 | + gcloud projects create $PROJECT_ID --name=$PROJECT_ID --folder=$FOLDER_ID |
| 27 | + gcloud alpha billing projects link $PROJECT_ID --billing-account $BILLING_ACCOUNT |
| 28 | + gcloud config set project $PROJECT_ID |
| 29 | + ``` |
| 30 | + |
| 31 | +1. Create cluster using terraform using defaults other than the project: |
| 32 | + |
| 33 | + ```bash |
| 34 | + # obtain user access credentials to use for Terraform commands |
| 35 | + gcloud auth application-default login |
| 36 | +
|
| 37 | + # continue in /terraform directory |
| 38 | + cd terraform |
| 39 | +
|
| 40 | + terraform init |
| 41 | + terraform plan -var=project=$PROJECT_ID |
| 42 | + terraform apply -var=project=$PROJECT_ID |
| 43 | + ``` |
| 44 | + NOTE: if you get an error due to default network not being present, run `gcloud compute networks create default --subnet-mode=auto` and retry the commands. |
| 45 | + |
| 46 | +1. To verify things have sync'ed, you can use `gcloud` to check status: |
| 47 | +
|
| 48 | + ```bash |
| 49 | + gcloud alpha container hub config-management status --project $PROJECT_ID |
| 50 | + ``` |
| 51 | +
|
| 52 | + In the output, notice that the `Status` will eventually show as `SYNCED` and the `Last_Synced_Token` will match the repo hash. |
| 53 | +
|
| 54 | +1. To see wordpress itself, you can use the kubectl proxy to connect to the service: |
| 55 | +
|
| 56 | + ```bash |
| 57 | + # get values from cluster that was created |
| 58 | + export CLUSTER_ZONE=$(terraform output -raw cluster_location) |
| 59 | + export CLUSTER_NAME=$(terraform output -raw cluster_name) |
| 60 | +
|
| 61 | + # then get creditials for it and proxy to the wordpress service to see it running |
| 62 | + gcloud container clusters get-credentials $CLUSTER_NAME --zone $CLUSTER_ZONE --project $PROJECT_ID |
| 63 | + kubectl proxy --port 8888 & |
| 64 | +
|
| 65 | + # curl or use the browser |
| 66 | + curl http://127.0.0.1:8888/api/v1/namespaces/default/services/wordpress/proxy/wp-admin/install.php |
| 67 | +
|
| 68 | + ``` |
| 69 | +
|
| 70 | +1. Finally, let's clean up. First, don't forget to foreground the proxy again to kill it. Also, apply `terraform destroy` to remove the GCP resources that were deployed to the project. |
| 71 | +
|
| 72 | + ```bash |
| 73 | + fg # ctrl-c |
| 74 | +
|
| 75 | + terraform destroy -var=project=$PROJECT_ID |
| 76 | + ``` |
0 commit comments