Step-by-Step Guide: Installing Kubernetes on AWS EC2 Using KOPS

Step-by-Step Guide: Installing Kubernetes on AWS EC2 Using KOPS

Prerequisites

  1. AWS Account: With permissions to create EC2, S3, IAM, and VPC resources.
  2. EC2 Instance:


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:

  • Sets up the cluster name and state storage.
  • Specifies the AWS zones, number of nodes, instance sizes, and DNS settings


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

  1. Cluster Fails to Validate:
  2. Nodes Not Ready:
  3. KOPS Command Errors:


Cost Optimization

  • Use Spot Instances: Add --node-spot-price and --master-spot-price to kops create cluster.
  • Terminate Clusters when not in use.
  • Avoid t2.micro for Production: Use t3.small or larger for reliability.


Why This Setup?

  • KOPS: Simplifies Kubernetes cluster lifecycle management on AWS.
  • S3 Bucket: Stores cluster state for recovery and updates.
  • t2.micro: Fits AWS Free Tier (but not recommended for production).


Next Steps:

  • Deploy a sample app: kubectl create deployment nginx --image=nginx
  • Explore KOPS Documentation for advanced configurations.

Following this guide, you’ve set up a cost-effective Kubernetes cluster on AWS – ideal for learning and experimentation!

要查看或添加评论,请登录

Humphrey Ikhalea的更多文章

社区洞察

其他会员也浏览了