Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

AWS CLI EKS Commands: Create a Cluster and Connect kubectl

We create a managed Kubernetes cluster: roles, create-cluster, connect kubectl, add worker nodes and add-ons, give access and upgrade.

In this chapter

  • Create the cluster role
  • Create an EKS cluster
  • Connect kubectl
  • Add a managed node group
  • Access, add-ons and upgrades

1. The big picture: a housing society office

A society office decides who lives where and fixes problems, while flats are where people live. The EKS control plane is the office (AWS runs it); nodes are the flats where your pods live.

aws eks creates and manages the cluster. Inside it you use kubectl. update-kubeconfig connects the two.

Cluster workflowIAM cluster role + node roleeks create-cluster (10–15 min)update-kubeconfigcreate-nodegroupkubectl get nodes
The control plane costs per hour even with no nodes.

2. Roles

EKS and the worker nodes must call AWS for you: ENIs, load balancers, image pulls.

The cluster role trusts eks.amazonaws.com with AmazonEKSClusterPolicy. The node role trusts ec2.amazonaws.com with worker, CNI and ECR read policies.

  1. Create the cluster role.
aws iam create-role --role-name cli-eks-cluster --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"eks.amazonaws.com"},"Action":"sts:AssumeRole"}]}' --query Role.Arn --output text
aws iam attach-role-policy --role-name cli-eks-cluster --policy-arn arn:aws:iam::aws:policy/AmazonEKSClusterPolicy

Expected output:

arn:aws:iam::111122223333:role/cli-eks-cluster
  1. Create the node role.
aws iam create-role --role-name cli-eks-node --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}' --query Role.Arn --output text
for p in AmazonEKSWorkerNodePolicy AmazonEKS_CNI_Policy AmazonEC2ContainerRegistryReadOnly; do aws iam attach-role-policy --role-name cli-eks-node --policy-arn arn:aws:iam::aws:policy/$p; done

Expected output:

arn:aws:iam::111122223333:role/cli-eks-node

3. Create the cluster and connect

Before creating a cluster, select a version currently in standard support. aws eks describe-cluster-versions lists supported versions and their status. Set EKS_LAB_VERSION to your selected version before running the create command. If practising the upgrade section, set EKS_LAB_NEXT_VERSION to the next supported minor version and verify node and add-on compatibility. Both EKS and its worker resources can incur ongoing charges.

aws eks update-kubeconfig writes the cluster connection details and authentication command that kubectl uses. A kubeconfig context selects a cluster and user configuration. First confirm the intended context and AWS caller. When kubectl get nodes succeeds, the network path and that operation’s Kubernetes authorization worked. An empty list can mean no nodes have joined yet.

Distinguish three failures before retrying. A timeout points toward the cluster endpoint and network. An authenticated but forbidden response points toward the Kubernetes access configuration. A node that appears but is NotReady joined and still needs diagnosis. Creating the EKS control plane does not by itself launch a worker for the application.

You want Kubernetes without running its masters yourself.

create-cluster needs a version, role and at least two subnets in different AZs. update-kubeconfig writes ~/.kube/config.

  1. Create it and wait.
aws eks create-cluster --name cli-eks --kubernetes-version "${EKS_LAB_VERSION:?Set a standard-support EKS version}" --role-arn arn:aws:iam::111122223333:role/cli-eks-cluster --resources-vpc-config subnetIds=subnet-0aaa111,subnet-0bbb222 --access-config authenticationMode=API --query cluster.status --output text
aws eks wait cluster-active --name cli-eks

Expected output:

CREATING
  1. Connect kubectl.
aws eks update-kubeconfig --name cli-eks --region ap-south-1
kubectl get svc

Expected output:

Added new context arn:aws:eks:ap-south-1:111122223333:cluster/cli-eks to /home/you/.kube/config
NAME         TYPE        CLUSTER-IP   PORT(S)
kubernetes   ClusterIP   10.100.0.1   443/TCP

4. Managed node group

Use this chapter’s names to follow the handoff. The EKS cluster cli-eks owns the Kubernetes control plane. The managed node group ng1 creates the EC2 worker capacity for Pods. Creating the cluster API and updating kubeconfig prepares an operator connection; it does not supply those workers. Think of the higher-level Kubernetes scheduler as assigning a workload to machines that actually register.

After the node-group creation returns a starting state, run the node-group waiter. Once AWS reports the group active, use the correct context to check the Kubernetes nodes. A listed node with status Ready has joined and can usually accept eligible work. A listed node with NotReady has a reported problem. If you expected two but see one, compare the group’s desired count and health with what Kubernetes lists. These checks help tell an incomplete EC2 launch from a join or readiness issue.

Pods need machines to run on.

create-nodegroup starts EC2 instances in an Auto Scaling group that AWS patches and replaces. scaling-config sets min, max and desired.

  1. Create the node group.
aws eks create-nodegroup --cluster-name cli-eks --nodegroup-name ng1 --node-role arn:aws:iam::111122223333:role/cli-eks-node --subnets subnet-0aaa111 subnet-0bbb222 --instance-types t3.medium --scaling-config minSize=1,maxSize=3,desiredSize=2 --query nodegroup.status --output text
aws eks wait nodegroup-active --cluster-name cli-eks --nodegroup-name ng1

Expected output:

CREATING
  1. See nodes.
kubectl get nodes

Expected output:

NAME                                        STATUS   VERSION
ip-10-0-1-23.ap-south-1.compute.internal   Ready    v<node-version>-eks
ip-10-0-2-45.ap-south-1.compute.internal   Ready    v<node-version>-eks
  1. Scale it.
aws eks update-nodegroup-config --cluster-name cli-eks --nodegroup-name ng1 --scaling-config desiredSize=3 --query update.status --output text

Expected output:

InProgress

5. Access, add-ons and upgrades

A teammate needs kubectl access, the cluster needs core add-ons and versions go out of support.

Access entries map an IAM role to cluster permissions. Add-ons like vpc-cni and coredns are managed by AWS. update-cluster-version upgrades one minor version at a time.

  1. Give a role admin access.
aws eks create-access-entry --cluster-name cli-eks --principal-arn arn:aws:iam::111122223333:role/DevOps
aws eks associate-access-policy --cluster-name cli-eks --principal-arn arn:aws:iam::111122223333:role/DevOps --policy-arn arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy --access-scope type=cluster --query associatedAccessPolicy.policyArn --output text

Expected output:

arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy
  1. List add-ons.
aws eks list-addons --cluster-name cli-eks --output text

Expected output:

ADDONS  coredns
ADDONS  kube-proxy
ADDONS  vpc-cni
  1. Upgrade the control plane.
aws eks update-cluster-version --name cli-eks --kubernetes-version "${EKS_LAB_NEXT_VERSION:?Set the next supported minor version}" --query update.status --output text

Expected output:

InProgress

6. Same action in Python, Node.js, Java, Go, .NET and PHP

Scripts start in the CLI. Apps call the same API through an SDK. Knowing both helps you move a tested command into real code.

Low-level CLI commands expose service operations. A high-level command or an automatically paginated list can make multiple requests. For example, aws eks describe-cluster --name cli-eks maps to the Describe cluster API.

Same action (Describe cluster) from code. Set the intended profile and Region for the SDK. Provider support and precedence can differ by SDK, and an explicit client setting can override defaults.

StackCode
Python (boto3)boto3.client("eks").describe_cluster(name="cli-eks")["cluster"]["status"]
Node.js (v3)await eks.send(new DescribeClusterCommand({name:"cli-eks"}));
Java (v2)eks.describeCluster(r -> r.name("cli-eks")).cluster().status();
Go (v2)c.DescribeCluster(ctx, &eks.DescribeClusterInput{Name: aws.String("cli-eks")})
.NETawait eks.DescribeClusterAsync(new DescribeClusterRequest{Name="cli-eks"});
PHP$eks->describeCluster(["name"=>"cli-eks"]);

7. Clean up so nothing keeps costing

Lab resources left running cost money and can be a security risk.

Delete in reverse order: children first, then parents.

  1. Delete the node group first.
aws eks delete-nodegroup --cluster-name cli-eks --nodegroup-name ng1
aws eks wait nodegroup-deleted --cluster-name cli-eks --nodegroup-name ng1

Expected output:

(no output means success)
  1. Delete the cluster.
aws eks delete-cluster --name cli-eks --query cluster.status --output text
aws eks wait cluster-deleted --name cli-eks

Expected output:

DELETING
  1. Delete the roles (detach policies first).
for r in cli-eks-cluster cli-eks-node; do for p in $(aws iam list-attached-role-policies --role-name $r --query "AttachedPolicies[].PolicyArn" --output text); do aws iam detach-role-policy --role-name $r --policy-arn $p; done; aws iam delete-role --role-name $r; done

Expected output:

(no output means success)
  1. Verify.
aws eks list-clusters --query clusters --output text

Expected output:

(no output means success)

Ravindra Bagale's Tip

kubectl says "You must be logged in to the server (Unauthorized)" when your IAM identity has no access entry. Run aws sts get-caller-identity and add an access entry for that ARN.

8. Interview questions and answers

Q. What does AWS manage in EKS?

The control plane: API servers and etcd across AZs.

Q. What does update-kubeconfig do?

Adds a context to ~/.kube/config that gets tokens via aws eks get-token.

Q. Managed node group vs Fargate?

Node groups are EC2 you see and pay for; Fargate runs each pod serverless.

Q. How do you grant cluster access?

Access entries plus access policies (or the old aws-auth ConfigMap).

Q. Upgrade order?

Control plane first, then add-ons, then node groups, one minor version at a time.

Try at home

  1. Deploy nginx with kubectl and expose it with a LoadBalancer service.
  2. Scale the node group to 3 and back to 1.

In short

  1. Two roles.
  2. create-cluster + wait.
  3. update-kubeconfig.
  4. create-nodegroup.
  5. Delete nodegroup before cluster.

Verify identity before scheduling a pod

After writing kubeconfig, check the selected context and the AWS identity used for the cluster. Run an approved cluster status command before trying a lab deployment. Delete lab node or Fargate resources and the cluster per their dependencies; check for load balancers provisioned by Services.

Interview check Why can DescribeCluster succeed but kubectl be denied?

DescribeCluster uses IAM API permissions. The Kubernetes endpoint also checks cluster authentication and authorization.

Reference documentation

Practise this chapter

Interview questions and answers

How do you separate EKS connectivity, authorization and worker readiness?

I would verify the kubeconfig context, caller identity and network path first. If the API responds but denies access, I would check the Kubernetes authorization for the exact action. Once I can list nodes, I would verify which nodes joined and whether they are Ready.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. What does update-kubeconfig configure?

Answer and explanation

A. The kubectl cluster connection and authentication settings

It configures the client connection. Worker capacity and workload deployment are separate.

2. A kubectl request was authenticated but received Forbidden. What should you check?

Answer and explanation

C. Kubernetes authorization for the chosen principal and action

Forbidden points to permissions for that API action rather than application code.

3. The control plane is active and get nodes returns an empty list. What should you investigate?

Answer and explanation

B. Whether worker nodes exist and have joined

The Kubernetes API can respond before a node group joins and supplies capacity.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

London Legal LLP is a law business in London. Oliver Hughes looks after its IT and cloud work. They count money in GBP and run their AWS resources in the eu-west-2 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 10

Easy Describe an EKS cluster

Check the version, status and endpoint of the cluster.

Describe cluster londonlegal-eks.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy How many addresses in a /16?

Picking a VPC size needs this count.

London Legal LLP plans the VPC 10.49.0.0/16. How many IP addresses does it hold in total?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Port for SSH

Security group rules need the right port.

Which TCP port must the security group open for SSH?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Show where you are

Before you copy or delete anything, you must know which folder you are in.

Oliver Hughes logged in to the London Legal LLP server and is not sure which folder the terminal is in. Print the current folder path.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Kubernetes Deployment

A Deployment keeps the right number of pods running.

Write a Deployment named londonlegal-web with 3 replicas running image nginx:1.27.

  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Medium Connect kubectl to EKS

update-kubeconfig writes the cluster details so kubectl can talk to it.

Update your kubeconfig for cluster londonlegal-eks in eu-west-2.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Kubernetes LoadBalancer Service

A Service of type LoadBalancer gets an AWS load balancer.

Write a Service of type LoadBalancer selecting app: web, port 80 to targetPort 8080.

  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Medium Usable IPs in an AWS /24 subnet

AWS keeps 5 addresses in every subnet.

How many addresses can London Legal LLP use for servers in subnet 10.49.1.0/24 on AWS?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Create an EKS cluster

The control plane needs a role and subnets to place its network interfaces.

Create cluster londonlegal-eks with role arn:aws:iam::114543297314:role/eksClusterRole and subnets subnet-0145432db314bb1,subnet-045432db314bb2.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Add a managed node group

Node groups are the EC2 servers that run your pods.

Create node group ng-1 in cluster londonlegal-eks with role arn:aws:iam::114543297314:role/eksNodeRole, subnets subnet-0145432db314bb1 subnet-045432db314bb2, scaling minSize=1,maxSize=3,desiredSize=2.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

EKS cluster. AWS runs the office; you run the flats. Cluster role trusts eks.amazonaws.com. create-cluster, then wait cluster-active. Connect: update-kubeconfig, kubectl get svc.
Revision card: EKS cluster · Download PNG · Open card page
EKS node group. Pods need machines. Node role: worker, CNI, ECR read. create-nodegroup with scaling-config. Delete node groups before the cluster.
Revision card: EKS node group · Download PNG · Open card page
Managed node group vs Fargate profile. EC2 you can see. / No nodes to see.
Comparison: Managed node group vs Fargate profile · Download PNG · Open card page
kubectl Unauthorized?. Your IAM identity has no access entry. Check with sts get-caller-identity. create-access-entry + associate-access-policy.
Interview card: kubectl Unauthorized? · Download PNG · Open card page

More cards: Ravi Sir's Cards.