Step-by-Step Guide: Installing Kubernetes on AWS EC2 Using KOPS
Humphrey Ikhalea
Cloud-Native DevOps & Backend Engineer (Kubernetes, AWS, Azure, GCP, Ansible, Terraform, Gitlab, Docker, Argo CD, Jenkin, Python, NodeJs, Golang, Bash)
Prerequisites
1. Install Dependencies
# Install Python, AWS CLI, kubectl, and KOPS
sudo apt-get update
sudo apt-get install -y python3-pip apt-transport-https
#Add Kubernetes repo and install kubectl
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install -y kubectl
# Install AWS CLI
pip3 install awscli --upgrade
echo 'export PATH="$PATH:/home/ubuntu/.local/bin/"' >> ~/.bashrc
source ~/.bashrc
If you encounter any error using pip3 for aws-cli installation
Try the command below it will fix the error
sudo snap install aws-cli --classic
aws --version
2. Install KOPS
# Download the latest KOPS binary
curl -LO https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64
# Make it executable and move to PATH
chmod +x kops-linux-amd64
sudo mv kops-linux-amd64 /usr/local/bin/kops
3. Configure AWS CLI
aws configure
# Follow prompts to enter AWS Access Key, Secret Key, Region (e.g., us-east-1), and output format (json)
4. Create S3 Bucket for KOPS State
aws s3api create-bucket --bucket kops-humphrey-storage --region us-east-1 --create-bucket-configuration LocationConstraint=us-east-1
5. Create the Kubernetes Cluster
# Generate cluster configuration for local domain
Here we are using a local domain, skip this step if you have a custom domain and move to the next stage.
kops create cluster --name=demok8scluster.k8s.local --state=s3://kops-humphrey-storage --zones=us-east-1a --node-count=1 --node-size=t2.micro --master-size=t2.micro --master-volume-size=8 --node-volume-size=8
# Edit cluster config (optional, to tweak settings) kops edit cluster demok8scluster.k8s.local --state=s3://kops-humphrey-storage
# Build the cluster kops update cluster demok8scluster.k8s.local --yes --state=s3://kops-humphrey-storage
For custom domain
If you have a custom domain the stage will be perfect for you. You can skip this stage if you testing with a local domain
领英推荐
Make Sure Your DNS is Set Up Right
Check that your DNS is pointing correctly to your domain:
If everything is set up properly, this command will return the nameservers for your domain.
kops create cluster --name=example.com --state=s3://kops-humphrey-storage --zones=us-east-1a --node-count=1 --node-size=t2.micro --master-size=t2.micro --master-volume-size=8 --dns-zone=example.com --node-volume-size=8
This command does a lot:
Note: Cluster creation takes 10–15 minutes.
6. Verify Cluster Health
kops validate cluster demok8scluster.k8s.local --state=s3://kops-humphrey-storage
# Check nodes
kubectl get nodes
7. Cleanup (Avoid Unnecessary Costs!)
kops delete cluster demok8scluster.k8s.local --state=s3://kops-humphrey-storage --yes
# Delete S3 bucket
aws s3 rb s3://kops-humphrey-storage --force
Troubleshooting Tips
Cost Optimization
Why This Setup?
Next Steps:
Following this guide, you’ve set up a cost-effective Kubernetes cluster on AWS – ideal for learning and experimentation!