kubernetes logo

Kubernetes tutorial – Create Kubernetes cluster on AWS with kops

This is in continuation of Kubernetes article series. In the earlier posts, we have seen how to create & deploy a simple cluster. Now in this post, we are going to look at how to provision Kubernetes cluster using kops utility. For more posts on Kubernetes, check out here & here.

kops (Kubernetes Operations) helps you create, destroy, upgrade and maintain production-grade, highly available, Kubernetes clusters from the command line. Some of the key features are listed below:

  • Automates the provisioning of Kubernetes clusters in AWS and GCE
  • Deploys Highly Available (HA) Kubernetes Masters
  • Uses DNS to identify clusters
  • Self-healing: everything runs in Auto-Scaling Groups
  • Limited OS support (Debian preferred, Ubuntu 16.04 supported, early support for CentOS & RHEL)
  • Supports custom Kubernetes add-ons
  • YAML Manifest Based API Configuration
  • Can directly provision, or generate terraform manifests

This quickstart assumes a basic understanding of Kubernetes concepts, please refer earlier posts for understanding on Kubernetes & how to create, deploy & rollout updates to the cluster.

Step #1.kops Installation

I’m using Linux so here are the commands to get kops, for other releases/ platform you can find it here.

wget -O kops 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 
chmod +x ./kops 
sudo mv ./kops /usr/local/bin/

Install kops CLI
Image – Install kops CLI

Step #2.kubectl installation

Download the latest release with the command:

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
kubectl Installation
Image – kubectl Installation

Make the kubectl binary executable.

chmod +x ./kubectl

Move the binary in to your PATH.

sudo mv ./kubectl /usr/local/bin/kubectl

To check what are the available kubectl commands,run kubectl from the terminal

$ kubectl
kubectl controls the Kubernetes cluster manager.

Find more information at https://github.com/kubernetes/kubernetes.

Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         Take a replication controller, service, deployment or pod and
expose it as a new Kubernetes Service
  run            Run a particular image on the cluster
  set            Set specific features on objects
  run-container  Run a particular image on the cluster. This command is
deprecated, use "run" instead

Basic Commands (Intermediate):
  get            Display one or many resources
  explain        Documentation of resources
  edit           Edit a resource on the server
  delete         Delete resources by filenames, stdin, resources and names, or
by resources and label selector

Deploy Commands:
  rollout        Manage the rollout of a resource
  rolling-update Perform a rolling update of the given ReplicationController
  scale          Set a new size for a Deployment, ReplicaSet, Replication
Controller, or Job
  autoscale      Auto-scale a Deployment, ReplicaSet, or ReplicationController

Cluster Management Commands:
  certificate    Modify certificate resources.
  cluster-info   Display cluster info
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         Mark node as unschedulable
  uncordon       Mark node as schedulable
  drain          Drain node in preparation for maintenance
  taint          Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe       Show details of a specific resource or group of resources
  logs           Print the logs for a container in a pod
  attach         Attach to a running container
  exec           Execute a command in a container
  port-forward   Forward one or more local ports to a pod
  proxy          Run a proxy to the Kubernetes API server
  cp             Copy files and directories to and from containers.
  auth           Inspect authorization

Advanced Commands:
  apply          Apply a configuration to a resource by filename or stdin
  patch          Update field(s) of a resource using strategic merge patch
  replace        Replace a resource by filename or stdin
  convert        Convert config files between different API versions

Settings Commands:
  label          Update the labels on a resource
  annotate       Update the annotations on a resource
  completion     Output shell completion code for the specified shell (bash or
zsh)

Other Commands:
  api-versions   Print the supported API versions on the server, in the form of
"group/version"
  config         Modify kubeconfig files
  help           Help about any command
  plugin         Runs a command-line plugin
  version        Print the client and server version information

Use "kubectl  --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all
commands).

Step #3.Create new IAM User on AWS Console

Login to AWS Console, In the navigation pane, choose Users and then choose Add user to create new user with Programmatic access. This is the user kops would connect and store the cluster state information.

Alternatively, you can also create the IAM user from the command line using AWS CLI tools.

Create new user for kops
Image – Create new user for kops

Set following IAM permissions for the kops user & click on Next

  • AmazonEC2FullAccess
  • AmazonRoute53FullAccess
  • AmazonS3FullAccess
  • IAMFullAccess
  • AmazonVPCFullAccess
Set user permissions
Image – Set user permissions

Review permissions & Click Next

Review Permissions
Image – Review Permissions

User creation successful, record Access key ID & Secret access key. We would be using them in the next step.

User creation successful
Image – User creation successful

Run the below commands to set the environment variables

export AWS_ACCESS_KEY_ID=<replace with your access key>
export AWS_SECRET_ACCESS_KEY=<replace with your secret key>

Step #4.Create new s3 bucket to store cluster information

In order to store the state of your cluster, and the representation of your cluster, we need to create a dedicated S3 bucket for kops to use. This bucket will become the source of truth for our cluster configuration.

Create S3 Bucket to store cluster information
Image – Create S3 Bucket to store cluster information

Enter bucket name & click Next,Enable Versioning for production environment if in case you ever need to revert or recover a previous state store.

S3 Bucket created
Image – S3 Bucket created

To recap,we have installed kubectl,kops utilities,created new IAM user and s3 bucket to hold cluster information. We are now ready to create our 1st cluster.

Step #5.Create cluster

Run the below commands to set the environment variables

export NAME=k8cluster.k8s.local <replace appropriately>
export KOPS_STATE_STORE=s3://kubernetes-upnxt-aws-io <replace appropriately>

Here NAME indicates the cluster name, make sure it ends with k8s.local and KOPS_STATE_STORE is the s3 bucket location.

Set environment variables for Cluster name & State store
Image – Set environment variables for Cluster name & State store

Next, step is to create cluster configuration. Make sure that you have generated SSH key pair before creating the cluster.

kops create cluster \
    --zones us-west-2a \
    ${NAME}

Here zones refers to which region/zone you would want to create the cluster. This is the basic example of creating cluster, more examples can be found here.

kops create cluster command
Image – kops create cluster command

Once the cluster is created, you can use kubectl to view and interact with the cluster. As you can see, the cluster has been created with 1 master & 1 worker node.

kubectl get nodes to view the cluster
Image – kubectl get nodes to view the cluster

Congrats! our Kubernetes cluster is online and working.

Next,  if you’re planning to deploy or update the application, you can follow the same set of steps as described in the earlier posts.

As a last step, if you want to delete the cluster that we created above use below command.

kops delete cluster --name ${NAME} --yes

 

Like this post? Don’t forget to share it!

Additional Resources

Summary
Kubernetes tutorial - Create Kubernetes cluster on AWS with kops
Article Name
Kubernetes tutorial - Create Kubernetes cluster on AWS with kops
Description
kops (Kubernetes Operations) helps you create, destroy, upgrade and maintain production-grade, highly available, Kubernetes clusters from the command line.In this post we are going to look at how to provision Kubernetes cluster on AWS using kops utility.
Author
Publisher Name
upnxtblog
Publisher Logo

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Docker Previous post Docker tutorial – Create,Add Nodes,Deploy and Scale service on the Swarm
Next post Use AssertJ to improve your test code readability, maintenance of tests easier