In the rapidly evolving world of cloud computing and container orchestration, Kubernetes has become the go-to solution for managing containerized applications at scale. Combining it with Infrastructure as Code (IaC) tools like Terraform and Ansible can streamline the setup and management of your development environments. This guide will walk you through setting up a Kubernetes development environment on AWS using IaC.

Setting Up an AWS Account

Before diving into Kubernetes and IaC, you need an AWS account. If you don’t have one, follow these steps:

  1. Sign Up for AWS: Visit the AWS Management Console and create an account. You’ll need to provide billing information, but AWS offers a free tier that covers many services.
  2. Create an IAM User: To avoid using root account credentials, create an IAM user with admin permissions.
    • Go to the IAM section in the AWS Console.
    • Click on “Users” > “Add user”.
    • Assign programmatic access and attach the “AdministratorAccess” policy.
    • Save the access and secret keys; you’ll need these to configure your development environment.
  1. Configure AWS CLI: Install and configure the AWS CLI on your local machine.

    aws configure

Enter your access key, secret key, default region, and output format.

Configuring the Local Development Environment

Next, set up your local machine with the necessary tools:

  1. Install Terraform: Terraform is used to provision infrastructure.
    • Download Terraform from the official site.
    • Install it by following the platform-specific instructions.
  2. Install Ansible: Ansible is used for configuration management and application deployment.

Install Ansible using pip:

pip install ansible

  1. Install kubectl: The Kubernetes command-line tool to interact with your cluster.
    • Download kubectl from the official Kubernetes site.
  2. Install eksctl: A CLI tool for creating and managing Kubernetes clusters on AWS.
    • Install eksctl following the instructions on the official site.

Creating the Kubernetes Development Environment with Terraform and Ansible

Now that your local environment is set up, let’s create the Kubernetes development environment.

  1. Define the Infrastructure with Terraform:
    • Create a directory for your Terraform files and navigate into it.

Create a main.tf file to define your infrastructure:

provider “aws” {

  region = “us-west-2”

}

resource “aws_vpc” “main” {

  cidr_block = “10.0.0.0/16”

}

resource “aws_subnet” “subnet” {

  vpc_id     = aws_vpc.main.id

  cidr_block = “10.0.1.0/24”

}

resource “aws_eks_cluster” “k8s” {

  name     = “k8s-cluster”

  role_arn = aws_iam_role.eks_cluster.arn

  vpc_config {

    subnet_ids = [aws_subnet.subnet.id]

  }

}

  1. Initialize and Apply Terraform Configuration:

Initialize the configuration:

terraform init

Apply the configuration:

terraform apply

Confirm the action when prompted.

  1. Configure Kubernetes with Ansible:

Create an Ansible playbook k8s-setup.yml:

– hosts: localhost

  tasks:

    – name: Configure kubectl

      command: eksctl create cluster –name k8s-cluster –region us-west-2

Run the playbook:

ansible-playbook k8s-setup.yml

Destroying the Kubernetes Development Environment

When you’re done with the development environment, cleaning up the resources is crucial to avoid unnecessary costs.

  1. Destroy Resources with Terraform:

Navigate to your Terraform directory and run:

terraform destroy

Confirm the action when prompted.

  1. Clean Up with Ansible:

Modify the Ansible playbook to delete the cluster:

– hosts: localhost

  tasks:

    – name: Delete kubectl cluster

      command: eksctl delete cluster –name k8s-cluster –region us-west-2

Run the modified playbook:

ansible-playbook k8s-setup.yml

Following these steps, you can efficiently set up, manage, and tear down a Kubernetes development environment on AWS using Terraform and Ansible.

References

Infrastructure as code

Building an end-to-end Kubernetes-based DevSecOps software factory on AWS