Skip to content

Instantly share code, notes, and snippets.

@manuelbcd
Last active June 15, 2021 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelbcd/b82ee991dd1027f0ec1dfa6cd83b84a1 to your computer and use it in GitHub Desktop.
Save manuelbcd/b82ee991dd1027f0ec1dfa6cd83b84a1 to your computer and use it in GitHub Desktop.
From https://sloopstash.com/blog/building-kubernetes-cluster-on-aws-cloud-using-terraform-and-ansible.html
Updates and corrections by manuelbcd
## Requirements
1. AWS account and IAM user credentials with enough permissions (If is your first time set admin rights)
2. Amazon EC2 key pair (i.e. "ec2k8s")
3. Linux Ubuntu 20.04 LTS operating system preferred
## AWS CLI
Install and configure AWS CLI
$ aws configure --profile tutorial
## Terraform
Install and configure Terraform (please note is old 0.14.4. I suggest using tfswitch)
# Download Terraform.
$ wget https://releases.hashicorp.com/terraform/0.14.4/terraform_0.14.4_linux_amd64.zip
# Extract Terraform from archive.
$ unzip terraform_0.14.4_linux_amd64.zip
# Install Terraform.
$ sudo mv terraform /usr/local/bin
# Check Terraform version.
$ terraform --version
Kubernetes starter kit
# Download Kubernetes starter-kit from GitHub to local filesystem path.
$ wget https://github.com/sloopstash/kickstart-kubernetes/archive/v1.2.1.zip
# Extract Kubernetes starter-kit from archive.
$ unzip v1.2.1.zip -d /opt/kickstart-kubernetes
# Change ownership of Kubernetes starter-kit directory.
$ sudo chown -R $USER:$USER /opt/kickstart-kubernetes
Changes.
- Change AMI from modules/aws to the following one "ami-0a199531cc0c22cef" (modern and optimized K8s AMI)
Provisioning AWS resources
# Switch to Kubernetes starter-kit directory.
$ cd /opt/kickstart-kubernetes/terraform
# Initialize Terraform configuration.
$ terraform init
# Store variables of Terraform configuration as environment variables.
$ export TF_VAR_env=STG
$ export TF_VAR_stg_vpc_cidr_blk=10.2.0.0/16
$ export TF_VAR_stg_ec2_key_pair=ec2-user
# Generate plan using Terraform configuration.
$ terraform plan -out stg-tfm-base-cfg.tfplan
# Apply plan generated by Terraform configuration.
$ terraform apply stg-tfm-base-cfg.tfplan
## Ansible-managed Kubernetes cluster deployment on Amazon EC2 instances
Install Ansible
# Install required system packages.
$ sudo apt install python-pip
# Install Ansible package from repository.
$ sudo pip install ansible==2.8.2
# Check Ansible version.
$ ansible --version
Deploy Kubernetes using Ansible
# Switch to Kubernetes starter-kit directory.
$ cd /opt/kickstart-kubernetes/ansible
# Update hosts in Ansible inventory with public IPs of Amazon EC2 instances.
$ vim inventory/stg/hosts
# Copy Amazon EC2 SSH private key to authenticate SSH server running on Ansible nodes.
$ cp ec2-user.pem secret/node.pem
# Trigger first time deployment on Kubernetes Ansible nodes.
$ ansible-playbook playbook/kubernetes.yml -i inventory/stg --tags "setup, configure, start"
Now we can SSH to provisioned resources
# SSH to master Kubernetes node.
$ ssh ec2-user@<MASTER_K8S_NODE_IP>
# List Kubernetes nodes.
$ kubectl get nodes -o wide
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment