How to Deploy Kubernetes Cluster in 60-Seconds using KinD Distro on Ubuntu EC2 Instance
I have been an avid and consistent user of Kubernetes, and overtime I have been able to install Kubernetes countless times on different machines and instances or virtual boxes. Within this time, I was faced with different installation and troubleshooting errors that cost me several hours, days and even weeks just to perform a single task “Install Kubernetes on my machine”. Everyday, I kept thinking of what other ways I can make life easier for myself when installing Kubernetes until I stumbled upon this goldmine “Kubernetes IN Docker”.
One amazing thing is, as developers we can quickly provision and destroy k8s clusters as many times as possible to test application deployments any time we want before pushing to production.
More Reasons? I got you..
1. Local Testing: KIND allows for easy, fast local Kubernetes cluster setup, enabling rapid testing and development.
2. CI Integration: Seamlessly integrates with CI/CD pipelines for automated testing.
3. Consistency: Ensures consistent environment setups mirroring production Kubernetes clusters.
4. Lightweight: Resource-efficient, running clusters in Docker containers.
5. Portability: Easily shareable configurations, enhancing collaboration across teams.
Overview:
Step 1: Launch an Ubuntu EC2 Instance
Step 2: Connect to Your EC2 Instance
Step 3: Install Docker
Step 4: Install Kind
Step 5: Create a Kubernetes Cluster using KinD
Step 6: Install Kubectl
Step 7: Use Kubectl to Interact with the Kind Cluster
Step 8: Access the Deployed Application
Deploying a Kubernetes cluster using `kind` (Kubernetes IN Docker) on an Ubuntu EC2 instance is a straightforward process. This comprehensive guide will walk you through each step, from setting up the EC2 instance to deploying a Kubernetes cluster with `kind`.
Step 1: Launch an Ubuntu EC2 Instance
- Log in to AWS Management Console: Navigate to the EC2 Dashboard.
2. Launch Instance: Click on “Launch Instance” and follow these steps:
- Choose an Amazon Machine Image (AMI): Select an Ubuntu Server 20.04 LTS AMI.
- Choose an Instance Type: Select a t2.medium or larger instance type for better performance.
- Configure Instance Details: Set the number of instances to 1, and adjust other settings as needed.
- Add Storage: The default 8GB or 20GB is typically sufficient, but you can increase if needed.
- Add Tags: Optionally add tags for easier management.
- Configure Security Group: Create a new security group with the following rules:
- SSH (port 22) from your IP address.
- HTTP (port 80) and HTTPS (port 443) cause we plan to expose services.
- Review and Launch: Review your settings and launch the instance. You will need to select or create a key pair to SSH into your instance.
If you are new to EC2 instances and creating VPCs, click here for a comprehensive guide
Step 2: Connect to Your EC2 Instance
- Obtain the Public IP Address: From the EC2 Dashboard, copy the public IP address of your instance.
2. SSH into the Instance: Open your terminal and connect using the key pair you selected during instance creation.
ssh -i /path/to/your-key-pair.pem ubuntu@your-ec2-public-ip
This is my key, and that is what I will be using to authenticate into this instance
Step 3: Install Docker
- Update the Package List:
sudo apt-get update
2. Install Required Packages:
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
3. Add Docker’s Official GPG Key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
4. Add Docker APT Repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
5. Update the Package List Again:
sudo apt-get update
6. Install Docker:
sudo apt-get install -y docker-ce
7. Verify Docker Installation:
sudo systemctl status docker
8. Add Your User to the Docker Group: (Optional, for running Docker commands without sudo):
sudo usermod -aG docker ${USER}
newgrp docker
Step 4: Install Kind
- Download and Install Kind:
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.14.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
2. Verify Kind Installation:
kind version
Step 5: Create a Kubernetes Cluster using KinD
- Create a Configuration File for the Kind Cluster: Optional
In this “kind-config.yaml” file, I will be using “One Master Node and One Worker Node”. You can add more nodes by adding one or more “role” in the kind-config.yaml file. Note: Master Node is the same as control-plane
cat <<EOF >kind-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
EOF
2. Create the Cluster:
kind create cluster --config kind-config.yaml
NOTE: If you did not create a configuration file, you can simply run:
kind create cluster #DONT RUN THIS COMMAND, IF YOU ALREADY DID THE ONE ABOVE
Step 6: Install Kubectl
- Download the Latest Release of Kubectl:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
2. Install Kubectl
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
3. Verify Kubectl Installation:
kubectl version --client
4. Verify Cluster Nodes:
kubectl get nodes
Step 7: Use Kubectl to Interact with the Kind Cluster
- Get Cluster Info:
kubectl cluster-info - context kind-kind
2. Deploy a Sample Application
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
Step 8: Access the Deployed Application
- Get the Public IP of the EC2 Instance: Use the same IP you used to SSH into the instance.
- Get the Port on Which Nginx is Running:
kubectl get svc
3. Get the Node Internal IP on Which Nginx is Running:
I will not be using a browser to access the page, I will be using the Node Internal IP and curl it directly from the terminal.
In the image above, we can see the Node details from which our nginx app is running together with its TCP port number. which are NodeIP = 172.18.0.3 and NodePor = 31349 . We will use the NodeIP and NodePort number to access the App or verify if the App is running successfully using “curl”.
3. Access the Application: Open your browser and navigate to
http://<your-ec2-public-ip>:<node-port>
In my case, just like from the image shown above I will be using my NodeInternal-IP and NodePort.
http://172.18.0.3:31349
Nginx App Output
By following these steps, you will have a fully functional Kubernetes cluster running on an Ubuntu EC2 instance using `kind`. You can now deploy and manage Kubernetes applications on this cluster.
Troubleshooting
#devops #devopsengineer #GabrielOkom #UgochiGabrielOkom #aws #awsdevops #kind #kindforkubernetes #sre #devopsprojects