Kubernetes On AWS Using KOPS
Introduction
Kubernetes Operations, or Kops, is an open source project used to set up Kubernetes clusters easily and swiftly. It is considered the “kubectl” way of creating clusters.
Kops allows deployment of highly available Kubernetes clusters on AWS and Google (GCP) clouds. It includes features such as dry-runs and automatic idempotency, terraform config generation making Kops a great option for do-it-yourself (DIY) developers.
Most DIY developers who like to set up clusters by themselves start by playing with minikube and then move onto Kops. For deploying small production grade clusters, Kops is one of the best tools out there. Among the numerous out-of-the-box features included with Kops are the large set of networking backends which give the user flexibility for picking and choosing the network stack that best works for their use case.
However, each of these network backends come with a myriad of configuration options and it quickly becomes complex and difficult to get those configurations right and bring a cluster up successfully.
In this article, we will demonstrate how to create Kubernetes Cluster using KOPS.
Prerequisites
- AWS EC2 Instance
- Create and attach below IAM roles to EC2 Instance,
* S3
* EC2
* VPC
* Route53
* Autoscaling
Steps to create Kubernetes Cluster Using KOPS
Step-1: Install AWS CLI
curl https://s3.amazonaws.com/aws-cli/awscli-bundle.zip -o awscli-bundle.zipapt install unzip pythonunzip awscli-bundle.zip#sudo apt-get install unzip - if you dont have unzip in your system./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
Step-2: Install Kops on EC2
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-amd64chmod +x kops-linux-amd64sudo mv kops-linux-amd64 /usr/local/bin/kops
Step-3: Install Kubectl
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectlchmod +x ./kubectlsudo mv ./kubectl /usr/local/bin/kubectl
Step-4: Create S3 bucket in AWS
S3 bucket is used by Kubernetes to persist cluster state.
Create S3 bucket using AWS CLI
Note: Make sure you choose bucket name that is unique across all AWS accounts
aws s3 mb s3://tests3.in.k8s --region ap-south-1
Step-5: Create private hosted zone in AWS Route53
- Head over to AWS Route53 and create hosted zone
- Choose name for example (testk8s.in)
- Choose type as private hosted zone for VPC
- Select default VPC in the region you are setting up your cluster
- Hit create
Step-6: Configure environment variables
Open .bashrc file
vi ~/.bashrc
Add the below content in .bashrc.
Note: You can choose any arbitrary name for cluster and make sure bucket name matches the one which we created in Step 4.
export KOPS_CLUSTER_NAME=testk8scluster.in
export KOPS_STATE_STORE=s3://tests3.in.k8s
Run the below command to reflect environment variables in the cluster
source ~/.bashrc
Step-7: Create SSH key pair
This keypair is used to SSH into Kubernetes cluster
ssh-keygen
Step-8: Create a Kubernetes cluster definition
kops create cluster \
--state=${KOPS_STATE_STORE} \
--node-count=2 \
--master-size=t2.micro \
--node-size=t2.micro \
--zones=ap-south-1a,ap-south-1b \
--name=${KOPS_CLUSTER_NAME} \
--dns private \
--master-count 1
Note: Choose appropriate values for region, zones and instance type
Step:9 Create Kubernetes Cluster
kops update cluster --yes
This may take a while to create the required AWS resources. Check the status of the Cluster using the below command and wait until the Cluster is Ready
kops validate cluster
Note: You might see validation failed error initially when you create cluster and it is expected behavior, you have to wait for some more time and check again.
Step10: Connect to the master
ssh admin@api.testk8scluster.in
Use “kubectl get nodes” command to get the details of Master Node and Worker Nodes
Application Deployment
We have this Sample Spring Boot application in below GitHub repository,
https://github.com/senthilkumar-ps/HelloKubernetes-App.git
Step1: Dockerize the application using this DockerFile and push the image to DockerHub
Step2: Create appropriate deployment and service entries in yaml for our dockerized application
Step3: Run the below command to deploy the application
kubectl create -f hellok8s.yaml
Once our application deployed successfully, get the External-IP to access the application
kubectl get svc
Step4: Navigate the URL http://External-IP:Port
Clean Up
Delete the Kubernetes Cluster using the following command
kops delete cluster testk8scluster.in -- yes
Conclusion
We have successfully created a Kubernetes Cluster using AWS Kops and deployed a sample Spring Boot application in it. Hope you find this article useful. In the next article, we shall see how to enable Continuous Deployment on AWS with ArgoCD using Helm.