This repository contains a Terraform script to create an Autopilot Kubernetes cluster, and deploy a kubernetes ingress service on Google Cloud Platform (GCP).
- Create an Autopilot Kubernetes cluster
- Deploy a Kubernetes service with default Nginx image
- Expose the service using Kubernetes Ingress
- Create a Cloud Armor security policy
- Terraform
- GCP account with required permissions
- Google Cloud SDK
To install Terraform, follow these steps:
-
Download the package from the Terraform downloads page.
-
Unzip the package. Terraform runs as a single binary named
terraform
. -
Move the Terraform binary to a directory included in your system's
PATH
.
For example, on Linux or macOS:
unzip terraform_*_linux_amd64.zip
sudo mv terraform /usr/local/bin/
On Windows, you can move the binary to a directory included in your PATH, or add the directory containing the binary to your PATH.
To install the Google Cloud SDK:
-
Visit the Google Cloud SDK documentation.
-
Follow the installation and initialization instructions for your operating system.
This SDK includes the gcloud command-line tool, which is necessary to authenticate with Google Cloud, set configuration values, and interact with Google Cloud APIs.
Make sure you have a Google Cloud Platform account. If you don't have one, you can create a new account and take advantage of the $300 free credit to get started with any GCP product.
Before you begin, you'll need to set up a Google Cloud Platform project:
- Create a new GCP project via the GCP Console.
- Enable the GCP services used by this project. You can do that by running the following command:
gcloud services enable container.googleapis.com
- Authenticate your GCP account using the following command:
gcloud auth login
gcloud auth application-default login
- Set your GCP project ID:
gcloud config set project <PROJECT_ID>
The terraform.tfvars
file must be populated with appropriate values for the project to work. Here's an example of what the contents might look like:
project = "<YOUR PROJECT ID>"
region = "europe-west1"
repository_id = "<YOUR REPOSITORY ID/NAME>"
location = "europe"
format = "DOCKER"
cluster_name = "<YOUR CLUSTER ID/NAME>"
network = "<YOUR NETWORK NAME>"
subnetwork = "<YOUR SUBNETWORK NAME>"
deployment_name = "<YOUR K8s DEPLOYMENT NAME>"
replicas = 1
image_url = "nginx:latest"
container_port = 80
service_name = "<YOUR SERVICE NAME>"
service_port = 80
target_port = 80
service_type = "NodePort"
policy_name = "<YOUR SECURITY POLICY NAME>"
policy_type = "CLOUD_ARMOR"
To deploy your infrastructure, follow these steps:
- Initialize your Terraform workspace, which will download the provider plugins:
terraform init
- Validate your Terraform configuration:
terraform validate
- View the actions that Terraform will perform:
terraform plan
- If everything is as expected, apply the Terraform:
terraform apply
After the process is complete, Terraform will have created your GCP Artifact Registry repository, built and pushed your Docker image, created a GCP Autopilot Kubernetes cluster, deployed your service and also create security policy in Cloud Armor.