AWS Builder Center

How to deploy a K8S cluster on AWS with kops

This article describe how to quickly deploy a workable K8S cluster on AWS.

Install the kubectl and kops binary on the machine

  1. Assume that you're using a Linux machine. Execute the following commands to install kubectl:
curl -Lo kubectl -s -L
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
  1. Execute the following commands to install kops:
curl -Lo kops -s | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64
chmod +x ./kops
sudo mv ./kops /usr/local/bin/


Setup the AWS environment on the machine

  1. Configure the aws env with the valid credentials:
aws configure
  1. Create a dedicated IAM user with the necessary permissions to kops:
aws iam create-group --group-name kops
aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonEC2FullAccess --group-name kops
aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonRoute53FullAccess --group-name kops
aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess --group-name kops
aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/IAMFullAccess --group-name kops
aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonVPCFullAccess --group-name kops
aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonSQSFullAccess --group-name kops
aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess --group-name kops
aws iam create-user --user-name kops
aws iam add-user-to-group --user-name kops --group-name kops
aws iam create-access-key --user-name kops
  1. Re-confugure the aws env with the newly created IAM users:
aws configure # Use your new access and secret key here
aws iam list-users # you should see a list of all your IAM users here
export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id)
export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key)


Create the S3 Buckets for the Cluster Store

  1. Create the s3 bucket for the cluster state store
aws s3api create-bucket \
--bucket my-kops-k8s-state-store \
--region us-east-1
  1. Enable the versioning of the bucket in case you ever need to revert or recover a previous state store.
aws s3api put-bucket-versioning --bucket my-kops-k8s-state-store --versioning-configuration Status=Enabled
  1. Create the bucket with the ACL as the cluster OIDC store
aws s3api create-bucket \
--bucket my-kops-k8s-oidc-store \
--region us-east-1 \
--object-ownership BucketOwnerPreferred
aws s3api put-public-access-block \
--bucket my-kops-k8s-oidc-store \
--public-access-block-configuration BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false
aws s3api put-bucket-acl \
--bucket my-kops-k8s-oidc-store \
--acl public-read
  1. Enable the encryption for the bucket as the cluster state store
aws s3api put-bucket-encryption --bucket my-kops-k8s-state-store --server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"AES256"}}]}'


Create your first K8S cluster with kops

  1. Export the env vars for your cluster
export NAME=mykopscluster.k8s.local
export KOPS_STATE_STORE=s3://my-kops-k8s-state-store

  1. Find the available AZ in the current region
aws ec2 describe-availability-zones --region us-east-1
  1. Create the k8s cluster with kops
kops create cluster \
--name=${NAME} \
--cloud=aws \
--zones=us-east-1a \
--discovery-store=s3://my-kops-k8s-oidc-store/${NAME}/discovery
  1. Confirm the final configuration to build the cluster
kops update cluster --name ${NAME} --yes --admin
  1. Validate the cluster status till it's ready.
kops validate cluster --wait 10m
  1. Check the node status
kubectl get nodes


  1. Check the system pod
kubectl -n kube-system get po




Cleanup

Delete the cluster simply with command below after it is used.
kops delete cluster --name ${NAME} --yes
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article