DEV Community

Inioluwa Eunice Atanda
Inioluwa Eunice Atanda

Posted on

5

Here's a step-by-step guide for setting up Kubernetes on an EC2 instance

Setting Up Kubernetes on EC2
Step 1: Update System Packages
Before installing any software, update your package lists to ensure you're getting the latest versions.

bash
sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Docker
Kubernetes runs containers, so you need to install Docker first.

  1. Install Required Dependencies Run the following command:
bash
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
Enter fullscreen mode Exit fullscreen mode
  1. Add Docker GPG Key and Repository Run these commands to add the Docker GPG key and repository:
bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Enter fullscreen mode Exit fullscreen mode
  1. Install Docker Update the package list and install Docker:
bash
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
Enter fullscreen mode Exit fullscreen mode
  1. Start and Enable Docker Ensure Docker runs on system startup:
bash
sudo systemctl start docker
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode
  1. Verify Docker Installation Check the installed version of Docker:
bash
docker --version
Step 3: Install Kubernetes Components (kubectl, kubeadm, kubelet)
Enter fullscreen mode Exit fullscreen mode
  1. Add Kubernetes Signing Key Run this command to add the Kubernetes signing key:
bash
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/kubernetes-archive-keyring.gpg
Enter fullscreen mode Exit fullscreen mode
  1. Add Kubernetes Repository Add the Kubernetes repository to your system's APT sources list:
bash
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
Enter fullscreen mode Exit fullscreen mode
  1. Install Kubernetes Components Update the package list and install kubelet, kubeadm, and kubectl:
bash
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
Enter fullscreen mode Exit fullscreen mode
  1. Prevent Automatic Updates (Optional) To avoid unintended upgrades that may disrupt your cluster, hold these packages at their current versions:
bash
sudo apt-mark hold kubelet kubeadm kubectl
Enter fullscreen mode Exit fullscreen mode
  1. Verify Installation Check the installed versions of Kubernetes components:
bash
kubeadm version
kubectl version --client
Enter fullscreen mode Exit fullscreen mode

Step 4: Enable Docker for Kubernetes
Kubernetes requires Docker’s cgroup driver to be set correctly.

  1. Check Current cgroup Driver Run this command to check the current cgroup driver:
bash
sudo docker info | grep -i cgroup
Enter fullscreen mode Exit fullscreen mode

If it does not say "systemd", proceed to configure it.

  1. Configure Docker to Use Systemd Create a configuration file for Docker:
bash
sudo mkdir -p /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
Enter fullscreen mode Exit fullscreen mode

🔹 step 9: Set Up Ingress for Better Traffic Routing
Instead of accessing our app via a NodePort, we’ll set up an Ingress Controller to handle traffic more efficiently.

🛠️ What is Ingress?
Ingress allows you to define routing rules for Kubernetes services.
It provides a stable URL (e.g., http://yourdomain.com).
It eliminates the need to use random NodePort numbers.
1️⃣ Install an Ingress Controller (NGINX)
Since we’re using AWS EC2, we’ll install the NGINX Ingress Controller.

Run:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
✅ This deploys the Ingress Controller.

Now, check if the pods are running:

kubectl get pods -n ingress-nginx
✅ Expected output:

NAME                                        READY   STATUS    RESTARTS   AGE
ingress-nginx-controller-xxxx               1/1     Running   0          1m
Enter fullscreen mode Exit fullscreen mode

2️⃣ Create an Ingress Resource
Now, let's create an Ingress Rule to route traffic to our Nginx deployment.

1️⃣ Create a file:

nano nginx-ingress.yaml
2️⃣ Add the following configuration:

`yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: nginx.example.com  # Replace this with your domain or public IP
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80`
Enter fullscreen mode Exit fullscreen mode

3️⃣ Apply the configuration:

kubectl apply -f nginx-ingress.yaml
✅ This sets up an Ingress route to forward traffic to the Nginx service.

3️⃣ Test the Ingress
Find the external IP of the Ingress Controller:

kubectl get svc -n ingress-nginx
✅ Look for an entry like this:
pgsql

NAME                                 TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)
ingress-nginx-controller             LoadBalancer  10.96.XXX.XXX   XX.XX.XX.XX    80:XXXXX/TCP
Enter fullscreen mode Exit fullscreen mode

🔹 The EXTERNAL-IP is what you'll use to access your app.

Now, test it:

curl -H "Host: nginx.example.com" http://XX.XX.XX.XX
✅ If successful, you'll see the Nginx welcome page.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs