Kubernetes is one of the most popular DevOps tools because of its amazing container orchestration system and features. But although Kubernetes offers so many functionalities, setting up a Kubernetes cluster from scratch is painful. This is where Kops comes into the picture. With Kops, it is a cakewalk to create a Kubernetes cluster on cloud providers like AWS, Google Cloud, etc. It makes setting up a Kubernetes cluster hassle-free, and in this article, I will discuss this awesome tool.
What is Kops?
Kops, also known as Kubernetes operations, is an open-source, official Kubernetes project, which allows you to create, maintain, upgrade and destroy a highly available production-grade Kubernetes cluster. It provisions the cloud infrastructure also depending on the requirement. The developers of Kops describe it as kubectl for Kubernetes clusters. Kops is mostly used in deploying AWS and GCE Kubernetes clusters. Kops officially supports only AWS, other cloud providers such as DigitalOcean, GCE, and OpenStack are in the beta stage. If you have worked on kubectl before, you will feel comfortable working on Kops. Kops provides commands to create, get, update, delete clusters. In addition, Kops knows how to apply changes in the existing clusters as it uses declarative configuration. With Kops, you can also scale up and down a Kubernetes cluster. Below are the features of Kops:
Deploys Kubernetes masters with high availability Rolling cluster updates are supported Automates the provisioning of AWS and GCE Kubernetes clusters Manages cluster add-ons Autocompletion of command in the command line Generates CloudFormation and Terraform configurations Supports state-sync model for dry-runs and automatic idempotency Creates instance groups to support heterogeneous clusters
Installing Kops
Below are simple steps to install Kops on a Linux environment. I am using Ubuntu 20.x. First, download Kops from the releases package. The command below downloads the latest package of Kops. You need to provide executable permission to the kops file you downloaded and move it to /usr/local/bin/ directory. Installation is done. Now you can run the kops command to verify the installation. Check the Kops version to be sure that Kops for installed correctly. Let us now look at few important Kops commands which are used widely by admins for executing Kubernetes operations.
Kops Commands
Below are the widely used Kops commands you must know.
kops create
The kops create command is used to register a cluster.
Syntax: kops create cluster
kops update
The kops update command is used to update the cluster with the specified cluster specification.
Syntax: kops update cluster –name
kops get
The kops get command is used to list all the clusters. Syntax: kops get clusters
kops delete
The kops delete command is used to delete a specific cluster from the registry and all the cloud resources assigned to that cluster.
Syntax: kops delete cluster –name
kops rolling-update
The kops rolling-update command is used to update a Kubernetes cluster to match the cloud and kops specifications.
Syntax: kops rolling-update cluster –name
kops validate
The kops validate command validates if the cluster you created is up or not. For example, if the pods and nodes are in the pending state, the validate command will return that the cluster is not healthy yet. Syntax: kops validate cluster –wait <specified_time> This command will wait and validate the cluster for the specified time. So, if you want to validate the cluster for five minutes, run the command with 5m as specified time. That was all about Kops fundamentals, let me now show you how to create a Kubernetes cluster on AWS using Kops.
Set Up Kubernetes on AWS using Kops
Before you begin with the steps mentioned below, these are a few pre-requisites:
Ubuntu 20.04 AWS CLI AWS Configure
Installing kubectl
Firstly, I will install kubectl. Kubectl is used to run command line commands on Kubernetes clusters. Download a kubectl package. You need to provide executable permission to the downloaded file and move it to /usr/local/bin/ directory.
Create S3 Bucket
Once you have AWS CLI installed and configured in your Linux machine, you will be able to run aws commands. I have aws cli installed on my Ubuntu system, so let me run a simple command which will list all the buckets in S3. It will be empty because I don’t have any s3 bucket as of now. Let me check if any ec2 instance is running. This means no ec2 instance is running as of now. Now you need to create an s3 bucket where Kops will save all the cluster’s state information. Here I am creating an s3 bucket in the us-west-2 region with the name geekkops-bucket-1132. You can use LocationConstraint to avoid any error with the region. If I list the aws s3 bucket again, I will get the bucket I just created. Run the below command to enable the version for the s3 bucket.
Generate Key
Generate ssh key for which will be used by Kops for cluster login and password generation.
Export Environment Variables
Expose the cluster name and s3 bucket as environment variables. This will be applicable only for the current session. I am using the suffix ‘.k8s.local’ because I am not using any preconfigured DNS.
Create the Cluster
Use the kops create command to create the cluster. Below are the parameters I am using to create a Kubernetes cluster on AWS using Kops:
–cloud tells the cloud provider I am using –zones is the zone where the cluster instance will get deployed –node-count is the number of nodes to deploy in the Kubernetes cluster –node-size and –master-size are the ec2 instance types, I am using the micro instances –name is the cluster name
Run kops get command to see if the cluster got created.
Update the Cluster
To apply the specified cluster specifications to the cluster, run the kops update command. If you immediately check the Kubernetes nodes are running or not, you will get an error. You need to be a little patient and wait for a few minutes (5-10) till the cluster is created.
Validate the Cluster
I am running the validate command for 5 minutes to check if the cluster is up and healthy or not. In the validate output, you will be able to see the node details when they are up.
List the Nodes and Pods
Now run the below command to check if all the nodes are ready and running. You can see both master and node are ready status. You can check all the pods running in the Kubernetes cluster.
Delete the Cluster
Just like creating a Kubernetes cluster, deleting a Kubernetes cluster using Kops is very straightforward. This kops delete command will remove all the cloud resources of the cluster and the cluster registry itself.
Conclusion
I hope this article on Kops was helpful, and you got to learn something new today. Kops is a fantastic tool to work with Kubernetes on the cloud. So go ahead and try out the steps mentioned in this article and set up your Kubernetes cluster on AWS using Kops.