arsalandywriter.com

Kubernetes Fundamentals: Creating a Cluster on AWS Cloud9

Written on

In this guide, we will explore the fundamentals of Kubernetes by constructing a Kubernetes cluster with the help of AWS Cloud9. It's worth noting that while some resources used in this project are not eligible for the free tier, the overall expense was only around two cents.

In an upcoming article, we will delve into Amazon EKS, a managed Kubernetes service. For this project, I aim to draw a comparison between a Kubernetes cluster and a Docker Swarm running on Cloud9. You can find my Docker Swarm project here, which utilizes free-tier resources in AWS for deployment.

Getting Started

Kubernetes serves as a platform designed to manage containerized workloads and services. A container is essentially a software unit that includes packaged code along with its dependencies, enabling the application to operate consistently across different computing environments. You can think of a container as being akin to a laptop—self-sufficient and portable, unlike a desktop that requires numerous external components to function.

Docker is closely associated with containers, acting as an open-source platform for containerization. It facilitates developers in building, deploying, and running applications within containers. Docker Swarm mode is comparable to Kubernetes, functioning as a container orchestration tool that allows the management of multiple containers across various host machines. However, Kubernetes tends to be more widely adopted and scales more effectively than Docker Swarm.

It is also possible for Docker and Kubernetes to work in tandem. One can utilize Docker for packaging and shipping an application, while Kubernetes can be employed for deploying and scaling that application.

Kubernetes Cluster

A Kubernetes cluster comprises a collection of worker machines that execute containers. It has several key components:

  • The Control Plane oversees the entire cluster and provides an interface for interaction.
  • Worker Nodes are in charge of running and monitoring the containers assigned to them.
    • Worker nodes include a Container Runtime (software for running containers, such as containerd or Docker Engine) and kubelet, which manages Kubernetes operations on the node.

Let’s get started on building a cluster!

Prerequisites

  • An AWS account with IAM permissions.
  • AWS Cloud9 IDE. You will need to create three environments: one for the Control Plane and the other two as worker nodes. Select the Ubuntu platform and the t3.small instance type. If you opt for the t2.micro instance type in Cloud9, you will encounter an error while initializing Kubernetes.

Your three environments should be arranged as shown above.

Before proceeding to Cloud9, we must add some inbound security rules. First, look up your IP address online and note it down.

Return to the AWS console, navigate to the EC2 service, and select the running Cloud9 instances. Click on your security groups, and then edit the inbound rules. Add a rule for all traffic and paste your IP address, ensuring to select the CIDR block with the 32-bit address. Save these rules.

Next, we will add the Control Plane security group ID to the inbound rules of each worker node. Similarly, add the security group IDs of each worker node to the Control Plane's inbound rules. Below is an example of what the inbound rules might look like for the Control Plane:

Kubernetes Cluster Build

Access your Control Plane Cloud9 environment. We will assign unique hostnames to each node for identification. For the Control Plane, execute the following command:

sudo hostnamectl set-hostname control-plane

On each worker node, use the corresponding names:

sudo hostnamectl set-hostname worker-1

sudo hostnamectl set-hostname worker-2

If you close the terminal and open a new one, the hostname should be displayed.

To free up space, let’s remove any pre-installed Docker images on Cloud9:

docker system prune -a

We will utilize containerd as our container runtime. Before installing containerd, we need to enable certain kernel modules to load on startup, ensuring that the overlay and br_netfilter modules are activated each time the server boots:

cat << EOF | sudo tee /etc/modules-load.d/containerd.conf

overlay

br_netfilter

EOF

To enable these modules immediately, run:

sudo modprobe overlay

sudo modprobe br_netfilter

Next, we will configure some networking settings:

cat << EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf

net.bridge.bridge-nf-call-iptables = 1

net.bridge.bridge-nf-call-ip6tables = 1

net.ipv4.ip_forward = 1

EOF

After that, execute:

sudo sysctl --system

Finally, let's install containerd:

sudo apt-get update && sudo apt-get install -y containerd

Next, create a configuration file for containerd by making a directory:

sudo mkdir -p /etc/containerd

Then generate the configuration file:

sudo containerd config default | sudo tee /etc/containerd/config.toml

Lastly, restart containerd to apply the new configuration:

sudo systemctl restart containerd

Before proceeding with the installation of Kubernetes packages, we must disable swap as kubelet will fail to start if swap is detected on a node. To disable swap, execute:

sudo swapoff -a && sudo sed -i '/ swap / s/^/#/' /etc/fstab

Now, let’s install the required packages as outlined in the Kubernetes documentation. Run the following command to ensure they are installed:

sudo apt-get update && sudo apt-get install -y apt-transport-https curl

Next, we will set up our package repository by downloading a GPG signing key:

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

Now we will configure the repository:

cat << EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list

deb https://apt.kubernetes.io/ kubernetes-xenial main

EOF

Run another command to update the package listings:

sudo apt-get update

Next, we will install the Kubernetes packages, including kubelet, kubeadm, and kubectl:

  • kubelet oversees Kubernetes operations on the node, including starting, updating, and shutting down containers.
  • kubeadm is a tool that handles the necessary tasks to install and set up a cluster.
  • kubectl is the command-line tool that enables you to execute commands against the cluster.

Installation command:

sudo apt-get install -y kubelet=1.24.0-00 kubeadm=1.24.0-00 kubectl=1.24.0-00

To prevent the installation packages from being automatically upgraded, execute the following command:

sudo apt-mark hold kubelet kubeadm kubectl

Repeat these steps for each worker node, starting with enabling kernel modules for containerd. Here are the commands to be executed one at a time on each node:

cat << EOF | sudo tee /etc/modules-load.d/containerd.conf

overlay

br_netfilter

EOF

sudo modprobe overlay

sudo modprobe br_netfilter

cat << EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf

net.bridge.bridge-nf-call-iptables = 1

net.bridge.bridge-nf-call-ip6tables = 1

net.ipv4.ip_forward = 1

EOF

sudo sysctl --system

sudo apt-get update && sudo apt-get install -y containerd

sudo mkdir -p /etc/containerd

sudo containerd config default | sudo tee /etc/containerd/config.toml

sudo systemctl restart containerd

sudo swapoff -a && sudo sed -i '/ swap / s/^/#/' /etc/fstab

sudo apt-get update && sudo apt-get install -y apt-transport-https curl

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

cat << EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list

deb https://apt.kubernetes.io/ kubernetes-xenial main

EOF

sudo apt-get update

sudo apt-get install -y kubelet=1.24.0-00 kubeadm=1.24.0-00 kubectl=1.24.0-00

sudo apt-mark hold kubelet kubeadm kubectl

Now, we will initialize the cluster in the Control Plane. It’s optional to specify a pod network CIDR for the internal pod network range. If you choose to do so, the command would look like this:

sudo kubeadm init --pod-network-cidr=192.168.0.0/16 --kubernetes-version 1.24.0

Upon successful execution, you should see a message indicating that the control plane has been initialized!

Next, we will execute the three commands provided in the output to begin utilizing the cluster. Also, make sure to note down the kubeadm join command, as we will need it shortly:

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

If you enter the command kubectl get nodes, you should observe the following:

Now we have the ability to interact with our cluster. As indicated, the node is not yet ready, necessitating the deployment of a pod network for the cluster. To achieve this, input the following command to install the Calico networking plugin:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Subsequently, we will join the worker nodes to the Control Plane. Precede the kubeadm join command with sudo and paste it into each worker node environment.

After successfully adding both worker nodes to the cluster, return to the Control Plane node and enter kubectl get nodes again:

Congratulations! You have successfully set up a Kubernetes cluster!

Testing

Let’s conduct a brief test on our cluster by deploying an Nginx web server. Enter the following command:

kubectl create deployment nginx-web --image=nginx

Next, execute kubectl get deployment followed by kubectl get pods. You should see that the deployment was successful:

For one more test, let’s scale the deployment to three replicas:

kubectl scale --replicas=3 deployment nginx-web

Check again to verify the scaling was successful:

You can also use the describe command with kubectl describe deployment nginx-web to view more details about the deployment:

Extra

An additional remarkable feature of Kubernetes is its ability to restore a pod if a replica goes down. To test this, execute the command:

kubectl delete pod <POD_NAME>

This will remove the specified pod. When you enter kubectl get pods again, you will notice that the pod is immediately recreated:

Remember to delete your resources afterwards and stop all instances to avoid incurring extra charges. Thank you for reading!

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Unlock Your Potential: A Comprehensive Guide to

Discover how

Galaxies on the Move: Unraveling the Mystery of Messier 90

Discover the intriguing case of Messier 90, a galaxy moving closer to us in an expanding universe, and the science behind this phenomenon.

Understanding Bear Aesthetic: Reflections on Nature and Humanity

An exploration of how bears' appreciation for beauty parallels human aesthetics.

Exploring Historical Milestones Through Day Tripping Adventures

A look into significant historical events and figures through day trips, highlighting inventions, tributes, and musical milestones.

Unlocking the Secrets to Better Sleep: A Comprehensive Guide

Discover common sleep disorders and effective strategies to enhance your sleep quality.

Conquer Procrastination: 7 Effective Strategies for Success

Discover seven powerful tactics to defeat procrastination and enhance your productivity, fostering a more fulfilling daily routine.

Understanding the Divide: Good Science vs. Bad Science

Explore the crucial differences between good and bad science, guiding informed citizens through the complexities of scientific literature.

Unveiling the Truth: 8 Inventions Attributed to Edison That Weren't His

Explore eight inventions often credited to Thomas Edison that he did not actually create. Discover the truth behind these famous innovations.