Getting started with Kubernetes Clusters on AWS Using Kops
Rounak Surana
?? SRE/DevOps Evangelist | Driving Cloud Infrastructure Excellence | Kubernetes & Terraform Expert
Kubernetes and Kops overview :
Kubernetes is an open-source, container orchestration platform. Applications packaged as Docker images can be easily deployed, scaled and managed in a Kubernetes cluster.
Some of the key features of Kubernetes are:
- Self-healing.
- Failed containers are restarted to ensure that the desired state of the application is maintained. If a node in the cluster dies, then the containers are rescheduled on a different node. Containers that do not respond to application-defined health checks are terminated and thus rescheduled.
- Horizontal scaling.
- Number of containers can be easily scaled up and down automatically based upon CPU utilization, or manually using a command.
- Service discovery and load balancing.
- Multiple containers can be grouped together discoverable using a DNS name. The service can be load balanced with integration to the native LB provided by the cloud provider.
- Application upgrades and rollbacks.
- Applications can be upgraded to a newer version without an impact to the existing one. If something goes wrong, Kubernetes rolls back the change.
Kops, short for Kubernetes Operations, is a set of tools for installing, operating and deleting Kubernetes clusters in the cloud. A rolling upgrade of an older version of Kubernetes to a new version can also be performed. It also manages the cluster add-ons. After the cluster is created, the usual kubectl CLI can be used to manage resources in the cluster.
Prerequisites: AWS Free tier eligible account, know how to use AWS services like EC2, IAM, S3, Route53, and a desire to learn & play with Kubernetes.
Setup Kubernetes (K8s) Cluster on AWS:
- Create an Ubuntu EC2 instance. You can start by creating a t2.micro Ubuntu Server.
- SSH to your Ubuntu Instance and run the following commands to install AWS-CLI.
sudo apt-get update sudo curl https://s3.amazonaws.com/aws-cli/awscli-bundle.zip -o awscli-bundle.zip sudo apt install unzip python # sudo apt-get install unzip - if you dont have unzip in your system sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
- Install Kubectl on Ubuntu Instance.
sudo curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl sudo chmod +x ./kubectl sudo mv ./kubectl /usr/local/bin/kubectl
- Install Kops on Ubuntu Instance.
sudo 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-amd64 sudo chmod +x kops-linux-amd64 sudo sudo mv kops-linux-amd64 /usr/local/bin/kops
- Create an IAM Role with Route53, EC2, IAM, and S3 full access.
- Attach IAM role to Ubuntu Instance.
- Go back to your EC2 instance and run command to configure aws.
aws configure # Note: If you create IAM user with programmatic access then provide Access keys. # Otherwise region information is enough. # As I am working in Mumbai region so region name is ap-south-1
- Create a Route53 Private hosted zone (you can create a Public hosted zone if you have a domain). Kops uses DNS for discovery, both inside the cluster and outside so that you can reach the Kubernetes API server from clients. For learning purposes, I am using the domain "example.com".
- Create an S3 bucket to store your cluster's state. Kops lets you manage your clusters even after installation. To do this, it must keep track of the clusters that you have created, along with their configuration, the keys they are using etc. This information is stored in an S3 bucket. S3 permissions are used to control access to the bucket.
aws s3 mb s3://rounak.example.com
- Expose the environment variable and then kops will use this location by default.
export KOPS_STATE_STORE=s3://rounak.example.com
- Don't forget to generate ssh-keys before creating a cluster. This will be used for logging into our Kubernetes Cluster.
- Create Kubernetes cluster definitions on the S3 bucket.
kops create cluster --cloud=aws --zones=ap-south-1b --name=rounak.example.com --dns-zone=example.com --dns private
- List your clusters using below command:
kops get cluster
- Edit your node instance group and change machineType to t2.micro. You can also change maxSize/minSize to 1 for creating only one node.
kops edit ig --name=rounak.example.com nodes
- Edit your master instance group and change machineType to t2.micro:
kops edit ig --name=rounak.example.com master-ap-south-1b Finall
- Finally, configure your Kubernetes cluster using below command:
kops update cluster --name rounak.example.com --yes
- You will now see new EC2 instances in your AWS EC2 running instances dashboard as "nodes" and "master".
- Please wait about 5-10 minutes for a master to start, dns-controller to launch, and DNS to propagate and then validate your cluster.
kops validate cluster
- List your nodes in the cluster.
- Finally, SSH to the master where we will do our Kubernetes deployments. Give input "yes" when the prompt appears for continue connecting.
ssh -i ~/.ssh/id_rsa [email protected] OR ssh -i ~/.ssh/id_rsa admin@<public_ip_of_your_master>
- After logging into our master node, run the following command to get nodes. We can create Kubernetes deployments, pods, and services on this master node.
kubectl get nodes
- To get back to our earlier k8s-management-server(Ubuntu), type the following command.
exit
- Finally, delete the cluster and clean up things.
kops delete cluster rounak.example.com --yes
- Confirm with: kops get cluster on the k8s-management-server.
With the deletion of cluster, you will see the EC2 instances(nodes and master) which rolled up automatically during cluster creation will get terminated. Also, don't forget to delete the created hosted zone on AWS Route53 and your S3 bucket. Finally you can stop/terminate your k8s-management-server also.
I hope you enjoyed and learned the proper way of creating Kubernetes Cluster on AWS with Kops. Please share with your friends and connections if you find it useful and worth reading.
For any queries, please feel free to reach out to me on [email protected] or Linkedin.
Head- Software Development, Head- Software Engineering, Delivery Head, CSD Practice Lead | Presales Lead | Agile Coach
4 年Rounak Surana - Much appreciable effort. Keep writing articles
Senior DevOps Engineer at Tata Consultancy Services | TCS AI Cloud
4 年Highly Appreciate ???? Much needed article for getting stared with k8 on aws.