DEV Community

Arun Ramachandran
Arun Ramachandran

Posted on • Edited on

Kubernetes in AWS with kops

Kops is an official Kubernetes project for managing a Kubernetes clusters in aws.
Kops Stands for Kubernetes Operations.
Kops is currently the best tool to deploy Kubernetes clusters to Amazon Web Services.
Kops has commands for creating clusters, updating settings, and applying changes , Kops automates a large part of operating Kubernetes on AWS.
Kops only availabe for Linux and Mac Platforms.

Prepare AWS for Kops

Management Node (Local System - ubuntu 20.0.4)

In this management node below requirements must be required for kops.

1.kops
2.kubectl
3.aws cli
4.s3 bucket access

Image description

AWS

  1. Create an IAM User
  2. Assign the Permissions
  3. Create S3 bucket for storing KOPS_STORE_STATE
  4. Route53

Install kops on Ubuntu 20.0.4

curl -Lo kops 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
chmod +x kops
sudo mv kops /usr/local/bin/kops
Enter fullscreen mode Exit fullscreen mode

Install kubectl on Ubuntu 20.0.4

Update the apt package index and install packages needed to use the Kubernetes apt repository

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode
sudo apt-get install -y apt-transport-https ca-certificates curl
Enter fullscreen mode Exit fullscreen mode

Download the Google Cloud public signing key:

sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
Enter fullscreen mode Exit fullscreen mode

Add the Kubernetes apt repository

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

Update apt package index with the new repository and install kubectl

sudo apt-get update

sudo apt-get install -y kubectl
Enter fullscreen mode Exit fullscreen mode

Installing AWS CLI

sudo  apt install awscli
Enter fullscreen mode Exit fullscreen mode

Verify AWS CLI using command

 aws
Enter fullscreen mode Exit fullscreen mode

Create/Log-in AWS Console Account.

SetUp AWS IAM permission for Kops.

Create a user(kops) and give them permission.

Permission required for Kops user

AmazonEC2FullAccess
AmazonRoute53FullAccess
AmazonS3FullAccess
IAMFullAccess
AmazonVPCFullAccess
OR
AdministratorAccess
Enter fullscreen mode Exit fullscreen mode

Configure User with AWS Account.

Run command on your machine

aws configure
Enter fullscreen mode Exit fullscreen mode

Provide AWS access Key and AWS Secret Access Key.

Specify Default region or Output format.

Verify credentials and config.

 ls -lrt ~/.aws/
Enter fullscreen mode Exit fullscreen mode

S3 bucket for the KOPS_STATE_STORE.

KOPS_STATE_STORE is the source of truth for all clusters managed by Kops.

Get fastest Region for Deploy the S3 Bucket.

Create an S3 bucket for KOPS_STATE-STORE

aws s3 mb s3://<bucket-name>
aws s3 mb s3://k8s-test-123
Enter fullscreen mode Exit fullscreen mode

User can use cloudping to choose the fastest region as per their location.

Kops clusters must be valid DNS names.

We need to SetUp DNS for the Kops Clusters.

SetUp DNS in AWS Route53.

I have a Domain cloudmates.in which is availabe in Godaddy

For creating DNS in Route53

Go to Route53
Create Hosted Zone
Zone Name kops.cloudmates.in
Copy The NS records and create NS in your Domain Name Provider once completes validate the dns

dig -t ns=kops.cloudmates.in

Enter fullscreen mode Exit fullscreen mode

With Kops 1.6.2 or later, then DNS configuration is optional.
The only requirement to trigger this is to have the cluster name end with .k8s.local
SetUp Kubernetes Cluster on AWS with Kops

Generate SSH Key

ssh-keygen -f .ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Create Cluster Syntax

kops create cluster --yes --state=<s3://<Define S3 Bucket Name>> --zones=<One or more Zones> --node-count=<Number of Nodes> --node-size=<Define Machine Size> --master-size=<Master Node Size>  --name=<Define DNS Name>

Enter fullscreen mode Exit fullscreen mode

Create Cluster with Route53 hosted zone

kops create cluster --yes --state=s3://k8s-storage-a12345 --zones=ap-south-1a --node-count=2 --node-size=t2.micro --master-size=t2.micro --name=kops.cloudmates.in

Enter fullscreen mode Exit fullscreen mode

Verify Node Status

kubectl get node

Enter fullscreen mode Exit fullscreen mode

Validate Cluster

kops validate cluster --state=<s3://<Define S3 Bucket Name>
kops validate cluster --state=s3://k8s-test-123`

Enter fullscreen mode Exit fullscreen mode
### Execute Custom Image on AWS kubernetes

*Create AWS Kubernetes Cluster Without Domain Name *

   kops create cluster --yes --state=s3://k8s-test-123 --zones=ap-south-1a --node-size=t2.micro --node-count=2 --master-size=t2.micro --name=test.k8s.local

Enter fullscreen mode Exit fullscreen mode

Verify Kubernetes Cluster.(Different Formats)

   kops validate cluster
   kops validate cluster -o json
   kops validate cluster -o yaml

Enter fullscreen mode Exit fullscreen mode

Start the Deployment on Kubernetes Cluster.

 kubectl create deployment myweb --image=cloudmates/customnginx

Enter fullscreen mode Exit fullscreen mode

Get Information of Running Deployments

kubectl get deployments

Enter fullscreen mode Exit fullscreen mode

Describe the Running Deployment

 kubectl describe deployment myweb

Enter fullscreen mode Exit fullscreen mode

Make the myweb container accessible via the internet loadbalancer

kubectl create service loadbalancer myweb --tcp=80:80

Enter fullscreen mode Exit fullscreen mode

Get Running Services

kubectl get svc

Enter fullscreen mode Exit fullscreen mode

Remove Services

 kubectl delete services myweb

Enter fullscreen mode Exit fullscreen mode

Remove Deployment

 kubectl delete deployment myweb

Enter fullscreen mode Exit fullscreen mode

Delete Cluster

kops delete cluster cloudmates.in --yes --state=s3://k8s-test-123

Enter fullscreen mode Exit fullscreen mode

Top comments (0)