Deploying the Dockerized application in a virtual machine or Elastic Compute Cloud (EC2) instance requires more maintenance activity for scaling the application and managing the environment. If we use Elastic Container Service for Kubernetes (EKS), the master is managed by Amazon Web Services (AWS). This is not beneficial as we do not have any control of the master, apart from increasing costs.
CloudifyOps recommends implementing a Kubernetes cluster using the KOPS method in AWS
This ensures the master and worker nodes are in our control and reduces the cost in comparison to EKS. The application can be scaled using Horizontal Pod Autoscaler (HPA).
What is a Kubernetes cluster?
A Kubernetes cluster is a set of nodes that run containerized applications. Containerizing applications packages an app with its dependencies and some necessary services. They are more lightweight and flexible than virtual machines.
Kubernetes clusters enable applications to be easily developed, moved and managed.
The clusters allow containers to run across multiple machines and environments: virtual, physical, cloud-based, and on-premises. Kubernetes containers are not restricted to a specific operating system, unlike virtual machines, and have the ability to share operating systems and run anywhere.
Kubernetes clusters consist of one master node and a number of worker nodes. These nodes can either be physical computers or virtual machines, depending on the cluster.
The master node controls the state of the cluster; for example, which applications are running and their corresponding container images. The master node is the origin for all task assignments. It coordinates processes such as:
- Scheduling and scaling applications
- Maintaining a cluster’s state
- Implementing updates
We can build a Kubernetes cluster in different ways:
- Official Kubernetes tool: Kubeadm (this is not efficient)
- AWS EKS : Takes approximately 20 minutes
- Azure EKS : Relatively Time-consuming
- KOPS: KOPS is a third party tool and will be used to create k8s clusters in any of the AWS cloud based environments.
Requirements to create a Kubernetes cluster with KOPS:
- Deploy a management server which holds all scripts
- S3 Bucket and Route 53 domain integration
- KOPS Binary (k8s cluster management) & Kubectl Binary (k8s cluster operations)
- AWS Command-Line Interface (CLI) and AWS Access/secret key or assign role with admin permissions
- SSH public and private keys.
How to Set Up:
Step 1: Prepare your Host Environment
Install an Ubuntu 20.04 server. This will be the management server that holds all the required scripts to create a cluster and the entire configuration.
Install Kubectl Binary:
Kubectl is a command-line tool that simplifies the management of Kubernetes clusters. It lets you run commands against them.
The following command helps to download the latest version of Kubectl
- curl -LO “https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl”
Provide read write and execution permissions for Kubectl folder
- Chmod 700 kubectl
Move kubectl folder to the /usr/local/bin
- mv kubectl /usr/local/bin/kubectl
To verify the installation, run the below command
- kubectl version — short
Install kops Binary
KOPS is a set of tools that can be used to install, run, and delete Kubernetes clusters in the cloud. It can also manage the add-ons for the cluster.
Downloading the KOPS binary
Provide read write executable permissions to the kops-linux-amd64
- chmod 700 kops-linux-amd64
Move kops-linux-amd64 directory to the /usr/local/bin and rename it as a kops
- mv kops-linux-amd64 /usr/local/bin/kops
To verify the KOPS installation, run the following
- KOPS version
Install AWS CLI tools
AWS CLI is used to communicate with the AWS cloud environment. While creating the cluster, we will write a state file to the S3 bucket, which the AWS CLI will help in writing to that location.
Before downloading AWS CLI, we need to install unzip in it
- apt install unzip -y
- curl “https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip” -o “awscliv2.zip”
- unzip awscliv2.zip
- sudo ./aws/install
Once the AWS CLI installation is complete, we can verify by running the below command.
- aws — version
Step 1: Configure AWS CLI tools
To configure this, we can use a user with AWS_ACCESS_KEY and AWS_SECRET_ACCESS_KEY or use a role with required permissions.
We create and attach a role with administrator permissions to the management instance.
Generate SSH key:
In the process of cluster creation, EC2 instances will be created as masters and slaves. The SSH keys will be attached to the master and slaves. We can login to the master and slave servers using a private key file.
To generate SSH key, run the below command
Step 2: Configure Route 53 Domains & S3 bucket
Creating a dedicated S3 bucket for KOPS will allow you to store all of your cluster’s details. In this guide, we shall call this bucket kops-state-store.
To create the s3 bucket, run the following
- aws s3api create-bucket \
— bucket <.bucket-name.> \
— region us-east-1
If you bought your domain elsewhere, and would like to dedicate the entire domain to AWS, you should follow this guide.
Step 3: Kubernetes cluster creation
We have configured all requirements to spin up a cluster. Now we can spin up a cluster by running the following command:
- kops create cluster — name=<.clustername.> \
— zones=us-east-1a — yes
If you are deploying a cluster using the above command, the clusters will have default volume sizes (node of 124GB and master of 64GB). To ensure we have control of the cluster, we can use customized command while creating the cluster.
kops create cluster — name=bittergourd.xyz \
— state=s3://<.s3-bucket-name.> — zones=us-east-1a,us-east-1b,us-east-1c\
— node-count=1 — node-size=t3.medium \
— master-size=t3.medium — master-volume-size 20 — node-volume-size 10 \
— dns-zone=<.hostedzone-name.> — yes
It is recommended to have same names for cluster and DNS. With the above command, we can create a single master and a single node cluster with the limited size attached to the instances. It will take 10 minutes to bring up the cluster once you hit enter. You can check the cluster state with the below command
- kops validate cluster — state s3://<.s3 bucket-name.>
Once the cluster is up, you will get the below output.
We can export a state location to the bashrc. For KOPS to read a state file location from the bashrc profile every time, we run the KOPS commands.
- export KOPS_STATE_STORE=s3://<.s3 bucket name.>
We can increase and decrease the size of the cluster by editing the instance groups
- kops get ig
kops edit ig <.instance group name.>
By increasing the maximum and minimum numbers, we can scale up and down the cluster.