iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article

Building a Quick EKS Environment for Testing with Terraform

に公開

What is this?

I wrote some Terraform code to easily create and destroy clusters for verification purposes while learning Kubernetes and EKS on my own, so I'm sharing it here 🍥

When you run terraform apply, the EKS environment will be born in about 10–15 minutes 👶
The estimated cost while running idle is approximately 15-20 yen/h (EKS cluster 0.1 USD + EC2 instance 0.04 USD). However, as of writing this Zenn article, I'm building this environment in a state of "we have no idea what we're doing, we're just going with the vibe," so please excuse me if I'm mistaken.

The k8s version is 1.21, and two t3.small instances are created as nodes.
Other AWS resources are configured with the prefix study-k8s.

Additionally, the following are configured:

The following are required locally:

  • direnv
  • awscli
  • terraform
    • 1.0.5
  • kubectl

Setup Procedures

The complete set is stored in this repository:

https://github.com/yktakaha4/study-k8s-terraform

The steps for applying are as follows:

# Please prepare the ID/Secret for a user with AdministratorAccess
$ cp -p .envrc.sample .envrc
$ direnv allow

# By default, it creates a 172.17.0.0/16 VPC and performs various tasks inside it.
# If this overlaps with your existing setup, create the following file to overwrite it.
$ cp -p terraform.tfvars.sample terraform.tfvars

# If you want to use an S3 backend, uncomment the lines in the following file
$ ls -l terraform.tf

# Apply to the AWS environment
$ terraform init
$ terraform apply

...omitted...

Apply complete! Resources: 48 added, 0 changed, 0 destroyed.

Outputs:

cluster_name = "study-k8s-XXXXXXXX"

# Update / Verify connection settings
$ aws eks update-kubeconfig --name study-k8s-XXXXXXXX
$ kubectl config current-context
arn:aws:eks:ap-northeast-1:1234567890:cluster/study-k8s-XXXXXXXX

# If "eks" is present in the GitVersion of the Server Version, you are likely connected to EKS
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.1", GitCommit:"632ed300f2c34f6d6d15ca4cef3d3c7073412212", GitTreeState:"clean", BuildDate:"2021-08-19T15:45:37Z", GoVersion:"go1.16.7", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"21+", GitVersion:"v1.21.2-eks-0389ca3", GitCommit:"8a4e27b9d88142bbdd21b997b532eb6d493df6d2", GitTreeState:"clean", BuildDate:"2021-07-31T01:34:46Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}

$ kubectl get nodes
NAME                                              STATUS   ROLES    AGE     VERSION
ip-172-16-2-184.ap-northeast-1.compute.internal   Ready    <none>   4m32s   v1.21.2-eks-55daa9d
ip-172-16-3-4.ap-northeast-1.compute.internal     Ready    <none>   4m41s   v1.21.2-eks-55daa9d

$ kubectl get pods --all-namespaces
NAMESPACE     NAME                                            READY   STATUS    RESTARTS   AGE
kube-system   aws-load-balancer-controller-5fb99754b5-tjdmk   1/1     Running   0          5m54s
kube-system   aws-node-5t57d                                  1/1     Running   0          4m8s
kube-system   aws-node-w7k8d                                  1/1     Running   0          4m17s
kube-system   coredns-76f4967988-bm6ld                        1/1     Running   0          7m48s
kube-system   coredns-76f4967988-sq5p7                        1/1     Running   0          7m48s
kube-system   kube-proxy-7s87r                                1/1     Running   0          4m8s
kube-system   kube-proxy-z8ps8                                1/1     Running   0          4m17s
kube-system   metrics-server-569b4c5df9-4knbj                 1/1     Running   0          5m57s

Actually getting your hands dirty makes you feel like you've understood a lot, and it's fun! 🍠
(Whether I actually understood it is another matter)

# Delete it when you're done
$ terraform destroy

References

Here are some resources that I found helpful while creating this:

https://github.com/terraform-aws-modules/terraform-aws-eks/tree/master/examples/managed_node_groups

https://zenn.dev/yukin01/articles/6dd6542993ab166d51c2

Discussion