{"id":144,"date":"2024-05-29T00:02:09","date_gmt":"2024-05-29T07:02:09","guid":{"rendered":"http:\/\/184.72.63.26\/?p=144"},"modified":"2024-10-01T14:23:07","modified_gmt":"2024-10-01T21:23:07","slug":"provision-an-eks-cluster-using-terraform","status":"publish","type":"post","link":"https:\/\/www.wallacel.com\/index.php\/2024\/05\/29\/provision-an-eks-cluster-using-terraform\/","title":{"rendered":"Provisioning an EKS cluster using Terraform"},"content":{"rendered":"\n<p>In this exercise, I will deploy an AWS Elastic Kubernetes Service (EKS) cluster using Terraform and configure kubectl to verify the cluster is ready to use. Kubernetes is an open-source container orchestration system used to manage and deploy containerized applications. A Kubernetes cluster is a collection of nodes that run containerized applications. With AWS EKS, I can run my Kubernetes applications on AWS using their Elastic Compute Cloud (EC2) and take advantage of its scalability and availability using services such as application load balancers (ALB) and auto scaling group (ASG).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing Terraform Configuration Files<\/h2>\n\n\n\n<p>This configuration will creates a VPC to provision an EKS cluster with the following architecture:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"581\" height=\"593\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/05\/eks-2.png\" alt=\"\" class=\"wp-image-177\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/eks-2.png 581w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/eks-2-294x300.png 294w\" sizes=\"auto, (max-width: 581px) 100vw, 581px\" \/><\/figure>\n\n\n\n<p>A vpc is created with three public subnets and three private subnets across two availability zones:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"yaml\" class=\"language-yaml\">module \"vpc\" {\n    source = \"terraform-aws-modules\/vpc\/aws\"\n    version = \"3.14.2\"\n\n    name = \"terraform-eks-vpc\"\n\n    cidr = \"10.0.0.0\/16\"\n    azs = slice(data.aws_availability_zones.available.names, 0, 3)\n\n    private_subnets = [\"10.0.1.0\/24\", \"10.0.2.0\/24\", \"10.0.3.0\/24\"]\n    public_subnets = [\"10.0.4.0\/24\", \"10.0.5.0\/24\", \"10.0.6.0\/24\"]<\/code><\/pre>\n\n\n\n<p>The EKS cluster will contain three nodes distributed across two node groups, with each compute node being of t2.micro type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"yaml\" class=\"language-yaml\">    eks_managed_node_groups = {\n        one = {\n            name = \"node-group-1\"\n            instance_types = [\"t2.micro\"]\n            min_size = 1\n            max_size = 3\n            desired_size = 2\n        }\n\n        two = {\n            name = \"node-group-2\"\n            instance_types = [\"t2.micro\"]\n            min_size = 1\n            max_size = 3\n            desired_size = 2\n        }\n    }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Running the Terraform Code<\/h2>\n\n\n\n<p>A terraform workflow consists of four main steps:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"460\" height=\"151\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/05\/workflow.png\" alt=\"\" class=\"wp-image-147\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/workflow.png 460w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/workflow-300x98.png 300w\" sizes=\"auto, (max-width: 460px) 100vw, 460px\" \/><\/figure>\n\n\n\n<p><strong>Init<\/strong> &#8211; Prepare the workspace and download the required modules and providers so Terraform can apply the configuration I have defined in various configuration files, run this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ terraform init<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"377\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/05\/init-1-1024x377.png\" alt=\"\" class=\"wp-image-178\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/init-1-1024x377.png 1024w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/init-1-300x110.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/init-1-768x283.png 768w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/init-1.png 1068w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p> <strong>Validate<\/strong> &#8211; Terraform will verify the syntax of your terraform configuration files<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ terraform validate<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"40\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/05\/validate-1-1024x40.png\" alt=\"\" class=\"wp-image-179\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/validate-1-1024x40.png 1024w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/validate-1-300x12.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/validate-1-768x30.png 768w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/validate-1.png 1045w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Plan<\/strong> &#8211; It allows you to preview the changes Terraform will make before apply them<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"414\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/05\/plan-1024x414.png\" alt=\"\" class=\"wp-image-180\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/plan-1024x414.png 1024w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/plan-300x121.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/plan-768x310.png 768w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/plan.png 1054w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Apply<\/strong> &#8211; Terraform will deploy the changes by creating or updating your resources in AWS<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ terraform apply<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"848\" height=\"428\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/05\/apply-2.png\" alt=\"\" class=\"wp-image-181\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/apply-2.png 848w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/apply-2-300x151.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/apply-2-768x388.png 768w\" sizes=\"auto, (max-width: 848px) 100vw, 848px\" \/><\/figure>\n\n\n\n<p>It takes about 10-15 minutes to create the cluster. When it is completed, I can see my EKS cluster in AWS console.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"748\" height=\"243\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/05\/aws_eks.png\" alt=\"\" class=\"wp-image-154\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/aws_eks.png 748w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/aws_eks-300x97.png 300w\" sizes=\"auto, (max-width: 748px) 100vw, 748px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Configure KubeCTL<\/h2>\n\n\n\n<p>Kubernetes provides a command line tool called <strong>kubectl<\/strong> for communicating with a Kubernetes cluster&#8217;s control plane, using the Kubernetes API. To use it first we need to a configuration file kubeconfig:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ aws eks update-kubeconfig --region region_name --name cluster_name<\/code><\/pre>\n\n\n\n<p>To validate and test my configuration to the master node:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$kubectl get svc\nNAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE\nkubernetes   ClusterIP   172.20.0.1   &lt;none&gt;        443\/TCP   5h39m<\/code><\/pre>\n\n\n\n<p>To view the status of my nodes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$kubectl get nodes custom-columns=Name:.metadata.name,nCPU:.status.capacity.cpu,Memory:.status.capacity.memory\n    Name                                            nCPU   Memory\n    ip-10-0-1-150.ca-central-1.compute.internal     1      2031268Ki\n    ip-10-0-1-189.ca-central-2.compute.internal     1      2031268Ki\n    ip-10-0-2-241.ca-central-2.compute.internal     1      2031268Ki\n    ip-10-0-2-172.ca-central-2.compute.internal     1      2031268Ki<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Destroy the EKS Cluster<\/h2>\n\n\n\n<p>To destroy the whole infrastructure that Terraform has created, simply use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ terraform destroy<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"406\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/05\/destroy-1-1024x406.png\" alt=\"\" class=\"wp-image-183\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/destroy-1-1024x406.png 1024w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/destroy-1-300x119.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/destroy-1-768x305.png 768w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/05\/destroy-1.png 1066w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You can find my configurations at <a href=\"https:\/\/github.com\/wallylee99\/EKS-with-Terraform\" data-type=\"link\" data-id=\"https:\/\/github.com\/wallylee99\/EKS-with-Terraform\">GitHub<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this exercise, I will deploy an AWS Elastic Kubernetes Service (EKS) cluster using Terraform and configure kubectl to verify the cluster is ready to use. Kubernetes is an open-source container orchestration system used to manage and deploy containerized applications. A Kubernetes cluster is a collection of nodes that run containerized applications. With AWS EKS, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":169,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[19,23,22],"class_list":["post-144","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","tag-aws","tag-kubernetes","tag-terraform"],"_links":{"self":[{"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/posts\/144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/comments?post=144"}],"version-history":[{"count":23,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":514,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/posts\/144\/revisions\/514"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/media\/169"}],"wp:attachment":[{"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}