GithubHelp home page GithubHelp logo

odennav / terraform-kubernetes-aws-ec2 Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 2.0 3.36 MB

Create a Virtual Private Cloud with Terraform on AWS and deploy a Kubernetes cluster

License: Apache License 2.0

Shell 34.47% HCL 65.53%
aws aws-ec2 aws-vpc aws-vpc-subnet kubernetes kubernetes-cluster kubernetes-deployment terraform ansible database kubespray nat-gateway automation internet-gateway private-subnets public-subnets security-policies database-subnets

terraform-kubernetes-aws-ec2's Introduction

aws Deploy AWS VPC with Terraform & Kubernetes in 3-Tier Arch k8s

This project deploys a 3-Tier Architecture on AWS using Terraform, creating a VPC with private, public,database subnets. Bastion, private and database EC2 instances created for VPC.

NAT gateway created for private EC2 instances to communicate with the internet and elastic IPs are assigned for NAT gateways. No routes created from NAT gateway to database instances.

Bash scripts used to automate deployment of kubernetes cluster to private EC2 instances with kubespray.

Requirements

  • Install Terraform
  • Install AWS CLI
  • Generate key pair for connection to EC2 instances in AWS console. Name it 'terraform-key'. Choose 'RSA' key pair type and use .pem key file format.
  • Minimum required version of Kubernetes is v1.27
  • Ansible v2.14+, Jinja 2.11+ and python-netaddr is installed on the machine that will run Ansible commands.
  • The target servers must have access to the Internet in order to pull docker images. Otherwise, additional configuration is required.
  • The target servers are configured to allow IPv4 forwarding.
  • If using IPv6 for pods and services, the target servers are configured to allow IPv6 forwarding.
  • The firewalls are not managed, you'll need to implement your own rules the way you used to. in order to avoid any issue during deployment you should disable your firewall.
  • If kubespray is run from non-root user account, correct privilege escalation method should be configured in the target servers. Then the ansible_become flag or command parameters --become or -b should be specified.

Getting Started

  1. Clone this Repo to Local machine

    cd /
    git clone [email protected]:odennav/terraform-kubernetes-aws-ec2.git
    cd terraform-kubernetes-aws-ec2/terraform-manifest
  2. Execute these Terraform commands sequentially on your Local machine to create the AWS infrastructure.

    Initializes terraform working directory

    terraform init

    Validate the syntax of the terraform configuration files

    terraform validate

    Create an execution plan that describes the changes terraform will make to the infrastructure

    terraform plan

    Apply the changes described in execution plan

    terraform apply -auto-approve

Check AWS console for instances created and running

ec2

  1. SSH access and New user setup

    Use .pem key from AWS to SSH into the public EC2 instance. IPv4 address of public EC2 instance will be shown in terraform outputs.

    ssh -i private-key/terraform-key.pem ec2-user@<ipaddress>

    Its possible to use public EC2 instance as a jumpbox to securely SSH into private EC2 instances within the VPC.

    Change root password(First-Login to control-dev)

    sudo passwd

    Switch to root user. Add new user to sudo group. In this case new user is 'odennav-admin'

    sudo adduser odennav-admin
    sudo usermod -aG sudo odennav-admin

    You'll be prompted to set a password and provide additional information about the new
    user, such as full name, work phone, etc. This information is optional. Press 'Enter'
    to skip each prompt.

    Test sudo privileges by switching to new user
    su - odennav-admin
    sudo ls /root

    You'll notice prompt to enter your user password. To disable this prompt for every sudo command, implement the following:

    Add sudoers file for odennav-admin

    cd /etc/sudoers.d/
    echo "odennav-admin ALL=(ALL) NOPASSWD: ALL" > odennav-admin

    Set permissions for sudoers file

    chmod 0440 odennav-admin

    Update yum package manager

    cd /
    sudo yum update -y
    sudo yum upgrade -y

    Confirm Git was installed by terraform

    git --version

    Confirm terraform-key was transferred to public EC2 instance by null provisioner

    Please note if .pem key not found, copy it manually. Also key can be copied to another folder because it will be deleted if node is restarted or shutdown

    ls -la /tmp/terraform-key.pem
    cp /tmp/terraform-key.pem /

    Change permissions of terraform-key.pem file

    SSH test will fail if permissions of .pem key are not secure enough

    chmod 400 /tmp/terraform-key.pem
  2. Clone this Repository to control-dev node

    cd /
    git clone [email protected]:odennav/terraform-kubernetes-aws-ec2.git
    git clone [email protected]:kubernetes-sigs/kubespray.git
  3. Copy IPv4 adresses of private EC2 instances deployed by Terraform

    Check IPv4 addresses in inventory file and input them in bash-scripts/ipaddr-list.txt

    Don't change format seen in .txt file, ip addresses will be read by bash scripts.

    For security reasons, don't share your private ips.

  4. Install Yum and Python utilities

    Updating Yum, installing necessary dependencies, and ensuring Python compatibility.

    chmod 770 dependencies-install
    ./dependencies-install
  5. Setup nodes for Kubernetes cluster

    chmod 770 kubespray-deploy.sh
    ./kubespray-env-build.sh

    This bash script copies SSH keys to private ec2 instances and updates Ansible inventory. Host inventory file edited and kubectl installed.

    Finaly, change directory to your local kubespray repo and execute cluster playbook to deploy kubernetes cluster.

    cd /kubespray
    ansible-playbook -i inventory/mycluster/hosts.yaml --become --become-user=root cluster.yml

Destroying Resources(Optional)

To tear down the infrastructure created by Terraform. Reduce costs incurred from AWS due to creation of resources.

terraform destroy

Enjoy!

terraform-kubernetes-aws-ec2's People

Contributors

odennav avatar

Stargazers

 avatar DevOps  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.