
Kubernetes (K8s) is the industry-standard container orchestration platform, widely used for managing containerized applications at scale. If you’re new to Kubernetes and want to get hands-on experience, this tutorial will guide you through setting up a local environment and deploying an application on Amazon EKS (Elastic Kubernetes Service) using Terraform and kubectl.
Prerequisites
Before we begin, ensure you have the following installed on your local machine:
- AWS CLI
- Terraform
- kubectl
- An AWS account with IAM access to create EKS clusters
Step 1: Configuring AWS Credentials and Profiles
First, set up AWS credentials using the AWS CLI:
aws configure --profile my-profile
Enter your AWS Access Key ID, Secret Access Key, Region, and default output format.
To verify that your profile is set up correctly, run:
aws sts get-caller-identity --profile my-profile
Step 2: Deploying an Amazon EKS Cluster Using Terraform
We will use Terraform to provision an EKS cluster. Create a new directory and initialize a Terraform project:
mkdir k8s-eks-setup && cd k8s-eks-setup
Create a main.tf file with the following Terraform configuration:
provider "aws" {
region = "us-west-2"
profile = "my-profile"
}
resource "aws_eks_cluster" "my_cluster" {
name = "my-eks-cluster"
role_arn = aws_iam_role.eks_role.arn
vpc_config {
subnet_ids = [aws_subnet.public1.id, aws_subnet.public2.id]
}
}
Run the following commands to deploy the cluster:
terraform init
terraform apply -auto-approve
Once complete, retrieve the cluster details:
aws eks --region us-west-2 update-kubeconfig --name my-eks-cluster --profile my-profile
This sets up kubectl to communicate with your EKS cluster.
Step 3: Deploying a Basic Application on Kubernetes
Now that we have a working Kubernetes cluster, let’s deploy a simple nginx application.
Create a file named deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Deploy it using:
kubectl apply -f deployment.yaml
Check if the pods are running:
kubectl get pods
Step 4: Exposing the Application with a Load Balancer
To make the application accessible externally, create a service.yaml file:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: LoadBalancer
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
Apply the service:
kubectl apply -f service.yaml
Retrieve the external LoadBalancer URL:
kubectl get services
Once the EXTERNAL-IP is available, open it in a browser to see the running nginx app!
Step 5: Basic Kubernetes Commands
Here are some essential kubectl commands to interact with your cluster:
- List all nodes:
kubectl get nodes
- List all pods:
kubectl get pods
- Get detailed pod info:
kubectl describe pod <pod-name>
- Check logs of a pod:
kubectl logs <pod-name>
- Execute a command inside a running pod:
kubectl exec -it <pod-name> -- /bin/sh
- Scale a deployment:
kubectl scale deployment nginx-deployment --replicas=4
- Delete a deployment:
kubectl delete deployment nginx-deployment
- Check cluster events:
kubectl get events
- Get cluster configuration details:
kubectl cluster-info
- Delete a deployment:
kubectl delete deployment nginx-deployment
Conclusion
Congratulations! You’ve successfully set up a local environment, deployed an Amazon EKS cluster with Terraform, and launched an nginx application using Kubernetes.
This is just the beginning — stay tuned for the next tutorial, where we’ll dive deeper into Kubernetes networking, autoscaling, and monitoring.
📢 Have questions or feedback? Drop a comment below or connect with me on Twitter/X@spysood!
Originally published on Medium.