DEV Community

Arun Ramachandran
Arun Ramachandran

Posted 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.

User can test the SubDomain

dig ns subdomain.example.com
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.

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>
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=k8s.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://kops-storage-a123456
Enter fullscreen mode Exit fullscreen mode
### Execute Custom Image on AWS kubernetes

Create AWS Kubernetes Cluster

   kops create cluster --yes --state=s3://kops-bucket-a12345 --zones=ap-south-1a --node-size=t2.micro --node-count=2 --master-size=t2.micro --name=cloudmates.in
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://kops-bucket-a12345
Enter fullscreen mode Exit fullscreen mode

Discussion (0)