|
| 1 | +# Using Terraform with GKE Attached Clusters |
| 2 | + |
| 3 | +The scripts provided here demonstrate how you can use Terraform to automate the process of |
| 4 | +creating, bootstrapping, and attaching a Kubernetes cluster. For a complete reference of the |
| 5 | +GKE attached clusters Terraform resource, see the |
| 6 | +[google_container_attached_cluster](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/container_attached_cluster) |
| 7 | +and |
| 8 | +[google_container_attached_install_manifest](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/container_attached_install_manifest) |
| 9 | +documentation. |
| 10 | + |
| 11 | +With GKE attached clusters you can manage any standard, [CNCF-compliant](https://www.cncf.io/certification/software-conformance/) |
| 12 | +Kubernetes installation, including clusters already in production. You can then add |
| 13 | +[Google Kubernetes Engine (GKE) Enterprise edition](https://cloud.google.com/kubernetes-engine) |
| 14 | +features to standardize and secure your clusters across multiple cloud environments and Kubernetes |
| 15 | +vendors. To learn more, see the |
| 16 | +[GKE attached clusters documentation](https://cloud.google.com/anthos/clusters/docs/multi-cloud/attached). |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +## High Level Process |
| 21 | + |
| 22 | +Attaching a cluster in GKE involves taking the steps below. The Terraform scripts provided in this |
| 23 | +github repository automatically perform steps 1-4, and are meant to provide a quick start to |
| 24 | +working with GKE attached clusters. |
| 25 | +1. Create a cluster. |
| 26 | +1. Invoke the [GenerateAttachedClustersInstallManifest](https://cloud.google.com/anthos/clusters/docs/multi-cloud/reference/rest/v1/projects.locations/generateAttachedClusterInstallManifest) |
| 27 | + API to retrieve a manifest of the bootstrapping deployment. |
| 28 | +1. Apply the manifest from step 1 to the cluster. |
| 29 | +1. Invoke the [Create](https://cloud.google.com/anthos/clusters/docs/multi-cloud/reference/rest/v1/projects.locations.attachedClusters/create) |
| 30 | + API to attach the cluster. |
| 31 | +1. (Optional) Delete the resources applied in step 2. |
| 32 | + |
| 33 | +## What Is Provided |
| 34 | + |
| 35 | +* The **AKS** folder contains a sample script for creating an AKS cluster on Azure and attaching it. |
| 36 | +* The **EKS** folder contains a sample script for creating an EKS cluster on AWS and attaching it. |
| 37 | +* The **modules** folder contains the `attached-install-manifest` module which demonstrates how to |
| 38 | +retrieve the manifest and apply it to the cluster using Helm. Both the AKS and EKS examples use it. |
| 39 | + |
| 40 | +## Prerequisites |
| 41 | + |
| 42 | +All samples assume the availability of ambient credentials, which are the default credentials |
| 43 | +automatically provided in the environment where you run the Terraform scripts. These credentials |
| 44 | +are typically obtained by authenticating your account using the Google Cloud SDK (gcloud) with the |
| 45 | +command `gcloud auth application-default login`. See the |
| 46 | +[documentation](https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login) |
| 47 | +for more information. |
| 48 | + |
| 49 | +The prerequisites for running the Terraform scripts are the following: |
| 50 | +1. Ensure the latest version of gcloud is [installed](https://cloud.google.com/sdk/docs/install). |
| 51 | +1. If you haven't already done so, [create](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project) |
| 52 | + your Google Cloud project. This generates a Google Cloud project ID and a project number. |
| 53 | +1. Set your active Google Cloud project and authenticate your account with the following commands: |
| 54 | + ```sh |
| 55 | + export PROJECT_ID=<your project id> |
| 56 | + gcloud auth login |
| 57 | + gcloud config set project $PROJECT_ID |
| 58 | + gcloud auth application-default login |
| 59 | + ``` |
| 60 | +1. Enable the GKE attached clusters API and its required services with the following commands: |
| 61 | + ```sh |
| 62 | + gcloud services enable gkemulticloud.googleapis.com |
| 63 | + gcloud services enable gkeconnect.googleapis.com |
| 64 | + gcloud services enable connectgateway.googleapis.com |
| 65 | + gcloud services enable cloudresourcemanager.googleapis.com |
| 66 | + gcloud services enable anthos.googleapis.com |
| 67 | + gcloud services enable logging.googleapis.com |
| 68 | + gcloud services enable monitoring.googleapis.com |
| 69 | + gcloud services enable opsconfigmonitoring.googleapis.com |
| 70 | + ``` |
| 71 | +1. Clusters will be created at a specific Kubernetes version. Attached clusters have an additional |
| 72 | + platform version that must be specified. The platform version’s _major.minor_ should match the |
| 73 | + cluster’s Kubernetes version. Both versions must be specified when attaching. |
| 74 | + You can list all supported platform versions using: |
| 75 | + ```sh |
| 76 | + gcloud container attached get-server-config --location=GOOGLE_CLOUD_REGION |
| 77 | + ``` |
| 78 | + There is also a Terraform data source that provides the same information: |
| 79 | + ``` |
| 80 | + data "google_container_attached_versions" "versions" { |
| 81 | + location = GCP_LOCATION |
| 82 | + project = GCP_PROJECT_ID |
| 83 | + } |
| 84 | + ``` |
0 commit comments