Difference between revisions of "Deploy EKS cluster using Terraform"

From wikieduonline
Jump to navigation Jump to search
 
(44 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
* Code: https://github.com/hashicorp/learn-terraform-provision-eks-cluster
 +
 +
* Terraform EKS module examples: https://github.com/terraform-aws-modules/terraform-aws-eks/tree/master/examples
 +
** [[Terraform EKS module Karpenter example]]: https://github.com/terraform-aws-modules/terraform-aws-eks/tree/master/examples/karpenter
 +
 
* [[Terraform EKS module]]:  
 
* [[Terraform EKS module]]:  
 
** https://registry.terraform.io/modules/terraform-aws-modules/eks/aws/latest
 
** https://registry.terraform.io/modules/terraform-aws-modules/eks/aws/latest
Line 5: Line 10:
 
*** <code>[[aws_eks_cluster]]</code> ([[Terraform EKS apply output]], <code>[[terraform show (grep resource) in EKS|terraform show]]</code> | <code>grep [[resource]]</code>)
 
*** <code>[[aws_eks_cluster]]</code> ([[Terraform EKS apply output]], <code>[[terraform show (grep resource) in EKS|terraform show]]</code> | <code>grep [[resource]]</code>)
 
*** <code>[[aws_eks_addon]]</code>
 
*** <code>[[aws_eks_addon]]</code>
** Examples: https://github.com/terraform-aws-modules/terraform-aws-eks/tree/master/examples
+
** [[Terraform eks managed node groups]]: <code>[[Terraform eks managed node groups: instance types|instance_types]]</code>
 +
 
  
 
== Example ==
 
== Example ==
Line 12: Line 18:
 
:<code>[[git clone]] https://github.com/hashicorp/learn-terraform-provision-eks-cluster && [[cd learn-terraform-provision-eks-cluster]]</code>
 
:<code>[[git clone]] https://github.com/hashicorp/learn-terraform-provision-eks-cluster && [[cd learn-terraform-provision-eks-cluster]]</code>
  
1) Execute (<code>[[tf login]]</code>)
+
2) Change parameters
[[export]] [[TF_CLOUD_ORGANIZATION]]=test
+
* Change <code>[[name]]</code> in [[main.tf ( formerly eks-cluster.tf)|main.tf]] ([[Terraform module: vpc]] section) to your prefered VPC name
+
* Change cluster_name in <code>[[main.tf ( formerly eks-cluster.tf)|main.tf]]</code>
Change [[TFC execution mode]] to [[local]] in TFC web portal
+
 
Change [[name]] in [[Terraform module: vpc]] to your prefer VPC name
+
3) Execute:
Configure your [[AWS profile]] name in [[main.tf]]  
+
* Create TFC account with mail or [[GitHub Account]] and create an [[Organization]] / Project / Workspace
 +
* <code>[[tf login]]</code> and get an authentication token ([[~/.terraform.d/credentials.tfrc.json]])
 +
* Change [[TFC execution mode]] to [[local]] for your workspace in [[TFC web portal]]
 +
 
 +
4)
 +
* Configure your [[AWS profile]] name in [[~/.aws/config]] to do not use default or make your working profile the default one.
 +
* <code>[[export]] [[TF_CLOUD_ORGANIZATION]]=your_organization_name</code>
  
 +
Change  your [[terraform.tf]] file with values from your new TFC account:
 
  [[terraform init]] #It may take several minutes
 
  [[terraform init]] #It may take several minutes
 
  [[terraform plan]]
 
  [[terraform plan]]
Line 26: Line 39:
 
  one liner: export [[TF_CLOUD_ORGANIZATION]]=test && [[tf init]] && [[tf plan]] && [[tf apply]]
 
  one liner: export [[TF_CLOUD_ORGANIZATION]]=test && [[tf init]] && [[tf plan]] && [[tf apply]]
  
2) Update configuration
+
5) Update configuration
 
  Update [[kubeconfig]] with credentials:
 
  Update [[kubeconfig]] with credentials:
  [[aws eks]] --region $([[terraform output -raw region]]) [[update-kubeconfig]] --name $([[terraform output -raw]] cluster_name)
+
  [[aws eks]] --region $([[terraform output -raw region]]) [[update-kubeconfig]] --name $([[terraform output -raw]] cluster_name) [[--profile]] XXXX
 
   
 
   
 
  [[Added new context]] arn:aws:eks:us-east-2:XXXXXXX:cluster/myeks-eks-xxxx to /Users/youruser/[[.kube/config]]
 
  [[Added new context]] arn:aws:eks:us-east-2:XXXXXXX:cluster/myeks-eks-xxxx to /Users/youruser/[[.kube/config]]
  
2.1) Review EKS cluster created
+
7) Upgrade cluster to the lastest version: <code>[[cluster_version]]</code>
 +
 
 +
7.1) Review EKS cluster created
 
  [[kubectl get nodes]]
 
  [[kubectl get nodes]]
 
  NAME                                      STATUS  ROLES    AGE    VERSION
 
  NAME                                      STATUS  ROLES    AGE    VERSION
Line 41: Line 56:
 
3) Review [[resources]]:
 
3) Review [[resources]]:
 
  <code>[[terraform show (EKS using Terraform)|terraform show]] | grep -w resource | sort | uniq</code>
 
  <code>[[terraform show (EKS using Terraform)|terraform show]] | grep -w resource | sort | uniq</code>
 +
<code>[[terraform show (EKS using Terraform)|terraform show]] | grep -w resource | sort | uniq | [[wc -l]]</code>
 +
34
 +
 +
2025 execution:
 +
<code>[[terraform show (EKS using Terraform)|terraform show]] | grep -w resource | sort | uniq | [[wc -l]]</code>
 +
37
  
 
4) Configure <code>[[kubectl]]</code>
 
4) Configure <code>[[kubectl]]</code>
Line 48: Line 69:
 
5) Review [[cluster status]]/configuration
 
5) Review [[cluster status]]/configuration
 
  [[kubectl cluster-info]]
 
  [[kubectl cluster-info]]
 +
[[kubectl cluster-info dump]]
 
  [[kubectl get all -A]]
 
  [[kubectl get all -A]]
 
  [[Default EKS installation]]
 
  [[Default EKS installation]]
Line 58: Line 80:
 
* <code>[[Error: failed to create backend alias to target]] "". The hostname is not in the correct format.</code>
 
* <code>[[Error: failed to create backend alias to target]] "". The hostname is not in the correct format.</code>
 
* <code>[[Error: Invalid or missing required argument]] .../... [[TF_CLOUD_ORGANIZATION]]</code>
 
* <code>[[Error: Invalid or missing required argument]] .../... [[TF_CLOUD_ORGANIZATION]]</code>
 +
* [[Unable to parse config file]] ~/.aws/config
 +
* [[Error: Retrieving AWS account details: validating provider credentials: retrieving caller identity from STS: operation error STS: GetCallerIdentity, https response error StatusCode: 403]]
  
 
== Related ==
 
== Related ==
 +
* [[Addons]]: <code>[[eks-pod-identity-agent]]</code>, <code>[[aws-efs-csi-driver]], [[amazon-cloudwatch-observability]]</code>
 
* <code>[[helm install]]</code>
 
* <code>[[helm install]]</code>
 
* <code>[[aws eks update-kubeconfig --name my-eks-cluster-name]]</code>
 
* <code>[[aws eks update-kubeconfig --name my-eks-cluster-name]]</code>
Line 70: Line 95:
 
* <code>[[aws eks create-cluster]]</code>
 
* <code>[[aws eks create-cluster]]</code>
 
* <code>[[asg_desired_capacity]]</code>, <code>[[InstanceType]]</code>
 
* <code>[[asg_desired_capacity]]</code>, <code>[[InstanceType]]</code>
* [[Terraform eks managed node groups: instance types]]
 
 
* [[K8s installation]]: <code>[[eksctl create cluster]]</code>
 
* [[K8s installation]]: <code>[[eksctl create cluster]]</code>
 
* [[Deploy GKE cluster using Terraform]]
 
* [[Deploy GKE cluster using Terraform]]
Line 77: Line 101:
 
* <code>[[node_groups]]</code>
 
* <code>[[node_groups]]</code>
 
* [[terraform.tf]]
 
* [[terraform.tf]]
 +
* [[Apply complete! Resources: 63 added, 0 changed, 0 destroyed.]]
 +
* [[Terraform destroy]]:
 +
** <code>[[Warning: EC2 Default Network ACL]]</code>
 +
** <code>[[Warning: Helm uninstall returned an information message]]</code>
  
 
== Activities ==
 
== Activities ==
 
* Review https://learn.hashicorp.com/tutorials/terraform/eks
 
* Review https://learn.hashicorp.com/tutorials/terraform/eks
 
* [[Create your first EKS Cluster using AWS Management Console]]
 
* [[Create your first EKS Cluster using AWS Management Console]]
 +
* [[Stop all nodes in your EKS cluster]]
 +
* [[Helm install nginx-ingress]]
  
 
== See also ==
 
== See also ==

Latest revision as of 10:22, 7 February 2025


Example[edit]

0) Download code

git clone https://github.com/hashicorp/learn-terraform-provision-eks-cluster && cd learn-terraform-provision-eks-cluster

2) Change parameters

3) Execute:

4)

Change  your terraform.tf file with values from your new TFC account: 
terraform init #It may take several minutes
terraform plan
terraform apply: Terraform EKS apply output
It may take around 15 min
one liner: export TF_CLOUD_ORGANIZATION=test && tf init && tf plan && tf apply

5) Update configuration

Update kubeconfig with credentials:
aws eks --region $(terraform output -raw region) update-kubeconfig --name $(terraform output -raw cluster_name) --profile XXXX

Added new context arn:aws:eks:us-east-2:XXXXXXX:cluster/myeks-eks-xxxx to /Users/youruser/.kube/config

7) Upgrade cluster to the lastest version: cluster_version

7.1) Review EKS cluster created

kubectl get nodes
NAME                                       STATUS   ROLES    AGE     VERSION
ip-10-0-1-29.us-east-2.compute.internal    Ready    <none>   5m31s   v1.23.9-eks-ba74326
ip-10-0-1-47.us-east-2.compute.internal    Ready    <none>   5m21s   v1.23.9-eks-ba74326
ip-10-0-2-121.us-east-2.compute.internal   Ready    <none>   5m13s   v1.23.9-eks-ba74326

3) Review resources:

terraform show | grep -w resource | sort | uniq
terraform show | grep -w resource | sort | uniq | wc -l
34
2025 execution: 
terraform show | grep -w resource | sort | uniq | wc -l
37

4) Configure kubectl

aws eks list-clusters --output text | awk '{print $2}'
aws eks update-kubeconfig --name my-eks-cluster-name

5) Review cluster status/configuration

kubectl cluster-info
kubectl cluster-info dump
kubectl get all -A
Default EKS installation

6) Install your desired addons:

helm install

Errors[edit]

Related[edit]

Activities[edit]

See also[edit]

Advertising: