0% found this document useful (0 votes)
12 views

Week 1 Implementation Detailed Guide to Create AWS EKS Cluster and Cluster Setup Using eksctl and Bash Scripts

Uploaded by

demy2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Week 1 Implementation Detailed Guide to Create AWS EKS Cluster and Cluster Setup Using eksctl and Bash Scripts

Uploaded by

demy2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Detailed Guide to Create AWS EKS Cluster and Cluster Setup Using

eksctl and Bash Scripts

Step-by-Step Guide

1. Initial Setup

Step 1: Install Prerequisites

 AWS CLI: Ensure AWS CLI is installed and configured.


 eksctl: Install eksctl from
https://docs.aws.amazon.com/eks/latest/userguide/eksctl.html.
 kubectl: Install kubectl from https://kubernetes.io/docs/tasks/tools/install-kubectl/.

# AWS CLI
aws configure

# eksctl
curl --location "https://github.com/weaveworks/eksctl/releases/download/v0.27.0/eksctl_$
(uname -s)_amd64.tar.gz" | tar xz -C /tmp

sudo mv /tmp/eksctl /usr/local/bin

# kubectl
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s
https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/
kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

Step 2: Create the create_cluster.sh Script

#!/bin/bash

CLUSTER_NAME="my-eks-cluster"
REGION="us-west-2"
NODE_TYPE="t2.medium"
NODES=3
NODES_MIN=1
NODES_MAX=4

eksctl create cluster \


--name $CLUSTER_NAME \
--region $REGION \
--nodegroup-name standard-workers \
--node-type $NODE_TYPE \
--nodes $NODES \
--nodes-min $NODES_MIN \
--nodes-max $NODES_MAX \
--managed

Step 3: Run the Script

chmod +x create_cluster.sh
./create_cluster.sh

2. Roles, Permissions, and Policies

Step 4: Create IAM Roles and Policies

aws iam create-role --role-name eksRole --assume-role-policy-document file://trust-


policy.json
aws iam attach-role-policy --role-name eksRole --policy-arn
arn:aws:iam::aws:policy/AmazonEKSClusterPolicy
aws iam attach-role-policy --role-name eksRole --policy-arn
arn:aws:iam::aws:policy/AmazonEKSServicePolicy

trust-policy.json

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "eks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

Step 5: Attach IAM Policies to Node Groups


aws iam create-role --role-name eksNodeRole --assume-role-policy-document file://trust-policy-
node.json
aws iam attach-role-policy --role-name eksNodeRole --policy-arn
arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy
aws iam attach-role-policy --role-name eksNodeRole --policy-arn
arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly
aws iam attach-role-policy --role-name eksNodeRole --policy-arn
arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy

trust-policy-node.json

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

3. S3 Bucket with Persistent Storage Options

Step 6: Create S3 Bucket

aws s3api create-bucket --bucket my-eks-storage --region us-west-2 --create-bucket-


configuration LocationConstraint=us-west-2

Step 7: Create IAM Policy for S3 Access

aws iam create-policy --policy-name EKS-S3-Policy --policy-document file://s3-


policy.json

s3-policy.json

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::my-eks-storage",
"arn:aws:s3:::my-eks-storage/*"
]
}
]
}

Step 8: Attach S3 Policy to Node Role

aws iam attach-role-policy --role-name eksNodeRole --policy-arn


arn:aws:iam::aws:policy/YourCustomS3PolicyARN

Development Environment Setup

Step 9: Apply 6 Pods and Add Worker Node

Pod Deployment Example (pod_deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
name: ca-agency-service
spec:
replicas: 2
selector:
matchLabels:
app: ca-agency-service
template:
metadata:
labels:
app: ca-agency-service
spec:
containers:
- name: web
image: your-docker-image
resources:
requests:
cpu: "500m"
memory: "256Mi"
limits:
cpu: "1000m"
memory: "512Mi"
- name: worker
image: your-docker-image
resources:
requests:
cpu: "500m"
memory: "256Mi"
limits:
cpu: "1000m"
memory: "512Mi"

Apply Deployment
kubectl apply -f pod_deployment.yaml

Project Structure

aws-eks-setup/
├── scripts/
│ ├── create_cluster.sh
│ └── delete_cluster.sh
├── iam/
│ ├── trust-policy.json
│ ├── trust-policy-node.json
│ └── s3-policy.json
├── manifests/
│ ├── pod_deployment.yaml
│ └── service.yaml
└── README.md

Flowchart

1. Initialize Setup
o Install AWS CLI, eksctl, and kubectl
o Create create_cluster.sh script
o Run the script to create EKS cluster
2. Configure IAM Roles and Policies
o Create IAM roles for EKS and nodes
o Attach necessary policies
3. Create S3 Bucket
o Create S3 bucket for persistent storage
o Create and attach S3 policy to node role
4. Development Environment
o Create deployment YAML files
o Apply deployments to Kubernetes cluster

1. Development: Apply 6 Pods per Environment

- Create a deploy_pods.sh script:

#!/bin/bash

kubectl apply -f configs/pod_deployment.yaml

Create a pod_deployment.yaml configuration:

apiVersion: apps/v1
kind: Deployment
metadata:
name: ca-agency-service
spec:
replicas: 2
selector:
matchLabels:
app: ca-agency-service
template:
metadata:
labels:
app: ca-agency-service
spec:
containers:
- name: web
image: your-docker-image
- name: worker
image: your-docker-image

# Repeat similar block for other services

Run the script:

chmod +x scripts/deploy_pods.sh
./scripts/deploy_pods.sh

2. Staging (Red) Environment Setup

- Pod Deployment with Readiness and Liveness Probes


(staging_pod_deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
name: ca-agency-service
spec:
replicas: 2
selector:
matchLabels:
app: ca-agency-service
template:
metadata:
labels:
app: ca-agency-service
spec:
containers:
- name: web
image: your-docker-image
resources:
requests:
cpu: "500m"
memory: "256Mi"
limits:
cpu: "1000m"
memory: "512Mi"
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 5
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 5
- name: worker
image: your-docker-image
resources:
requests:
cpu: "500m"
memory: "256Mi"
limits:
cpu: "1000m"
memory: "512Mi"
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 5
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 5

Apply Staging Deployment

kubectl apply -f staging_pod_deployment.yaml


3. Production (Blue) Environment Setup

Step-by-Step Guide with Advanced Features

1. Horizontal Pod Autoscaler (hpa.yaml)

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: ca-agency-service
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: ca-agency-service
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50

Apply HPA

kubectl apply -f hpa.yaml

2. Load Balancer Service (service.yaml)

apiVersion: v1
kind: Service
metadata:
name: ca-agency-service
spec:
type: LoadBalancer
selector:
app: ca-agency-service
ports:
- protocol: TCP
port: 80
targetPort: 8080

Apply Service

kubectl apply -f service.yaml


3. Pod Deployment with Security and Self-Healing
(production_pod_deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
name: ca-agency-service
spec:
replicas: 2
selector:
matchLabels:
app: ca-agency-service
template:
metadata:
labels:
app: ca-agency-service
spec:
containers:
- name: web
image: your-docker-image
resources:
requests:
cpu: "500m"
memory: "256Mi"
limits:
cpu: "1000m"
memory: "512Mi"
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 5
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 5
- name: worker
image: your-docker-image
resources:
requests:
cpu: "500m"
memory: "256Mi"
limits:
cpu: "1000m"
memory: "512Mi"
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 5
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 5
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000

Apply Production Deployment

kubectl apply -f production_pod_deployment.yaml

Summary

 Initial Setup: Install AWS CLI, eksctl, and kubectl.


 Cluster Creation: Use eksctl to create EKS cluster.
 IAM Configuration: Create and attach necessary IAM roles and policies.
 S3 Configuration: Create S3 bucket and configure access.
 Development Environment: Deploy 6 pods with specified configurations.
 Staging Environment: Mirror development setup with added high-availability
features.
 Production Environment: Apply advanced features like auto-scaling, load
balancing, and security.

Flowchart

1. Setup and Configuration


o Install tools
o Create cluster with eksctl
o Configure IAM roles and policies
o Set up S3 bucket
2. Environment Deployment
o Development: Apply pods and services
o Staging: Add high-availability features and health checks
o Production: Implement auto-scaling, load balancing, self-healing, and
security

You might also like