Provisioning Compute Resources

Kubernetes requires a set of machines to host the Kubernetes control plane and the worker nodes where containers are ultimately run. In this lab you will provision the compute resources required for running a secure and highly available Kubernetes cluster across a single compute zone.

Ensure a default compute zone and region have been set as described in the Prerequisites lab.

Networking

The Kubernetes networking model assumes a flat network in which containers and nodes can communicate with each other. In cases where this is not desired network policies can limit how groups of containers are allowed to communicate with each other and external network endpoints.

Setting up network policies is out of scope for this tutorial.

Virtual Private Cloud Network

In this section a dedicated Virtual Private Cloud (VPC) network will be setup to host the Kubernetes cluster.

Create the kubernetes-the-hard-way custom VPC network:

  1. gcloud compute networks create kubernetes-the-hard-way --subnet-mode custom

A subnet must be provisioned with an IP address range large enough to assign a private IP address to each node in the Kubernetes cluster.

Create the kubernetes subnet in the kubernetes-the-hard-way VPC network:

  1. gcloud compute networks subnets create kubernetes \
  2. --network kubernetes-the-hard-way \
  3. --range 10.240.0.0/24

The 10.240.0.0/24 IP address range can host up to 254 compute instances.

Firewall Rules

Create a firewall rule that allows internal communication across all protocols:

  1. gcloud compute firewall-rules create kubernetes-the-hard-way-allow-internal \
  2. --allow tcp,udp,icmp \
  3. --network kubernetes-the-hard-way \
  4. --source-ranges 10.240.0.0/24,10.200.0.0/16

Create a firewall rule that allows external SSH, ICMP, and HTTPS:

  1. gcloud compute firewall-rules create kubernetes-the-hard-way-allow-external \
  2. --allow tcp:22,tcp:6443,icmp \
  3. --network kubernetes-the-hard-way \
  4. --source-ranges 0.0.0.0/0

An external load balancer will be used to expose the Kubernetes API Servers to remote clients.

List the firewall rules in the kubernetes-the-hard-way VPC network:

  1. gcloud compute firewall-rules list --filter="network:kubernetes-the-hard-way"

output

  1. NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED
  2. kubernetes-the-hard-way-allow-external kubernetes-the-hard-way INGRESS 1000 tcp:22,tcp:6443,icmp False
  3. kubernetes-the-hard-way-allow-internal kubernetes-the-hard-way INGRESS 1000 tcp,udp,icmp Fals

Kubernetes Public IP Address

Allocate a static IP address that will be attached to the external load balancer fronting the Kubernetes API Servers:

  1. gcloud compute addresses create kubernetes-the-hard-way \
  2. --region $(gcloud config get-value compute/region)

Verify the kubernetes-the-hard-way static IP address was created in your default compute region:

  1. gcloud compute addresses list --filter="name=('kubernetes-the-hard-way')"

output

  1. NAME ADDRESS/RANGE TYPE PURPOSE NETWORK REGION SUBNET STATUS
  2. kubernetes-the-hard-way XX.XXX.XXX.XXX EXTERNAL us-west1 RESERVED

Compute Instances

The compute instances in this lab will be provisioned using Ubuntu Server 20.04, which has good support for the containerd container runtime. Each compute instance will be provisioned with a fixed private IP address to simplify the Kubernetes bootstrapping process.

Kubernetes Controllers

Create three compute instances which will host the Kubernetes control plane:

  1. for i in 0 1 2; do
  2. gcloud compute instances create controller-${i} \
  3. --async \
  4. --boot-disk-size 200GB \
  5. --can-ip-forward \
  6. --image-family ubuntu-2004-lts \
  7. --image-project ubuntu-os-cloud \
  8. --machine-type e2-standard-2 \
  9. --private-network-ip 10.240.0.1${i} \
  10. --scopes compute-rw,storage-ro,service-management,service-control,logging-write,monitoring \
  11. --subnet kubernetes \
  12. --tags kubernetes-the-hard-way,controller
  13. done

Kubernetes Workers

Each worker instance requires a pod subnet allocation from the Kubernetes cluster CIDR range. The pod subnet allocation will be used to configure container networking in a later exercise. The pod-cidr instance metadata will be used to expose pod subnet allocations to compute instances at runtime.

The Kubernetes cluster CIDR range is defined by the Controller Manager’s --cluster-cidr flag. In this tutorial the cluster CIDR range will be set to 10.200.0.0/16, which supports 254 subnets.

Create three compute instances which will host the Kubernetes worker nodes:

  1. for i in 0 1 2; do
  2. gcloud compute instances create worker-${i} \
  3. --async \
  4. --boot-disk-size 200GB \
  5. --can-ip-forward \
  6. --image-family ubuntu-2004-lts \
  7. --image-project ubuntu-os-cloud \
  8. --machine-type e2-standard-2 \
  9. --metadata pod-cidr=10.200.${i}.0/24 \
  10. --private-network-ip 10.240.0.2${i} \
  11. --scopes compute-rw,storage-ro,service-management,service-control,logging-write,monitoring \
  12. --subnet kubernetes \
  13. --tags kubernetes-the-hard-way,worker
  14. done

Verification

List the compute instances in your default compute zone:

  1. gcloud compute instances list --filter="tags.items=kubernetes-the-hard-way"

output

  1. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
  2. controller-0 us-west1-c e2-standard-2 10.240.0.10 XX.XX.XX.XXX RUNNING
  3. controller-1 us-west1-c e2-standard-2 10.240.0.11 XX.XXX.XXX.XX RUNNING
  4. controller-2 us-west1-c e2-standard-2 10.240.0.12 XX.XXX.XX.XXX RUNNING
  5. worker-0 us-west1-c e2-standard-2 10.240.0.20 XX.XX.XXX.XXX RUNNING
  6. worker-1 us-west1-c e2-standard-2 10.240.0.21 XX.XX.XX.XXX RUNNING
  7. worker-2 us-west1-c e2-standard-2 10.240.0.22 XX.XXX.XX.XX RUNNING

Configuring SSH Access

SSH will be used to configure the controller and worker instances. When connecting to compute instances for the first time SSH keys will be generated for you and stored in the project or instance metadata as described in the connecting to instances documentation.

Test SSH access to the controller-0 compute instances:

  1. gcloud compute ssh controller-0

If this is your first time connecting to a compute instance SSH keys will be generated for you. Enter a passphrase at the prompt to continue:

  1. WARNING: The public SSH key file for gcloud does not exist.
  2. WARNING: The private SSH key file for gcloud does not exist.
  3. WARNING: You do not have an SSH key for gcloud.
  4. WARNING: SSH keygen will be executed to generate a key.
  5. Generating public/private rsa key pair.
  6. Enter passphrase (empty for no passphrase):
  7. Enter same passphrase again:

At this point the generated SSH keys will be uploaded and stored in your project:

  1. Your identification has been saved in /home/$USER/.ssh/google_compute_engine.
  2. Your public key has been saved in /home/$USER/.ssh/google_compute_engine.pub.
  3. The key fingerprint is:
  4. SHA256:nz1i8jHmgQuGt+WscqP5SeIaSy5wyIJeL71MuV+QruE $USER@$HOSTNAME
  5. The key's randomart image is:
  6. +---[RSA 2048]----+
  7. | |
  8. | |
  9. | |
  10. | . |
  11. |o. oS |
  12. |=... .o .o o |
  13. |+.+ =+=.+.X o |
  14. |.+ ==O*B.B = . |
  15. | .+.=EB++ o |
  16. +----[SHA256]-----+
  17. Updating project ssh metadata...-Updated [https://www.googleapis.com/compute/v1/projects/$PROJECT_ID].
  18. Updating project ssh metadata...done.
  19. Waiting for SSH key to propagate.

After the SSH keys have been updated you’ll be logged into the controller-0 instance:

  1. Welcome to Ubuntu 20.04 LTS (GNU/Linux 5.4.0-1019-gcp x86_64)
  2. ...

Type exit at the prompt to exit the controller-0 compute instance:

  1. $USER@controller-0:~$ exit

output

  1. logout
  2. Connection to XX.XX.XX.XXX closed

Next: Provisioning a CA and Generating TLS Certificates