Skip to content

Instantly share code, notes, and snippets.

@chrislovecnm
Last active April 30, 2021 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chrislovecnm/de2c43f52197ae8f8700024305165fe1 to your computer and use it in GitHub Desktop.
Save chrislovecnm/de2c43f52197ae8f8700024305165fe1 to your computer and use it in GitHub Desktop.
GKE IP Aliasing Example

GKE IP Alaising Example

This is a demo of creating the subnets needed for a GKE Cluster

Create The Network

Create the base network for GKE.

gcloud compute networks create my-vpc-network \
  --subnet-mode=custom

Create the Subnets

Create the base subnet and the secondary range subnets. The secondary range subnets include ranges for the pods and the services.

gcloud compute networks subnets create my-cluster-nodes-subnet \
    --network my-vpc-network \
    --region us-central1 \
    --range 10.0.0.0/24 \
    --secondary-range my-cluster-pod-subnet=10.1.0.0/16,my-cluster-service-subnet=10.2.0.0/20

We are creating a subnet for the hosts called my-cluster-host-subnet. Within that subnet we are adding a secondary ranges for the pods and services; my-cluster-pod-subnet, my-cluster-service-subnet.

Create the Cluster

gcloud container clusters create my-cluster \
  --region us-central1 \
  --network=my-vpc-network \
  --enable-ip-alias \
  --subnetwork=my-cluster-nodes-subnet \
  --cluster-secondary-range-name=my-cluster-pod-subnet \
  --services-secondary-range-name=my-cluster-service-subnet \
  --num-nodes=1

The cluster will use the following subnets.

Table 1. Cluster Subnets
Type Subnet IP Range

Nodes

my-cluster-nodes-subnet

10.0.0.0/24

Pods

my-cluster-pod-subnet

10.1.0.0/16

Service

my-cluster-service-subnet

10.2.0.0/20

Alias

Because we are awesome lazy engineers.

alias k=kubectl

Deploy Pod and Service

k run hello-world --image=gcr.io/google-samples/hello-app:1.0 --port 8080
k expose deployment hello-world --type=LoadBalancer --name=hello-service

View IP Spaces

The following commands will show the different IP address that are being used.

Node IPs

This will display the node IP addresses.

k get no -o yaml | grep 10.0 | grep address

Pod IPs

This will display the IP Address of the running pod.

k get po -o wide

Service IP

This will display the IP Address of the running service.

k get svc

Cleanup

This sometimes is done automatically. But good hygiene denotes to do it.

k delete svc --all

Delete the cluster

gcloud container clusters delete my-cluster \
  --region us-central1

Delete the subnetwork

gcloud compute networks subnets delete my-cluster-nodes-subnet \
  --region us-central1

Finally remove the network.

gcloud compute networks delete my-vpc-network
@garzoglio
Copy link

Typo in the "Create the Subnets" section: the command creates subnet "my-cluster-nodes-subnet", but the text refers to it as "my-cluster-host-subnet"

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