2017-09-18 07:50:55 +02:00
# AWS
2024-08-17 17:05:54 +02:00
In this tutorial, we'll create a Kubernetes v1.31.0 cluster on AWS with Flatcar Linux.
2017-09-18 07:50:55 +02:00
2018-06-24 00:15:57 +02:00
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.
2017-09-18 07:50:55 +02:00
Introduce the component system for managing pre-installed addons
* Previously: Typhoon provisions clusters with kube-system components
like CoreDNS, kube-proxy, and a chosen CNI provider (among flannel,
Calico, or Cilium) pre-installed. This is convenient since clusters
come with "batteries included". But it also means upgrading these
components is generally done in lock-step, by upgrading to a new
Typhoon / Kubernetes release
* It can be valuable to manage these components with a separate
plan/apply process or through automations and deploy systems. For
example, this allows managing CoreDNS separately from the cluster's
lifecycle.
* These "components" will continue to be pre-installed by default,
but a new `components` variable allows them to be disabled and
managed as "addons", components you apply after cluster creation
and manage on a rolling basis. For some of these, we may provide
Terraform modules to aide in managing these components.
```
module "cluster" {
# defaults
components = {
enable = true
coredns = {
enable = true
}
kube_proxy = {
enable = true
}
# Only the CNI set in var.networking will be installed
flannel = {
enable = true
}
calico = {
enable = true
}
cilium = {
enable = true
}
}
}
```
An earlier variable `install_container_networking = true/false` has
been removed, since it can now be achieved with this more extensible
and general components mechanism by setting the chosen networking
provider enable field to false.
2024-05-19 00:05:33 +02:00
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.
2017-09-18 07:50:55 +02:00
## Requirements
* AWS Account and IAM credentials
* AWS Route53 DNS Zone (registered Domain Name or delegated subdomain)
2020-08-11 06:02:56 +02:00
* Terraform v0.13.0+
2017-09-18 07:50:55 +02:00
## Terraform Setup
2020-08-11 06:02:56 +02:00
Install [Terraform ](https://www.terraform.io/downloads.html ) v0.13.0+ on your system.
2017-09-18 07:50:55 +02:00
```sh
$ terraform version
2021-06-17 22:25:43 +02:00
Terraform v1.0.0
2017-09-18 07:50:55 +02:00
```
2018-10-07 19:57:16 +02:00
Read [concepts ](/architecture/concepts/ ) to learn about Terraform, modules, and organizing resources. Change to your infrastructure repository (e.g. `infra` ).
2017-09-18 07:50:55 +02:00
```
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.
```tf
provider "aws" {
region = "eu-central-1"
shared_credentials_file = "/home/user/.config/aws/credentials"
}
2018-01-12 15:56:08 +01:00
2020-08-11 06:02:56 +02:00
provider "ct" {}
terraform {
required_providers {
ct = {
source = "poseidon/ct"
2022-08-18 18:04:20 +02:00
version = "0.11.0"
2020-08-11 06:02:56 +02:00
}
aws = {
source = "hashicorp/aws"
2023-04-17 18:34:33 +02:00
version = "4.61.0"
2020-08-11 06:02:56 +02:00
}
}
2018-11-27 09:08:51 +01:00
}
2017-09-18 07:50:55 +02:00
```
Additional configuration options are described in the `aws` provider [docs ](https://www.terraform.io/docs/providers/aws/ ).
!!! tip
Regions are listed in [docs ](http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region ) or with `aws ec2 describe-regions` .
## Cluster
2020-10-21 07:47:19 +02:00
Define a Kubernetes cluster using the module `aws/flatcar-linux/kubernetes` .
2017-09-18 07:50:55 +02:00
```tf
2019-08-08 05:42:40 +02:00
module "tempest" {
2024-08-17 17:05:54 +02:00
source = "git::https://github.com/poseidon/typhoon//aws/flatcar-linux/kubernetes?ref=v1.31.0"
2017-09-18 07:50:55 +02:00
# AWS
2018-03-26 05:41:52 +02:00
cluster_name = "tempest"
dns_zone = "aws.example.com"
dns_zone_id = "Z3PAABBCFAKEC0"
2024-08-03 00:01:48 +02:00
# instances
2018-03-26 05:41:52 +02:00
worker_count = 2
2019-01-30 07:56:03 +01:00
worker_type = "t3.small"
2024-08-03 00:01:48 +02:00
# configuration
ssh_authorized_key = "ssh-rsa AAAAB3Nz..."
2017-09-18 07:50:55 +02:00
}
```
2020-10-21 07:47:19 +02:00
Reference the [variables docs ](#variables ) or the [variables.tf ](https://github.com/poseidon/typhoon/blob/master/aws/flatcar-linux/kubernetes/variables.tf ) source.
2017-09-18 07:50:55 +02:00
## ssh-agent
2019-09-05 07:20:36 +02:00
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` .
2017-09-18 07:50:55 +02:00
```sh
ssh-add ~/.ssh/id_rsa
ssh-add -L
```
## Apply
Initialize the config directory if this is the first use with Terraform.
```sh
terraform init
```
Plan the resources to be created.
```sh
$ terraform plan
2022-08-21 17:52:35 +02:00
Plan: 109 to add, 0 to change, 0 to destroy.
2017-09-18 07:50:55 +02:00
```
Apply the changes to create the cluster.
```sh
$ terraform apply
...
2019-12-06 07:56:42 +01:00
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)
2017-09-18 07:50:55 +02:00
2022-08-21 17:52:35 +02:00
Apply complete! Resources: 109 added, 0 changed, 0 destroyed.
2017-09-18 07:50:55 +02:00
```
2017-11-07 06:19:11 +01:00
In 4-8 minutes, the Kubernetes cluster will be ready.
2017-09-18 07:50:55 +02:00
## Verify
2019-12-06 07:56:42 +01:00
[Install kubectl ](https://kubernetes.io/docs/tasks/tools/install-kubectl/ ) on your system. Obtain the generated cluster `kubeconfig` from module outputs (e.g. write to a local file).
2017-09-18 07:50:55 +02:00
```
2019-12-06 07:56:42 +01:00
resource "local_file" "kubeconfig-tempest" {
content = module.tempest.kubeconfig-admin
filename = "/home/user/.kube/configs/tempest-config"
}
```
List nodes in the cluster.
```
$ export KUBECONFIG=/home/user/.kube/configs/tempest-config
2017-09-18 07:50:55 +02:00
$ kubectl get nodes
2019-09-23 02:37:23 +02:00
NAME STATUS ROLES AGE VERSION
2024-08-17 17:05:54 +02:00
ip-10-0-3-155 Ready < none > 10m v1.31.0
ip-10-0-26-65 Ready < none > 10m v1.31.0
ip-10-0-41-21 Ready < none > 10m v1.31.0
2017-09-18 07:50:55 +02:00
```
List the pods.
```
$ kubectl get pods --all-namespaces
2020-02-13 23:39:48 +01:00
NAMESPACE NAME READY STATUS RESTARTS AGE
2024-08-03 00:01:48 +02:00
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
2020-02-13 23:39:48 +01:00
kube-system coredns-1187388186-wx1lg 1/1 Running 0 34m
2018-11-03 23:04:08 +01:00
kube-system coredns-1187388186-qjnvp 1/1 Running 0 34m
2020-02-13 23:39:48 +01:00
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
2017-09-18 07:50:55 +02:00
```
## Going Further
2019-01-03 08:33:02 +01:00
Learn about [maintenance ](/topics/maintenance/ ) and [addons ](/addons/overview/ ).
2017-09-18 07:50:55 +02:00
## Variables
2020-10-21 07:47:19 +02:00
Check the [variables.tf ](https://github.com/poseidon/typhoon/blob/master/aws/flatcar-linux/kubernetes/variables.tf ) source.
2018-04-24 06:36:20 +02:00
2017-09-18 07:50:55 +02:00
### 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" |
2018-04-24 06:36:20 +02:00
| ssh_authorized_key | SSH public key for user 'core' | "ssh-rsa AAAAB3NZ..." |
2017-09-18 07:50:55 +02:00
#### DNS Zone
2018-04-24 06:36:20 +02:00
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` .
2017-09-18 07:50:55 +02:00
2018-04-24 06:36:20 +02:00
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.
2017-09-18 07:50:55 +02:00
```tf
resource "aws_route53_zone" "zone-for-clusters" {
name = "aws.example.com."
}
```
2019-09-29 20:14:36 +02:00
Reference the DNS zone id with `aws_route53_zone.zone-for-clusters.zone_id` .
2017-09-18 07:50:55 +02:00
!!! tip ""
2018-04-24 06:36:20 +02:00
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 ](http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/SOA-NSrecords.html ).
2017-09-18 07:50:55 +02:00
### Optional
| Name | Description | Default | Example |
|:-----|:------------|:--------|:--------|
2024-08-03 00:01:48 +02:00
| os_image | AMI channel for a Container Linux derivative | "flatcar-stable" | flatcar-stable, flatcar-beta, flatcar-alpha |
2017-09-18 07:50:55 +02:00
| controller_count | Number of controllers (i.e. masters) | 1 | 1 |
2018-12-18 21:29:27 +01:00
| controller_type | EC2 instance type for controllers | "t3.small" | See below |
2024-08-03 00:01:48 +02:00
| 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 |
2019-09-29 20:14:36 +02:00
| worker_price | Spot price in USD for worker instances or 0 to use on-demand instances | 0/null | 0.10 |
2024-08-03 00:01:48 +02:00
| worker_target_groups | Target group ARNs to which worker instances should be added | [] | [aws_lb_target_group.app.id] |
2020-03-29 20:46:22 +02:00
| controller_snippets | Controller Container Linux Config snippets | [] | [example ](/advanced/customization/ ) |
| worker_snippets | Worker Container Linux Config snippets | [] | [example ](/advanced/customization/ ) |
2022-02-07 16:44:09 +01:00
| networking | Choice of networking provider | "cilium" | "calico" or "cilium" or "flannel" |
2017-09-18 07:50:55 +02:00
| network_mtu | CNI interface MTU (calico only) | 1480 | 8981 |
2018-03-26 06:36:10 +02:00
| 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" |
2019-09-29 20:14:36 +02:00
| worker_node_labels | List of initial worker node labels | [] | ["worker-pool=default"] |
2017-09-18 07:50:55 +02:00
Check the list of valid [instance types ](https://aws.amazon.com/ec2/instance-types/ ).
2018-05-31 07:57:15 +02:00
!!! warning
2024-08-03 00:01:48 +02:00
Do not choose a `controller_type` smaller than `t3.small` . Smaller instances are not sufficient for running a controller.
2018-05-31 07:57:15 +02:00
2017-09-18 07:50:55 +02:00
!!! tip "MTU"
2018-06-12 03:11:50 +02:00
If your EC2 instance type supports [Jumbo frames ](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/network_mtu.html#jumbo_frame_instances ) (most do), we recommend you change the `network_mtu` to 8981! You will get better pod-to-pod bandwidth.
2018-11-27 07:40:56 +01:00
#### 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.