Getting Started With Kubernetes on AWS Tutorial (2023 Update)

Matt Mickiewicz
Share

Welcome to this beginner’s tutorial on using Kubernetes with AWS. In this tutorial, we’ll walk you through the process of setting up a Kubernetes cluster on AWS, deploying a sample application, and managing your cluster. By the end of this tutorial, you’ll have a solid understanding of the basics of Kubernetes and how to use it with AWS.

Contents:

  1. Introduction to Kubernetes
  2. Setting up a Kubernetes Cluster on AWS
  3. Deploying a Sample Application
  4. Managing Your Cluster
  5. Common Questions about Kubernetes

Introduction to Kubernetes

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It was originally developed by Google and is now maintained by the Cloud Native Computing Foundation (CNCF). Kubernetes provides a powerful and flexible framework for managing containers, making it an essential tool for modern application development and deployment.

Setting up a Kubernetes Cluster on AWS

In this section, we’ll guide you through the process of setting up a Kubernetes cluster on AWS using the managed Kubernetes service, Amazon Elastic Kubernetes Service (EKS).

Prerequisites

Before we begin, make sure you have the following:

  • an AWS account
  • AWS CLI installed and configured
  • kubectl installed
  • eksctl installed

Creating a Kubernetes Cluster with eksctl

eksctl is a command-line tool that simplifies the process of creating and managing Kubernetes clusters on AWS. To create a new cluster, run the following command:

eksctl create cluster --name my-cluster --region us-west-2 --nodes 3

This command creates a new Kubernetes cluster named my-cluster in the us-west-2 region with three worker nodes. It may take a few minutes for the cluster to be created.

Configuring kubectl to Use Your Cluster

Once your cluster is created, you need to configure kubectl to use it. Run the following command:

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

This command updates your kubectl configuration to use the new cluster.

Deploying a Sample Application

In this section, we will deploy a simple “Hello, World!” application to your Kubernetes cluster.

Creating a Deployment

Create a file named hello-world.yaml with the following contents:

apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 3
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: gcr.io/google-samples/node-hello:1.0
ports:
- containerPort: 8080

This YAML file defines a Kubernetes Deployment that creates three replicas of a “Hello, World!” application. Save the file and run the following command to create the Deployment:

kubectl apply -f hello-world.yaml

Exposing the Application

To access the application from outside the cluster, you need to create a Kubernetes Service. Run the following command:

kubectl expose deployment hello-world --type=LoadBalancer --port=80 --target-port=8080

This command creates a LoadBalancer Service that exposes the “Hello, World!” application on port 80.

Accessing the Application

To access the “Hello, World!” application, you need to find the load balancer’s address. Run the following command:

kubectl get services

Look for the EXTERNAL-IP of the hello-world service. Once you have the external IP, open a web browser and navigate to http://EXTERNAL-IP. You should see the “Hello, World!” message displayed.

Managing Your Cluster

In this section, we’ll cover some basic tasks for managing your Kubernetes cluster, such as scaling your application and updating your deployment.

Scaling Your Application

To scale your application, you can update the number of replicas in your Deployment. For example, to increase the number of replicas to 5, run the following command:

kubectl scale deployment hello-world --replicas=5

This command updates the Deployment to have five replicas. Kubernetes will automatically create additional Pods to meet the desired replica count.

Updating Your Deployment

If you need to update your application, you can update the container image in your Deployment. For example, to update the “Hello, World!” application to version 2.0, run the following command:

kubectl set image deployment hello-world hello-world=gcr.io/google-samples/node-hello:2.0

This command updates the container image in the Deployment, and Kubernetes will automatically perform a rolling update to replace the old Pods with new ones.

Monitoring Your Cluster

To monitor the status of your cluster, you can use the Kubernetes Dashboard, a web-based user interface for managing your cluster. To access the Dashboard, run the following command:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml

Next, start the kubectl proxy by running:

kubectl proxy

Now, you can access the Dashboard by navigating to this URL in your web browser:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/
➥https:kubernetes-dashboard:/proxy/

Common Questions about Kubernetes

Let’s end by answering some common question about Kybernetes.

What is the difference between a Pod and a Deployment?

A Pod is the smallest and simplest unit in Kubernetes and represents a single instance of a running process. A Deployment is a higher-level abstraction that manages the desired state of your application, such as the number of replicas and the container image to use. Deployments automatically create and manage Pods to ensure the desired state is maintained.

What is a Kubernetes Service?

A Kubernetes Service is an abstraction that defines a logical set of Pods and a policy for accessing them. Services provide a stable IP address and DNS name, making it easy to discover and access your application within the cluster or externally.

How do I update my application in Kubernetes?

To update your application, you can update the container image in your Deployment using the following command:

kubectl set image

Kubernetes will perform a rolling update to replace the old Pods with new ones, ensuring zero downtime.

How do I scale my application in Kubernetes?

To scale your application, you can update the number of replicas in your Deployment using the following command:

kubectl scale

Kubernetes will automatically create or remove Pods to meet the desired replica count.

What is the Kubernetes Dashboard?

The Kubernetes Dashboard is a web-based user interface for managing your Kubernetes cluster. It provides an overview of the cluster’s state, as well as the ability to create delete resources such as Deployments, Services, and Pods.

Conclusion

By following this tutorial, yo’ve learned the basics of using Kubernetes with AWS, including setting up a Kubernetes cluster on AWS, deploying a sample application, and managing your cluster. With this foundation, you can continue to explore more advanced Kubernetes concepts and features to build and deploy scalable, resilient applications.