Skip to content

Latest commit

 

History

History
242 lines (178 loc) · 9.39 KB

aws.md

File metadata and controls

242 lines (178 loc) · 9.39 KB

AWS

In this tutorial, we'll create a Kubernetes v1.31.3 cluster on AWS with Flatcar Linux.

We'll declare a Kubernetes cluster using the Typhoon Terraform module. Then apply the changes to create a VPC, gateway, subnets, security groups, controller instances, worker auto-scaling group, network load balancer, and TLS assets.

Controller hosts are provisioned to run an etcd-member peer and a kubelet service. Worker hosts run a kubelet service. Controller nodes run kube-apiserver, kube-scheduler, kube-controller-manager, and coredns, while kube-proxy and (flannel, calico, or cilium) run on every node. A generated kubeconfig provides kubectl access to the cluster.

Requirements

  • AWS Account and IAM credentials
  • AWS Route53 DNS Zone (registered Domain Name or delegated subdomain)
  • Terraform v0.13.0+

Terraform Setup

Install Terraform v0.13.0+ on your system.

$ terraform version
Terraform v1.0.0

Read concepts to learn about Terraform, modules, and organizing resources. Change to your infrastructure repository (e.g. infra).

cd infra/clusters

Provider

Login to your AWS IAM dashboard and find your IAM user. Select "Security Credentials" and create an access key. Save the id and secret to a file that can be referenced in configs.

[default]
aws_access_key_id = xxx
aws_secret_access_key = yyy

Configure the AWS provider to use your access key credentials in a providers.tf file.

provider "aws" {
  region                  = "eu-central-1"
  shared_credentials_file = "/home/user/.config/aws/credentials"
}

provider "ct" {}

terraform {
  required_providers {
    ct = {
      source  = "poseidon/ct"
      version = "0.11.0"
    }
    aws = {
      source = "hashicorp/aws"
      version = "4.61.0"
    }
  }
}

Additional configuration options are described in the aws provider docs.

!!! tip Regions are listed in docs or with aws ec2 describe-regions.

Cluster

Define a Kubernetes cluster using the module aws/flatcar-linux/kubernetes.

module "tempest" {
  source = "git::https://github.com/poseidon/typhoon//aws/flatcar-linux/kubernetes?ref=v1.31.3"

  # AWS
  cluster_name = "tempest"
  dns_zone     = "aws.example.com"
  dns_zone_id  = "Z3PAABBCFAKEC0"

  # instances
  worker_count = 2
  worker_type  = "t3.small"

  # configuration
  ssh_authorized_key = "ssh-rsa AAAAB3Nz..."
}

Reference the variables docs or the variables.tf source.

ssh-agent

Initial bootstrapping requires bootstrap.service be started on one controller node. Terraform uses ssh-agent to automate this step. Add your SSH private key to ssh-agent.

ssh-add ~/.ssh/id_rsa
ssh-add -L

Apply

Initialize the config directory if this is the first use with Terraform.

terraform init

Plan the resources to be created.

$ terraform plan
Plan: 109 to add, 0 to change, 0 to destroy.

Apply the changes to create the cluster.

$ terraform apply
...
module.tempest.null_resource.bootstrap: Still creating... (4m50s elapsed)
module.tempest.null_resource.bootstrap: Still creating... (5m0s elapsed)
module.tempest.null_resource.bootstrap: Creation complete after 11m8s (ID: 3961816482286168143)

Apply complete! Resources: 109 added, 0 changed, 0 destroyed.

In 4-8 minutes, the Kubernetes cluster will be ready.

Verify

Install kubectl on your system. Obtain the generated cluster kubeconfig from module outputs (e.g. write to a local file).

resource "local_file" "kubeconfig-tempest" {
  content         = module.tempest.kubeconfig-admin
  filename        = "/home/user/.kube/configs/tempest-config"
  file_permission = "0600"
}

List nodes in the cluster.

$ export KUBECONFIG=/home/user/.kube/configs/tempest-config
$ kubectl get nodes
NAME           STATUS  ROLES   AGE  VERSION
ip-10-0-3-155  Ready   <none>  10m  v1.31.3
ip-10-0-26-65  Ready   <none>  10m  v1.31.3
ip-10-0-41-21  Ready   <none>  10m  v1.31.3

List the pods.

$ kubectl get pods --all-namespaces
NAMESPACE     NAME                                      READY  STATUS    RESTARTS  AGE
kube-system   cilium-1m5bf                              1/1    Running   0         34m
kube-system   cilium-7jmr1                              1/1    Running   0         34m
kube-system   cilium-bknc8                              1/1    Running   0         34m
kube-system   coredns-1187388186-wx1lg                  1/1    Running   0         34m
kube-system   coredns-1187388186-qjnvp                  1/1    Running   0         34m
kube-system   kube-apiserver-ip-10-0-3-155              1/1    Running   0         34m
kube-system   kube-controller-manager-ip-10-0-3-155     1/1    Running   0         34m
kube-system   kube-proxy-14wxv                          1/1    Running   0         34m
kube-system   kube-proxy-9vxh2                          1/1    Running   0         34m
kube-system   kube-proxy-sbbsh                          1/1    Running   0         34m
kube-system   kube-scheduler-ip-10-0-3-155              1/1    Running   1         34m

Going Further

Learn about maintenance and addons.

Variables

Check the variables.tf source.

Required

Name Description Example
cluster_name Unique cluster name (prepended to dns_zone) "tempest"
dns_zone AWS Route53 DNS zone "aws.example.com"
dns_zone_id AWS Route53 DNS zone id "Z3PAABBCFAKEC0"
ssh_authorized_key SSH public key for user 'core' "ssh-rsa AAAAB3NZ..."

DNS Zone

Clusters create a DNS A record ${cluster_name}.${dns_zone} to resolve a network load balancer backed by controller instances. This FQDN is used by workers and kubectl to access the apiserver(s). In this example, the cluster's apiserver would be accessible at tempest.aws.example.com.

You'll need a registered domain name or delegated subdomain on AWS Route53. You can set this up once and create many clusters with unique names.

resource "aws_route53_zone" "zone-for-clusters" {
  name = "aws.example.com."
}

Reference the DNS zone id with aws_route53_zone.zone-for-clusters.zone_id.

!!! tip "" If you have an existing domain name with a zone file elsewhere, just delegate a subdomain that can be managed on Route53 (e.g. aws.mydomain.com) and update nameservers.

Optional

Name Description Default Example
os_image AMI channel for a Container Linux derivative "flatcar-stable" flatcar-stable, flatcar-beta, flatcar-alpha
controller_count Number of controllers (i.e. masters) 1 1
controller_type EC2 instance type for controllers "t3.small" See below
controller_disk_size Size of EBS volume in GB 30 100
controller_disk_type Type of EBS volume gp3 io1
controller_disk_iops IOPS of EBS volume 3000 4000
controller_cpu_credits Burstable CPU pricing model null (i.e. auto) standard, unlimited
worker_disk_size Size of EBS volume in GB 30 100
worker_disk_type Type of EBS volume gp3 io1
worker_disk_iops IOPS of EBS volume 3000 4000
worker_cpu_credits Burstable CPU pricing model null (i.e. auto) standard, unlimited
worker_price Spot price in USD for worker instances or 0 to use on-demand instances 0/null 0.10
worker_target_groups Target group ARNs to which worker instances should be added [] [aws_lb_target_group.app.id]
controller_snippets Controller Container Linux Config snippets [] example
worker_snippets Worker Container Linux Config snippets [] example
networking Choice of networking provider "cilium" "calico" or "cilium" or "flannel"
network_mtu CNI interface MTU (calico only) 1480 8981
host_cidr CIDR IPv4 range to assign to EC2 instances "10.0.0.0/16" "10.1.0.0/16"
pod_cidr CIDR IPv4 range to assign to Kubernetes pods "10.2.0.0/16" "10.22.0.0/16"
service_cidr CIDR IPv4 range to assign to Kubernetes services "10.3.0.0/16" "10.3.0.0/24"
worker_node_labels List of initial worker node labels [] ["worker-pool=default"]

Check the list of valid instance types.

!!! warning Do not choose a controller_type smaller than t3.small. Smaller instances are not sufficient for running a controller.

!!! tip "MTU" If your EC2 instance type supports Jumbo frames (most do), we recommend you change the network_mtu to 8981! You will get better pod-to-pod bandwidth.

Spot

Add worker_price = "0.10" to use spot instance workers (instead of "on-demand") and set a maximum spot price in USD. Clusters can tolerate spot market interuptions fairly well (reschedules pods, but cannot drain) to save money, with the tradeoff that requests for workers may go unfulfilled.