Skip to content

Instantly share code, notes, and snippets.

@jason-riddle
Last active August 1, 2020 19:04
Show Gist options
  • Save jason-riddle/38689c5a7d2e8e5f2bc0d1e668c5b065 to your computer and use it in GitHub Desktop.
Save jason-riddle/38689c5a7d2e8e5f2bc0d1e668c5b065 to your computer and use it in GitHub Desktop.

Prep Work (Google Cloud)

Visit https://console.cloud.google.com and a create project with the name of kubernetes-cluster and take note of the project id.

Or use gcloud alpha projects create kubernetes-cluster-1345 if you have access.

Update gcloud.

gcloud components install kubectl
gcloud --quiet components update

Run the init command.

gcloud init --console-only

Values can be tweaked, if required. Region and Zone can also be set with env var CLOUDSDK_COMPUTE_REGION and CLOUDSDK_COMPUTE_ZONE.

gcloud config configurations activate default
gcloud config set compute/region us-east1
gcloud config set compute/zone us-east1-b
gcloud config set account [email protected]
gcloud auth application-default login

Set your project to the correct project id. Project can also be set with the env var CLOUDSDK_CORE_PROJECT.

gcloud config set project kubernetes-cluster-1345

List before proceeding.

gcloud auth list
gcloud config list

Enable the Google Container Engine API on https://console.cloud.google.com/flows/enableapi?apiid=container.

Creating the Cluster

Create your container cluster. The type g1-small has 0.5 vCPUs, 1.7 GB of Memory, and the regular price is $0.027 while the premptible price is $0.0070 an hour. This is our default node pool.

Additonal info such as pricing and machine types are available as well.

gcloud beta container clusters create kubernetes-cluster \
  --cluster-version 1.5.1 \
  --zone us-east1-b \
  --machine-type g1-small \
  --disk-size 10 \
  --preemptible \
  --enable-autoupgrade \
  --num-nodes 1

Set gcloud defaults.

gcloud config set container/cluster kubernetes-cluster
gcloud container clusters get-credentials kubernetes-cluster

Create your node pool.

The type n1-standard-1 has 1 vCPU, 3.75 GB of Memory, and the regular price is $0.050 while the premptible price is $0.010 an hour. This is our small-pool.

gcloud beta container node-pools create small-pool \
  --machine-type n1-standard-1 \
  --disk-size 20 \
  --preemptible \
  --enable-autoupgrade \
  --num-nodes 1

The type n1-standard-4 has 4 vCPUs, 15 GB of Memory, and the regular price is $0.200 while the premptible price is $0.040 an hour. This is our medium-pool.

gcloud beta container node-pools create medium-pool \
  --machine-type n1-standard-4 \
  --disk-size 40 \
  --preemptible \
  --enable-autoupgrade \
  --num-nodes 1

The type n1-standard-16 has 16 vCPUs, 60 GB of Memory, and the regular price is $0.800 while the premptible price is $0.160 an hour. This is our large-pool.

gcloud beta container node-pools create large-pool \
  --machine-type n1-standard-16 \
  --disk-size 80 \
  --preemptible \
  --enable-autoupgrade \
  --num-nodes 0

Get info on your cluster.

gcloud container node-pools list
NAME          MACHINE_TYPE  DISK_SIZE_GB  NODE_VERSION
default-pool  g1-small      10            1.4.6
tiny          g1-small      10            1.4.6
gcloud compute instances list
NAME                                               ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP      STATUS
gke-kubernetes-cluster-default-pool-eec27ade-hklo  us-east1-b  g1-small      true         10.142.0.2   104.196.210.40   RUNNING
gke-kubernetes-cluster-tiny-41d181a4-eban          us-east1-b  g1-small      true         10.142.0.3   104.196.176.110  RUNNING
gcloud container clusters list
NAME                ZONE        MASTER_VERSION  MASTER_IP        MACHINE_TYPE  NODE_VERSION  NUM_NODES  STATUS
kubernetes-cluster  us-east1-b  1.4.6           104.196.117.207  g1-small      1.4.6         2          RUNNING
kubectl get nodes --show-labels
NAME                                                STATUS    AGE       LABELS
gke-kubernetes-cluster-default-pool-eec27ade-hklo   Ready     6m        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/instance-type=g1-small,beta.kubernetes.io/os=linux,cloud.google.com/gke-nodepool=default-pool,cloud.google.com/gke-preemptible=true,failure-domain.beta.kubernetes.io/region=us-east1,failure-domain.beta.kubernetes.io/zone=us-east1-b,kubernetes.io/hostname=gke-kubernetes-cluster-default-pool-eec27ade-hklo
gke-kubernetes-cluster-tiny-41d181a4-eban           Ready     1m        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/instance-type=g1-small,beta.kubernetes.io/os=linux,cloud.google.com/gke-nodepool=tiny,cloud.google.com/gke-preemptible=true,failure-domain.beta.kubernetes.io/region=us-east1,failure-domain.beta.kubernetes.io/zone=us-east1-b,kubernetes.io/hostname=gke-kubernetes-cluster-tiny-41d181a4-eban
kubectl cluster-info
Kubernetes master is running at https://10.0.0.5
GLBCDefaultBackend is running at https://10.0.0.5/api/v1/proxy/namespaces/kube-system/services/default-http-backend
Heapster is running at https://10.0.0.5/api/v1/proxy/namespaces/kube-system/services/heapster
KubeDNS is running at https://10.0.0.5/api/v1/proxy/namespaces/kube-system/services/kube-dns
kubernetes-dashboard is running at https://10.0.0.5/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Useful Commands

Exec a command

kubectl get pods \
  -l app=blog -o template \
  --template="{{range.items}}{{.metadata.name}}{{end}}" \
  | xargs -I{} kubectl exec {} --namespace default -i -t -- sh

Forward connections to pods

kubectl port-forward {pod} {port}

View the dashboard.

kubectl proxy

localhost:8001/ui

Create a persistent disk (for volumes)

gcloud compute disks create pv-gce-s1gd \
  --zone us-east1-b \
  --size 9GB \
  --type pd-standard

Resizing a cluster

gcloud compute instance-groups list
NAME                                              ZONE        NETWORK  MANAGED  INSTANCES
gke-kubernetes-cluster-default-pool-c5cd7fbc-grp  us-east1-b  default  Yes      1
gke-kubernetes-cluster-low-mem-f4ffb3e4-grp       us-east1-b  default  Yes      1
gcloud compute instance-groups managed resize gke-kubernetes-cluster-default-pool-c5cd7fbc-grp \
  --zoneus-east1-b \
  --size 3
gcloud compute instance-groups managed wait-until-stable gke-kubernetes-cluster-default-pool-c5cd7fbc-grp \
  --zone us-east1-b

Deleting the cluster

Cleanup resources.

kubectl delete jobs,petsets,deployments,daemonsets,replicationcontrollers,replicasets,pods,configmaps,secrets,services,thirdpartyresources \
  --namespace monitoring \
  --all

Delete the cluster.

gcloud container clusters delete kubernetes-cluster

Optionally, delete the project.

gcloud projects delete kubernetes-cluster-1345

Optional API's to enable

Google Cloud Logging API on https://console.cloud.google.com/flows/enableapi?apiid=logging.

Google Cloud Monitoring API on https://console.cloud.google.com/flows/enableapi?apiid=cloudmonitoring.

Google Cloud Container Builder API (Alpha) on https://console.cloud.google.com/flows/enableapi?apiid=cloudbuild.googleapis.com.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment