Introduction
Kubernetes has become the de facto standard for container orchestration, allowing developers to manage and scale their applications with ease. While setting up a Kubernetes cluster can be a complex task, tools like kOps simplify the process, especially for deploying Kubernetes on AWS. In this guide, we’ll walk you through the steps to set up a Kubernetes cluster using kOps.
What is kOps?
kops, short for Kubernetes Operations, is an open-source tool for deploying and managing Kubernetes clusters. It is designed to create production-grade Kubernetes clusters on AWS and is highly configurable, making it an excellent choice for those looking to leverage the power of Kubernetes without the complexity.
Prerequisites
Before you begin, make sure you have the following:
- An AWS account
- IAM service and Kubernetes knowledge
Create a AWS IAM user
- In the AWS Management Console, search for “IAM” and select the IAM service.
- Create a new user.
- In the IAM dashboard, under the “Users” section, click on “Create user.”.
Attach Policies:
- Directly attach policies: Choose specific policies to grant the user permissions. These policies are not fine-grained; you can attach the roles based on the principle of least privilege.
- AmazonEC2FullAccess
- AmazonEventBridgeFullAccess
- AmazonRoute53FullAccess
- AmazonS3FullAccess
- AmazonSQSFullAccess
- AmazonVPCFullAccess
- AutoScalingFullAccess
- IAMFullAccess
- Under Security Credentials, create an access key with programmatic access, After you get a successful message, download the access key file.
Configure kOps server
Download and Install kubectl
Kubectl is a command line tool used to interact with the Kubernetes cluster. Think of it as the pilot’s controls for a complex aircraft. It allows you to manage and configure various aspects of your Kubernetes environment
curl -LO https://dl.k8s.io/release/v1.29.0/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
Download and Install AWS CLI
AWS CLI (Amazon Web Services Command Line Interface) is your ticket to managing AWS resources directly from your terminal. It’s a powerful tool that lets you interact with a wide range of AWS services without needing to open the web console.
sudo apt install unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
Use the command aws configure
to configure the CLI with the access and secret keys.
Download and Install KOps
curl -Lo kops https://github.com/kubernetes/kops/releases/download/v1.29.0/kops-linux-amd64
chmod +x kops
sudo mv kops /usr/local/bin/kops
Create an S3 Bucket for kOps State Storage
aws s3api create-bucket --bucket <your-unique-bucket-name> --region <your-region> --create-bucket-configuration LocationConstraint=<your-region>
Set the environment variable for your bucket:
When you set a variable in the shell without using it export
, it is considered a shell variable and is only available within the current shell session, but if you start a new shell session or run a new process from this shell, that variable will not be available for that process.
KOPS_STATE_STORE
defines the S3 bucket used to store the state of the cluster. This is where kOps will save the cluster’s configuration, secrets, and other metadata.
export KOPS_STATE_STORE=s3://<bucket-name>
By adding the variable to .bashrc
, it will be automatically set whenever you start a new shell session, ensuring the variable persists across sessions. It ensures the consistency of environment variables across different shell sessions. You don’t have to manually set variables whenever you open a new terminal window or connect to a new session.
echo “export KOPS_STATE_STORE=s3://<bucket-name>” >> .bashrc
Use Kops to create the cluster
The kops create cluster
command is used to create the initial configuration for a Kubernetes cluster on AWS using kOps (Kubernetes Operations). This command sets up the cluster’s desired state, which can then be modified and applied to deploy the cluster infrastructure and control plane components.
kops create cluster \
--name=devopstryhard.k8s.local \
--zones=ap-south-1a \
--node-count=1 \
--node-size=t3.small \
--control-plane-size=t3.small \
--control-plane-volume-size=10 \
--node-volume-size=10
kOps create cluster command breakdown:
Cluster Name (--name
):
- Specifies the fully qualified domain name (FQDN) for the cluster. This is used to identify the cluster uniquely within the kOps configuration.
- Example:
--name=mycluster.example.com
Cluster availability zones (--zones
):
- Specifies the AWS availability zones where the cluster nodes will be deployed.
- Example:
--zones=us-west-2a,us-west-2b
Cluster worker nodes count (--node-count
):
- Sets the number of worker nodes to create in the cluster.
- Example:
--node-count=3
Cluster worker node machine configuration (--node-size)
:
- Determines the instance type for the worker nodes.
- Example:
--node-size=t3.medium
Cluster control plane master node machine configuration (--control-plane-size
):
- Specifies the instance type for the master nodes.
- Example:
--control-plance-size=t3.medium
Control plane master node volume size (--control-plane-volume-size
):
- Specifies the size of the EBS volume attached to each master node.
- Example:
--control-plane-volume-size=50
Worker Node Volume Size (--node-volume-size
):
- Specifies the size of the EBS volume attached to each worker node.
- Example:
--node-volume-size=100
Update the cluster
The kops update cluster
command is used to apply the configuration changes you’ve made to your Kubernetes cluster. This command translates the desired state of your cluster, as defined in the kOps state store, into actual changes in your cloud provider infrastructure.
kops update cluster --name=<cluster-name> --yes --admin
This command below creates a kubeconfig file for interacting with your Kubernetes cluster as an administrator. Once generated, you can use this file with tools like kubectl
to interact with your cluster.
kops export kubecfg --admin
Validate the cluster
The kops validate cluster command is used to verify the health and readiness of your Kubernetes cluster after creation or updates. It checks the state of the cluster components to ensure they are all functioning correctly.
kops validate cluster --wait 10m --count 3
It also helps in checking the cluster’s health and ensures that all nodes are up and running and that the Kubernetes API server is responsive. Node status confirms that all nodes are healthy and correctly registered with the Kubernetes API server. Component status verifies the proper functioning of critical Kubernetes components like etcd, kube-scheduler, and kube-controller-manager. Finally, the output provides a summary indicating if the cluster is ready or highlights any issues needing attention.
Conclusion
In conclusion, creating a Kubernetes cluster using kOps on AWS provides a robust and flexible solution for managing containerized applications at scale. Kops simplifies the complex process of cluster deployment by automating the provisioning of AWS resources and ensuring the seamless integration of various components. Whether you are just beginning your Kubernetes journey or looking to optimize your existing setup, kops on AWS is a proven method to achieve a resilient and well-architected Kubernetes environment.