This guide provides step-by-step instructions to set up an AWS infrastructure using Terraform, including a VPC, public and private subnets, a Bastion host, security groups, and EC2 instances for Kubernetes.
- Install Terraform
Download and install Terraform. - Install AWS CLI
Download and install AWS CLI. - Set Up SSH Keys
Ensure you have an SSH key pair for accessing the Bastion host. The public key will be used in Terraform.
Make your AWS credentials available to Terraform by exporting them as environment variables.
export AWS_ACCESS_KEY_ID="your-access-key-id"
export AWS_SECRET_ACCESS_KEY="your-secret-access-key"
export AWS_DEFAULT_REGION="your-region"
Alternatively, you can configure credentials using the AWS CLI:
aws configure
Clone this repository to your local machine.
git clone https://github.com/joselrnz/k8s-cluster-setup.git
cd k8s-cluster-setup
Edit the terraform.tfvars
file to configure your infrastructure.
vpc_cidr = "10.0.0.0/16"
public_subnet_cidr = "10.0.1.0/24"
private_subnet_cidr = "10.0.2.0/24"
ami_id = "ami-04b4f1a9cf54c11d0"
bastion_instance_type = "t2.micro"
ec2_instance_type = "t2.medium"
key_name = "your-ssh-key-name"
my_ip = "your-public-ip/32" # Replace with your actual IP
Run the following command to initialize the Terraform configuration.
terraform init
Ensure your configuration is valid.
terraform validate
Review the changes Terraform will make to your AWS infrastructure.
terraform plan
Deploy the infrastructure to AWS.
terraform apply
Type yes
to confirm the deployment.
Once the infrastructure is deployed, you can connect to the Bastion host using SSH.
ssh -i ~/.ssh/your-private-key.pem ec2-user@<bastion-public-ip>
-
VPC
- CIDR:
10.0.0.0/16
- CIDR:
-
Public Subnet
- CIDR:
10.0.1.0/24
- CIDR:
-
Private Subnet
- CIDR:
10.0.2.0/24
- CIDR:
-
Route Tables
- Public Route Table
- Routes:
Destination CIDR Target Description 10.0.0.0/16 local Routes traffic within the VPC 0.0.0.0/0 Internet GW Routes traffic to the internet gateway for public access
- Routes:
- Private Route Table
- Routes:
Destination CIDR Target Description 10.0.0.0/16 local Routes traffic within the VPC 0.0.0.0/0 NAT Gateway Routes outbound traffic via NAT Gateway
- Routes:
- Public Route Table
-
Bastion Host
- Public-facing EC2 instance for secure SSH access.
-
EC2 Instances
- One Master Node
- Two Worker Nodes
-
Security Groups
- Bastion: Allows SSH access from your IP.
- Control Plane: Allows Kubernetes API and related traffic.
- Worker Nodes: Allows Kubernetes-related traffic.
Protocol | Direction | Port Range | Source | Purpose |
---|---|---|---|---|
TCP | Inbound | 22 | Your IP (/32 ) |
SSH access |
Protocol | Direction | Port Range | Source | Purpose |
---|---|---|---|---|
TCP | Inbound | 6443 | 0.0.0.0/0 | Kubernetes API server |
TCP | Inbound | 2379-2380 | 0.0.0.0/0 | etcd server client API |
TCP | Inbound | 10250 | 0.0.0.0/0 | Kubelet API |
TCP | Inbound | 10259 | 0.0.0.0/0 | kube-scheduler |
TCP | Inbound | 10257 | 0.0.0.0/0 | kube-controller-manager |
Protocol | Direction | Port Range | Source | Purpose |
---|---|---|---|---|
TCP | Inbound | 10250 | 0.0.0.0/0 | Kubelet API |
TCP | Inbound | 10256 | 0.0.0.0/0 | kube-proxy |
TCP | Inbound | 30000-32767 | 0.0.0.0/0 | NodePort Services |