Skip to main content

Launching Kubernetes on AWS (EKS)

Prerequisites

⚙️ Ensure your environment is ready! Before setting up Amazon EKS, confirm that you have the following:
  • AWS Account with appropriate permissions.
  • AWS CLI installed and configured.
  • kubectl installed.
  • eksctl installed.

Setting up Amazon EKS

1. Install Required Tools

# Install AWS CLI
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
 
# Install eksctl
curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
sudo mv /tmp/eksctl /usr/local/bin 
 
# Install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" 
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
💡 Why install these tools? AWS CLI helps manage AWS resources, kubectl is for Kubernetes management, and eksctl simplifies EKS cluster setup.

2. Create an EKS Cluster

eksctl create cluster \
    --name my-eks-cluster \
    --region us-west-2 \
    --node-type t3.medium \
    --nodes 2 \
    --nodes-min 1 \
    --nodes-max 3 \
    --managed
⚠️ Resource Costs Remember that running a cluster incurs AWS charges. Be sure to monitor your usage to avoid unexpected bills.

3. Configure kubectl

aws eks update-kubeconfig --name my-eks-cluster --region us-west-2
🛠️ Validate Configuration Run kubectl get nodes to confirm your cluster is successfully set up.

Cluster Management

Scaling the Cluster

# Scale the node group
eksctl scale nodegroup --cluster=my-eks-cluster --name=ng-1 --nodes=3

# Auto-scaling configuration
eksctl create nodegroup \
    --cluster my-eks-cluster \
    --region us-west-2 \
    --name ng-mixed \
    --node-type t3.medium \
    --nodes 2 \
    --nodes-min 1 \
    --nodes-max 5 \
    --asg-access
📈 Why Scale? Scaling ensures your cluster meets changing workloads efficiently.

Deploying Applications

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: sample-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
      - name: sample-app
        image: nginx:latest
        ports:
        - containerPort: 80
💡 Deploying Applications Use deployments to ensure your applications are scalable and resilient.

Setting up Load Balancing

apiVersion: v1
kind: Service
metadata:
  name: sample-app-service
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: nlb
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    app: sample-app
⚠️ Networking Costs Load balancers incur costs. Use them wisely to optimize expenses.

Monitoring and Logging

Installing Metrics Server

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

Setting up CloudWatch Logging

apiVersion: v1
kind: Namespace
metadata:
  name: amazon-cloudwatch
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: cloudwatch-agent
  namespace: amazon-cloudwatch
spec:
  selector:
    matchLabels:
      name: cloudwatch-agent
  template:
    metadata:
      labels:
        name: cloudwatch-agent
    spec:
      containers:
        - name: cloudwatch-agent
          image: amazon/cloudwatch-agent:latest

Security Best Practices

Network Policies

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress
🔒 Secure Your Cluster Network policies prevent unauthorized communication between pods.

RBAC Configuration

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: pod-reader
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]

Cost Optimization

Resource Quotas

apiVersion: v1
kind: ResourceQuota
metadata:
  name: compute-resources
spec:
  hard:
    requests.cpu: "4"
    requests.memory: 8Gi
    limits.cpu: "8"
    limits.memory: 16Gi
💡 Why Resource Quotas? They ensure efficient resource usage and cost control.

Cleanup

Delete the Cluster

eksctl delete cluster --name my-eks-cluster --region us-west-2
⚠️ Data Loss Alert Deleting the cluster will remove all associated resources. Backup data if necessary.

Troubleshooting

Common Issues and Solutions

  1. Node Group Issues
# Check node status
kubectl get nodes
kubectl describe node <node-name>

# Check node group health
eksctl get nodegroup --cluster my-eks-cluster
  1. Pod Issues
# Check pod status
kubectl get pods
kubectl describe pod <pod-name>
kubectl logs <pod-name>
  1. Networking Issues
# Check service status
kubectl get svc
kubectl describe svc <service-name>

# Check DNS resolution
kubectl run test-dns --image=busybox:1.28 -- nslookup kubernetes.default
🛠️ Troubleshooting Tips Most issues can be resolved by checking logs and validating configurations.

Additional Resources

📧 Contact

For questions or feedback, reach out: 📨 Email: [email protected] 🌐 Portfolio: Brian Kimemia GitHub: BrianKN019
Thank you for exploring this project! Let’s innovate and build secure AWS solutions together. 🚀