AWS Elastic Kubernetes Service (EKS)

ยท

5 min read

AWS Elastic Kubernetes Service (EKS)

Introduction

Kubernetes has become the de facto standard for container orchestration allowing developers to manage, scale and deploy containerized applications seamlessly. AWS Elastic Kubernetes Service (EKS) simplifies the process of running Kubernetes on AWS by managing the complexity of the control plane and integrating with various AWS services. In this blog we'll cover the basics of Kubernetes with AWS EKS, setting up a Kubernetes cluster and deploying containerized applications.

What is AWS EKS?

AWS EKS is a managed Kubernetes service that makes it easy to run Kubernetes on AWS without needing to install and operate your own Kubernetes control plane or nodes. With EKS AWS handles the control plane, providing high availability and automatic scaling. This allows you to focus on deploying and managing your applications.

Key Features of AWS EKS

  1. Managed Control Plane : AWS manages the Kubernetes control plane ensuring high availability and security.

  2. Integration with AWS Services : Seamless integration with other AWS services such as IAM, CloudWatch and ELB.

  3. High Availability and Scalability : EKS automatically scales the control plane and supports multi-AZ deployments.

  4. Security : Integrated with AWS IAM for authentication and authorization and supports Kubernetes RBAC.

Setting Up a Kubernetes Cluster with EKS

Prerequisites

  1. AWS CLI : Install and configure the AWS CLI.

  2. kubectl : Install the Kubernetes command-line tool, kubectl .

  3. eksctl : Install eksctl , a CLI tool for creating and managing EKS clusters.

Step 1 :- Create an EKS Cluster

Using eksctl you can create a new EKS cluster with a single command.

eksctl create cluster --name my-eks-cluster --region us-west-2 --nodes 3
  • --name : The name of the EKS cluster.

  • --region : The AWS region where the cluster will be created.

  • --nodes : The number of worker nodes to create.

This command creates an EKS cluster with a managed control plane and three worker nodes.

Step 2 :- Configure kubectl

To interact with your EKS cluster you need to configure kubectl.

aws eks --region us-west-2 update-kubeconfig --name my-eks-cluster

This command configures kubectl to use the context of your new EKS cluster.

Deploying Containerized Applications

Step 1 :- Create a Kubernetes Deployment

A Kubernetes Deployment manages a set of identical pods ensuring that the specified number of replicas are running.

Create a file named deployment.yaml with the following content :-

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Apply the deployment using kubectl :-

kubectl apply -f deployment.yaml

Step 2 :- Expose the Deployment with a Service

To expose the Nginx deployment to the internet create a Kubernetes Service of type LoadBalancer.

Create a file named service.yaml with the following content :-

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

Apply the service using kubectl :-

kubectl apply -f service.yaml

Step 3 :- Access the Application

Once the service is created it will provision an AWS Elastic Load Balancer (ELB) and expose your application to the internet. To get the external IP of the service run :-

kubectl get svc nginx-service

Access your application using the external IP address provided.

Managing EKS Cluster and Nodes

Scaling the Cluster

You can scale the number of nodes in your EKS cluster using eksctl :-

eksctl scale nodegroup --cluster=my-eks-cluster --name=<nodegroup-name> --nodes=5

Replace <nodegroup-name> with the name of your node group. This command scales the node group to 5 nodes.

Updating the Cluster

AWS EKS supports rolling updates for Kubernetes versions. To update your cluster to a new Kubernetes version use the AWS Management Console or eksctl :-

eksctl upgrade cluster --name my-eks-cluster --region us-west-2 --kubernetes-version 1.20

Monitoring and Logging

AWS provides several tools for monitoring and logging your EKS cluster :-

  • CloudWatch : Use CloudWatch Container Insights to collect, aggregate and analyze metrics and logs.

  • Prometheus and Grafana : Deploy Prometheus and Grafana for custom metrics and monitoring.

Example :- Setting Up CloudWatch Container Insights

  1. Enable Container Insights :-
aws eks update-cluster-config --name my-eks-cluster --region us-west-2 --logging '{"clusterLogging":[{"types":["api","audit","authenticator","controllerManager","scheduler"],"enabled":true}]}'
  1. Install the CloudWatch Agent :-
kubectl apply -f https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-eks.yaml

This will deploy the CloudWatch agent and FluentD as a DaemonSet on your cluster.

Best Practices for AWS EKS

1. Use IAM Roles for Service Accounts

Assign IAM roles to Kubernetes service accounts for fine-grained access control.

Example : Create an IAM role and associate it with a service account :-

eksctl create iamserviceaccount \
  --name my-service-account \
  --namespace default \
  --cluster my-eks-cluster \
  --attach-policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess \
  --approve

2. Implement Network Policies

Use Kubernetes Network Policies to control the traffic flow between pods and services.

Example : Create a file named network-policy.yaml with the following content :-

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-nginx
  namespace: default
spec:
  podSelector:
    matchLabels:
      app: nginx
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: nginx
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: nginx

Apply the network policy using kubectl :-

kubectl apply -f network-policy.yaml

3. Use Spot Instances for Cost Savings

Leverage EC2 Spot Instances to reduce costs for non-critical workloads.

Example : Create a node group with spot instances :-

eksctl create nodegroup --cluster my-eks-cluster --name spot-nodes --nodes 3 --instance-types "m5.large,m5a.large" --spot

4. Implement CI/CD Pipelines

Use CI/CD pipelines to automate the deployment of your applications to EKS.

Example :- AWS CodePipeline

  1. Create a CodePipeline :
  • Navigate to the AWS CodePipeline console and create a new pipeline.

  • Configure the source stage to use a code repository (e.g. GitHub).

  • Configure the build stage to use AWS CodeBuild.

  • Configure the deploy stage to use a custom deployment provider or Kubernetes manifest.

Conclusion

AWS Elastic Kubernetes Service (EKS) simplifies the process of running Kubernetes on AWS allowing you to focus on deploying and managing your containerized applications. By following the steps outlined in this blog you can set up a Kubernetes cluster, deploy applications and manage your EKS cluster effectively. Additionally implementing best practices such as IAM roles for service accounts, network policies, spot instances and CI/CD pipelines will ensure that your EKS environment is secure, cost-effective and scalable.

Stay tuned for more insights in our upcoming blog posts.

Let's connect and grow on LinkedIn :Click Here

Let's connect and grow on Twitter :Click Here

Happy Cloud Computing!!!

Happy Reading!!!

Sudha Yadav

ย