Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
    • Helm Chart
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log & Monitor
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[AWS] Create EKS Cluster and EKS Node Groups in Public and Private Subnets

Posted on August 21, 2022September 20, 2022 By nim No Comments on [AWS] Create EKS Cluster and EKS Node Groups in Public and Private Subnets

Bài này bạn sẽ cũng mình providing 1 cluster EKS or K8s on AWS bằng terraform
Mình sẽ không thể giải thích từng line.
nhưng chỗ nào mình thấy khó hiểu mình sẽ note lại

Contents

  • 1) Overview
  • 2) Using terraform to provide a EKS cluster.
    • Create keypair or ssh-key on AWS
    • Verify EKS Cluster from AWS Mgmt Console
    • Install kubectl, configure kubeconfig and verify
  • 3) Understand EKS Cluster Network Interfaces
  • 4) EKS Security Groups
    • – SecurityGroup Cluster
    • – SecurityGroup Node

1) Overview

Đây là mô hình của chúng ta.

2) Using terraform to provide a EKS cluster.

Bài này thì chúng ta sẽ tạo 1 cluster EKS Public
Endpoint của Node controller sẽ public và các node worker sẽ access vào controller qua IP Public
Bạn sẽ thao khảo code ở link bên dưới!
https://github.com/mrnim94/terraform-aws/tree/master/eks/AWS-EKS-Cluster-Basics

Đầu tiên chúng ta đi soi variable:

cluster_service_ipv4_cidr –> service_ipv4_cidr: chỗ này thì bạn dùng để khai báo IP service cho cluster k8s
trong resource aws_eks_cluster có key version thì variable cluster_version: để quy định cluster k8s sẽ có version là bao nhiêu
==> nếu để null thì chúng ta có thể edit bằng file có đuôi là .tfvars

chỗ này thì để tạo cluster EKS vào kiểu public enpoint controller.

endpoint_public_access  = var.cluster_endpoint_public_access
==> (Optional) Whether the Amazon EKS public API server endpoint is enabled. Default is

Phần này thì là cho phép tất cả các IP access vào eks.

Tìm hiểu về IAM role cho eks cluster

chúng ta kiềm hiểu 2 policy này trước.

Cái này để EKS cluster sẽ được create network trong vpc.
# Create IAM Role
resource "aws_iam_role" "eks_master_role" {
  name = "${local.name}-eks-master-role"

  assume_role_policy = <<POLICY
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "eks.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
POLICY
}

# Associate IAM Policy to IAM Role
resource "aws_iam_role_policy_attachment" "eks-AmazonEKSClusterPolicy" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
  role       = aws_iam_role.eks_master_role.name
}

resource "aws_iam_role_policy_attachment" "eks-AmazonEKSVPCResourceController" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKSVPCResourceController"
  role       = aws_iam_role.eks_master_role.name
}

/*
# Optionally, enable Security Groups for Pods
# Reference: https://docs.aws.amazon.com/eks/latest/userguide/security-groups-for-pods.html
resource "aws_iam_role_policy_attachment" "eks-AmazonEKSVPCResourceController" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKSVPCResourceController"
  role       = aws_iam_role.eks_master_role.name
}
*/

Tiếp đến là chúng ta có 1 file c5-04-iamrole-for-eks-nodegroup.tf

# IAM Role for EKS Node Group 
resource "aws_iam_role" "eks_nodegroup_role" {
  name = "${local.name}-eks-nodegroup-role"

  assume_role_policy = jsonencode({
    Statement = [{
      Action = "sts:AssumeRole"
      Effect = "Allow"
      Principal = {
        Service = "ec2.amazonaws.com"
      }
    }]
    Version = "2012-10-17"
  })
}

resource "aws_iam_role_policy_attachment" "eks-AmazonEKSWorkerNodePolicy" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
  role       = aws_iam_role.eks_nodegroup_role.name
}

resource "aws_iam_role_policy_attachment" "eks-AmazonEKS_CNI_Policy" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
  role       = aws_iam_role.eks_nodegroup_role.name
}

resource "aws_iam_role_policy_attachment" "eks-AmazonEC2ContainerRegistryReadOnly" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
  role       = aws_iam_role.eks_nodegroup_role.name
}

c5-06-eks-cluster.tf giờ chúng ta thực hiện tại EKS cluster.

# Create AWS EKS Cluster
resource "aws_eks_cluster" "eks_cluster" {
  name     = "${local.name}-${var.cluster_name}"
  role_arn = aws_iam_role.eks_master_role.arn
  version = var.cluster_version

  vpc_config {
    subnet_ids = module.vpc.public_subnets
    endpoint_private_access = var.cluster_endpoint_private_access
    endpoint_public_access  = var.cluster_endpoint_public_access
    public_access_cidrs     = var.cluster_endpoint_public_access_cidrs    
  }

  kubernetes_network_config {
    service_ipv4_cidr = var.cluster_service_ipv4_cidr
  }
  
  # Enable EKS Cluster Control Plane Logging
  enabled_cluster_log_types = ["api", "audit", "authenticator", "controllerManager", "scheduler"]

  # Ensure that IAM Role permissions are created before and deleted after EKS Cluster handling.
  # Otherwise, EKS will not be able to properly delete EKS managed EC2 infrastructure such as Security Groups.
  depends_on = [
    aws_iam_role_policy_attachment.eks-AmazonEKSClusterPolicy,
    aws_iam_role_policy_attachment.eks-AmazonEKSVPCResourceController,
  ]
}

Create keypair or ssh-key on AWS

Ở line code này
https://github.com/mrnim94/terraform-aws/blob/master/eks/AWS-EKS-Cluster-Basics/c5-07-eks-node-group-public.tf#L16-L18
bạn cần cần đưa vào 1 ssh để bạn có thể access được con node
thực hiện tại keypair hoặc inport key nhé

sau đó chúng ta tạo các workler node EKS.

# Create AWS EKS Node Group - Public
resource "aws_eks_node_group" "eks_ng_public" {
  cluster_name    = aws_eks_cluster.eks_cluster.name

  node_group_name = "${local.name}-eks-ng-public"
  node_role_arn   = aws_iam_role.eks_nodegroup_role.arn
  subnet_ids      = module.vpc.public_subnets
  #version = var.cluster_version #(Optional: Defaults to EKS Cluster Kubernetes version)    
  
  ami_type = "AL2_x86_64"  
  capacity_type = "ON_DEMAND"
  disk_size = 20
  instance_types = ["t3.medium"]
  
  
  remote_access {
    ec2_ssh_key = "eks-terraform-key"
  }

  scaling_config {
    desired_size = 1
    min_size     = 1    
    max_size     = 2
  }

  # Desired max percentage of unavailable worker nodes during node group update.
  update_config {
    max_unavailable = 1    
    #max_unavailable_percentage = 50    # ANY ONE TO USE
  }

  # Ensure that IAM Role permissions are created before and deleted after EKS Node Group handling.
  # Otherwise, EKS will not be able to properly delete EC2 Instances and Elastic Network Interfaces.
  depends_on = [
    aws_iam_role_policy_attachment.eks-AmazonEKSWorkerNodePolicy,
    aws_iam_role_policy_attachment.eks-AmazonEKS_CNI_Policy,
    aws_iam_role_policy_attachment.eks-AmazonEC2ContainerRegistryReadOnly,
  ] 

  tags = {
    Name = "Public-Node-Group"
  }
}

OoK! Giờ bạn clone code của mình về và bạn thực hiện terraform apply
là 1 bạn đã có 1 cluster k8s trên AWS.

Verify EKS Cluster from AWS Mgmt Console

GIờ chúng ta có 1 cluster eks

Bạn sẽ cần nhớ tên cluster
thực hiện cài kubectl và aws cli.

thực hiện cấu hình authen aws cli
aws configure

Sau khi đã config xong thì thực hiện export ra file kubeconfig để gõ lệnh kubectl.

aws eks --region <region-code> update-kubeconfig --name <cluster_name>
aws eks --region us-east-1 update-kubeconfig --name SAP-dev-eksdemo

#list the eks cluster on aws
aws eks list-clusters --region <region_name> --profile <profile_name>

Install kubectl, configure kubeconfig and verify

Giờ thử soi file kubeconfig
cat /root/.kube/config

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeU1EZ3hPVEF5TVRrek5sb1hEVE15TURneE5qQXlNVGt6Tmxvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTVg2ClpNTWhKeG9paHRZYU5meWIxWHZtMUxzRlJSbS9RVE9yQjRxZ0UyZXMveFFsMVV1VVdaZkhIa2gzS0RVcWRtMTYwVGs1NAoxVTArVHNFRjFPUjh2Y0cxUWxnSWYwQ29ZUDQrYWR1TkN4aVJ6RWw0K1FDZzB0bEUra0NSMTZSQWRWUFp6clRnCmRpYUJHQnRxOHBhaEUzTDJCWDVJR3JxZFRCUFY0LzMxcWUyanBEeG9qOG5oNzd6TXdGNFU0ZTVOSE9QQUNoVi8KN2NrPQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
    server: https://57FF4F2A08F314FEC4D1A87078ADDEDB.gr7.us-east-1.eks.amazonaws.com
  name: arn:aws:eks:us-east-1:250887682577:cluster/SAP-dev-eksdemo
contexts:
- context:
    cluster: arn:aws:eks:us-east-1:250887682577:cluster/SAP-dev-eksdemo
    user: arn:aws:eks:us-east-1:250887682577:cluster/SAP-dev-eksdemo
  name: arn:aws:eks:us-east-1:250887682577:cluster/SAP-dev-eksdemo
current-context: arn:aws:eks:us-east-1:250887682577:cluster/SAP-dev-eksdemo
kind: Config
preferences: {}
users:
- name: arn:aws:eks:us-east-1:250887682577:cluster/SAP-dev-eksdemo
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1beta1
      args:
      - --region
      - us-east-1
      - eks
      - get-token
      - --cluster-name
      - SAP-dev-eksdemo
      command: aws

Đầu tiên ta chú ý đến
server: https://57FF4F2A08F314FEC4D1A87078ADDEDB.gr7.us-east-1.eks.amazonaws.com

arn:aws:eks:us-east-1:250887682577

Bạn thấy là nó giống với ID của user chạy terraform.

Bước tiếp theo chúng ta có thể ssh vào các con worker node hem!

Ở bước trên bạn đã có private key, giờ chúng ta sẽ sài

chmod 400 private-key/eks-terraform-key.pem
ssh -i private-key/eks-terraform-key.pem ec2-user@<IP-Public-of-node (EC2)>

Nếu bạn không chạy lên trên thì sẽ bị lỗi ntn

ssh -i private-key/eks-terraform-key.pem ec2-user@54.198.194.112
The authenticity of host '54.198.194.112 (54.198.194.112)' can't be established.
ECDSA key fingerprint is SHA256:hvU67Fy+5vEwJhmaXHFyxxKgohHZ/XPX9QCM3qKFdfM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Please type 'yes', 'no' or the fingerprint: yes
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '54.198.194.112' (ECDSA) to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'private-key/eks-terraform-key.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "private-key/eks-terraform-key.pem": bad permissions
Bạn có thể lấy IP public của node ở đây!

Sau khi đã ssh thì ta chạy 1 số lệnh kiểm tra con node

>>>>>
ps -ef | grep kube
>>>>>
root      3052     1  1 04:14 ?        00:02:36 /usr/bin/kubelet --cloud-provider aws --config /etc/kubernetes/kubelet/kubelet-config.json --kubeconfig /var/lib/kubelet/kubeconfig --container-runtime docker --network-plugin cni --node-ip=10.0.102.250 --pod-infra-container-image=602401143452.dkr.ecr.us-east-1.amazonaws.com/eks/pause:3.5 --v=2 --node-labels=eks.amazonaws.com/nodegroup-image=ami-0cca3d8b6715c9f74,eks.amazonaws.com/capacityType=ON_DEMAND,eks.amazonaws.com/nodegroup=SAP-dev-eks-ng-public --max-pods=17
root      3697  3675  0 04:14 ?        00:00:01 kube-proxy --v=2 --config=/var/lib/kube-proxy-config/config --hostname-override=ip-10-0-102-250.ec2.internal
root     17563 16548  0 06:49 pts/0    00:00:00 grep --color=auto kube

3) Understand EKS Cluster Network Interfaces

Bạn nhìn vào chỗ mũi tên mình chỉ

Bạn sẽ thấy là EKS Control Plane nó nằm ở VPC của amazon và con này thì do Amazon quản lý.

Bạn cũng thấy là EKS control Plane sẽ cần tạo 1 EKS Elastic Network Interface ở VPC của bạn

Elastic Network Interfaces (ENI)
– When you create the cluster, Amazon EKS creates and manages network interfaces in your account that have Amazon EKS <Cluster name> in their description.

Trong mục EC2 bạn tìm đến phần network interface
bạn sẽ thấy EKS đã tạo
Đây là hình mình chôm được
Đây cũng là 1 hình mình chôm được
và đây là ảnh của mình


– These network interfaces allow AWS Fargate and Amazon EC2 instances to communicate with the EKS control plane present in Amazon VPC in Amazon Account (Not our AWS Account).
– The Amazon EKS created cluster security group and any additional security groups that you specify when you create your cluster are applied to these network interfaces.
– Very Important Note: These Network Interfaces were created in our EKS VPC in our AWS account but attached to EKS Control Plane Master Node instances of Amazon VPC.

Đây là structure is quite complex

Quay trợ lại 1 chút

Bạn cũng thấy là EKS Elastic network interface được tạo ở Public subnet
và việc tạo ở đâu là do chúng ta quyết định

Bạn có thể view phần config VPC bên dưới

https://github.com/mrnim94/terraform-aws/blob/master/eks/AWS-EKS-Cluster-Basics/c5-06-eks-cluster.tf#L7-L8
bạn thấy chỗ đó đang nhân subnet public

vpc_config {
    subnet_ids = module.vpc.public_subnets
...

4) EKS Security Groups

Bạn sẽ thấy là nó sẽ có 2 Security Group
– SG Cluster
– SG Node

Giờ ta trở lại mô hình thực tế!

Bạn thấy là VPC để phục vụ cho cluster EKS là vpc-03743e0ba29ca35c3

Trở lại với EC2 -> Security Group.

Chúng ta sẽ chỉ quan tâm đến các security Group có liên quan đền VPC vpc-03743e0ba29ca35c3
Đây tiên chúng ta sẽ quan tâm đến security default cho VPC vpc-03743e0ba29ca35c3

sg-009d0855ae27d15c6 – default
Được tạo mặc định khi create VPC thì phải
Inbound rules và Outbound rules
đều là All traffic, All protocol, All port range.

– SecurityGroup Cluster

Giờ chúng ta tìm hiểu cái này

– SecurityGroup Node

Cái SG này là để grant access vào các con node EKS (K8s) trong 1 subnet
EKS created security group applied to ENI that is attached to EKS Control Plane master nodes, as well as any managed workloads.
Nếu bạn vào instance đang là con node của k8s bạn sẽ thấy nó đang được apply 1 SG

Như theo chuyên gia khuyên thì nếu bạn muốn change bất cứ cái gì liên quan đền incoming thì change trong SG eks-remoteAccess-xxxx

Bạn cũng thấy ở trên là chúng ta đang có SG để access incoming SSH.
Vậy cài nào đã tạo SG này.

https://github.com/mrnim94/terraform-aws/blob/master/eks/AWS-EKS-Cluster-Basics/c5-07-eks-node-group-public.tf#L16-L18
bạn soi dòng này

# Create AWS EKS Node Group - Public
resource "aws_eks_node_group" "eks_ng_public" {
  cluster_name    = aws_eks_cluster.eks_cluster.name
  
  #>>>Look at belove
  remote_access {
    ec2_ssh_key = "eks-terraform-key"
  }
.......

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_node_group#remote_access-configuration-block
ec2_ssh_key – (Optional) EC2 Key Pair name that provides access for SSH communication with the worker nodes in the EKS Node Group. If you specify this configuration, but do not specify source_security_group_ids when you create an EKS Node Group, port 22 on the worker nodes is opened to the Internet (0.0.0.0/0).
==> là do cài này nó tạo



AWS - Amazon Web Service, Terraform

Post navigation

Previous Post: [Argocd] Run “bash/sh” to execute pods on kubernetes via Argocd.
Next Post: [AWS] Looking into IAM Role and Assume Role in AWS

More Related Articles

[EKS] Checking your EKS cluster that is working efficiently. AWS - Amazon Web Service
[EKS] Upgrade eks cluster by eksctl AWS - Amazon Web Service
[terraform] Error: InvalidPermission.Duplicate: the specified rule AWS - Amazon Web Service
[Metrics Server] Install metrics-server on Kubernetes. AWS - Amazon Web Service
[Bitbucket Pipeline] Design bitbucket-pipeline and eksctl to upgrade EKS cluster AWS - Amazon Web Service
[AWS] EKS IAM Roles for Service Accounts (IRSA) using Terraform AWS - Amazon Web Service

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • Experiences for IP Addresses Shortage on EKS Clusters March 29, 2023
  • [Talisman] Discover the sensitive information in your code. March 28, 2023
  • [Prometheus/Grafana] Install Prometheus and Grafana on ubuntu. March 27, 2023
  • [Kong Gateway] WebSocket connection failed March 26, 2023
  • [Nextcloud] Can’t download files to have a size bigger than 2Gi on NextCloud – RaspBerry March 24, 2023

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • Golang
    • Jquery & JavaScript
    • Laravel
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
    • Longhorn – Storage
    • MetalLB
    • Vault
    • VictoriaMetrics
  • Log & Monitor
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2023 NimTechnology.