- Download Terraform binary:
wget https://releases.hashicorp.com/terraform/1.0.4/terraform_1.0.4_linux_amd64.zip
unzip terraform_1.0.4_linux_amd64.zip -d /usr/local/bin
- Verify you can run terraform:
terraform -v
- Download Google Cloud SDK:
curl https://sdk.cloud.google.com | bash
Move to /usr/local/bin and/or set your PATH if needed
Or via your package manager, i.e for Ubuntu/Debian:
echo "deb http://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install google-cloud-sdk
-
Run
gcloud init
and follow instructions -
Run
gcloud auth application-default login
and follow instructions -
Enable the following API's for the compute and container services:
gcloud services enable compute.googleapis.com
gcloud services enable container.googleapis.com
- You can now list your GKE clusters by running the following:
gcloud container clusters list
- You can very easily create a GKE cluster by running:
gcloud container clusters create \
--machine-type e2-micro \
--num-nodes 1 \
--zone us-east1-a \
--cluster-version latest \
testk8s-cluster
Creating cluster testk8s-cluster in us-east1-a... Cluster is being deployed... Cluster is being health-checked...
-
When the cluster is created you will find a kubeconfig file the the directory you ran the command from.
-
Install kubectl if you haven't alraeady:
sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update && sudo apt-get install -y kubectl
- Run
kubectl get nodes
- Run
gcloud container clusters update
to change settings in the cluster such as the number of nodes. - Destroy the cluster by running:
gcloud container clusters delete testk8s-cluster --zone us-east1-a
NOTE: be careful not to run this on a production cluster for obvious reasons!
-
Grab the terraform files in the repository
-
Amend the tf files in the repo to match your project, region, credentials file, etc
-
Run
terraform init
, this should initialize terraform and generate the state file -
Run
terraform validate
to make sure the config has no errors -
Run
terraform plan
which is essentially a dry-run and will provide a summary of all the changes that will be made and resources that will be created -
Now
terraform apply
, this will create the cluster environment, enter the project-id when prompted and hit yes to confirm you want to continue. Go grab a coffee, this may take a while. -
Inspect your directry tree structure, you will now see the state and kubeconfig files have been generated.
-
Run
export KUBECONFIG="${PWD}/kubeconfig-prod"
Then run:kubectl get pods --all-namespaces
-
Now you can create deployments in the cluster and apply them like any other kubernetes:
kubectl create -f deployment.yaml
- Run
terraform destroy
to destroy the entire cluster. NOTE: be careful not to run this in production!
NOTE: this will incur charges!
kubectl create -f svc-loadbalancer.yaml
kubectl get services
kubectl create -f ingress.yaml
kubectl describe ingress <pod>
see: https://kubernetes.io/docs/reference/kubectl/cheatsheet/
kubectl get pods -o wide
kubectl get ingress
kubectl get services
kubectl get nodes
kubectl logs <pod>
kubectl exec --stdin --tty <pod> -- /bin/bash
kubectl describe pods
kubectl describe nodes
kubectl create -f <yaml>
kubectl delete -f <yaml>
gcloud compute forwarding-rules list \ --filter description~hello-gke-k8s \ --format \ "table[box](name,IPAddress,target.segment(-2):label=TARGET_TYPE)"