[Cloude] AWS EKS

MeltingOlafยท2022๋…„ 5์›” 30์ผ

[Cloud]

๋ชฉ๋ก ๋ณด๊ธฐ
25/25

โœ”๏ธ AWS EKS

  • Amazon Elastic Kubernetes Service(Amazon EKS)
  • EKS๋Š” Kubernetes๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ด€๋ฆฌํ˜• ์„œ๋น„์Šค์ด๋‹ค.
  • ๋…ธ๋“œ๋ฅผ ์„ค์น˜, ์ž‘๋™ ๋ฐ ์œ ์ง€ ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ์žฅ์ 

https://docs.aws.amazon.com/ko_kr/eks/latest/userguide/what-is-eks.html

โœ”๏ธ AWS EKS ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

$ choco install awscli aws-iam-authenticator eksctl kubernetes-helm

aws ๊ณ„์ • ๋“ฑ๋ก

$ aws configure
$ eksctl create cluster --name myeks --nodes=3 --region=ap-northeast-2

โญ Load Balancer Service โŒ = class lb๋กœ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— nlb๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์–ด์•ผํ•จ
โญ Ingress: โŒ

โœ”๏ธ YAML ํŒŒ์ผ์„ ์ด์šฉํ•ด์„œ EKS ๋ฐฐํฌํ•˜๊ธฐ

$ mkdir aws-eks
$ cd aws-eks

myeks.yaml

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: myeks-custom  # ์ด๋ฆ„์€ ๊ฐ™์„ ์ˆ˜ ์—†๋‹ค
  region: ap-northeast-2
  version: "1.22"

๊ฐ€์šฉ์˜์—ญ ์ง€์ •
- ํ•„์ˆ˜๋Š” ์•„๋‹ˆ๋‚˜ ์ผ๋ฐ˜์ ์œผ๋กœ ์ง€์ •ํ•˜๋Š” ํŽธ

# AZ
availabilityZones: ["ap-northeast-2a", "ap-northeast-2b",  "ap-northeast-2c"]

eks์™€ AWS iam๊ณ„์ •์„ ์—ฐ๊ฒฐ ํ•ด ์ฃผ๋Š” ๋ถ€๋ถ„
wellKnownPolicies : ๊ณ„์ •์— ํ•ด๋‹น๋˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๊ถŒํ•œ ์„ค์ •

# IAM OIDC & Service Account
iam:
  withOIDC: true
  serviceAccounts:
    - metadata:
        name: aws-load-balancer-controller # Addon ์ถ”๊ฐ€ํ•  ๋•Œ ํ•„์š”
        namespace: kube-system
      wellKnownPolicies:
        awsLoadBalancerController: true
    - metadata:
        name: ebs-csi-controller-sa
        namespace: kube-system
      wellKnownPolicies:
        ebsCSIController: true
    - metadata:
        name: cluster-autoscaler
        namespace: kube-system
      wellKnownPolicies:
        autoScaler: true

Worker๋…ธ๋“œ์˜ ๊ทธ๋ฃน

# Managed Node Groups
managedNodeGroups:
  # On-Demand Instance
  - name: myeks-ng1
    instanceType: t3.medium
    minSize: 2
    desiredCapacity: 3
    maxSize: 4
    privateNetworking: true
    ssh:
      allow: true
      publicKeyPath: ./keypair/myeks.pub
    availabilityZones: ["ap-northeast-2a", "ap-northeast-2b", "ap-northeast-2c"]
    iam:
      withAddonPolicies:
        autoScaler: true
        albIngress: true
        cloudWatch: true
        ebs: true
# Fargate Profiles
fargateProfiles:
  - name: fg-1
    selectors:
    - namespace: dev
      labels:
        env: fargate
        
# CloudWatch Logging
cloudWatch:
  clusterLogging:
    enableTypes: ["*"]
    

ํ‚ค ์ƒ์„ฑ

$ mkdir keypair
$ ssh-keygen -f keypair/myssh
$ eksctl create cluster -f myeks.yaml

โœ”๏ธ NLB for LoadBalancer Service

โœ”๏ธ AWS Load Balancer Controller ์„ค์น˜

$ helm repo add eks https://aws.github.io/eks-charts
$ helm repo update

Account ํ™•์ธ

$ aws sts get-caller-identity
{
    "UserId": "AIxxx",
    "Account": "4xxx",
    "Arn": "arn:aws:iam::4xxx"
}
$ helm install aws-load-balancer-controller eks/aws-load-balancer-controller -n kube-system --set clusterName=myeks-lunaris --set serviceAccount.create=false --set serviceAccount.name=aws-load-balancer-controller --set image.repository=[account]412059376128.dkr.ecr.ap-northeast-2.amazonaws.com/amazon/aws-load-balancer-controller

NAME: aws-load-balancer-controller
LAST DEPLOYED: Mon May 30 20:56:35 2022
NAMESPACE: kube-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
AWS Load Balancer controller installed!

์ƒ˜ํ”Œ ์ฝ”๋“œ
myapp.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myweb-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: myweb
          image: ghcr.io/c1t1d0s7/go-myweb
          ports:
            - containerPort: 8080

`mysvc.yaml

apiVersion: v1
kind: Service
metadata:
  name: myweb-svc-lb
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "external"
    service.beta.kubernetes.io/aws-load-balancer-nlb-target-type: "instance"
    service.beta.kubernetes.io/aws-load-balancer-scheme: "internet-facing"
spec:
  type: LoadBalancer
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080

โœ”๏ธ Ingress for ALB

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myweb-ing
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/target-type: instance
    alb.ingress.kubernetes.io/scheme: internet-facing
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: myweb-svc-lb
                port:
                  number: 80
  • alb.ingress.kubernetes.io/target-type
    - instance: EC2 ํƒ€๊ฒŸ
    - ip: Pod ํƒ€๊ฒŸ(Fargate)
  • alb.ingress.kubernetes.io/scheme
    - internal: ๋‚ด๋ถ€
    - internet-facing: ์™ธ๋ถ€

โœ”๏ธ EBS for CSI

EBS ์Šค๋ƒ…์ƒท
EBS ํฌ๊ธฐ ๋ณ€๊ฒฝ

$ eksctl get iamserviceaccount --cluster myeks-custom

NAMESPACE       NAME                            ROLE ARN
kube-system     aws-load-balancer-controller    arn:aws:iam::xxxxx:role/eksctl-myeks-custom-addon-iamserviceaccount-Role1-11N0OKMVG2DYY
kube-system     aws-node                        arn:aws:iam::xxxx:role/eksctl-myeks-custom-addon-iamserviceaccount-Role1-CLMK7A6K5NL3
kube-system     cluster-autoscaler              arn:aws:iam::xxxx:role/eksctl-myeks-custom-addon-iamserviceaccount-Role1-1S02W28MZOSL4
kube-system     ebs-csi-controller-sa           arn:aws:iam::xxxx:role/eksctl-myeks-custom-addon-iamserviceaccount-Role1-15HLE8HBOD9CN
$ eksctl create addon --name aws-ebs-csi-driver --cluster myeks-lunaris--service-account-role-arn  arn:aws:iam::xxxx:role/eksctl-myeks-lunaris-addon-iamserviceaccount-Role1-15HLE8HBOD9CN --force

โœ”๏ธ Metrics Server

$ kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

โœ”๏ธ Cluster Autoscaler

โœ”๏ธ ์ˆ˜๋™ ์Šค์ผ€์ผ๋ง
$ eksctl scale nodegroup --name myeks-ng1 --cluster myeks-lunaris --nodes 2
โœ”๏ธ์ž๋™ ์Šค์ผ€์ผ๋ง
$ curl -o cluster-autoscaler-autodiscover.yaml https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml

cluster-autoscaler-autodiscover.yaml

...
163: - --node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/myeks-lunaris
...
$ kubectl apply -f cluster-autoscaler-autodiscover.yaml
$ kubectl patch deployment cluster-autoscaler -n kube-system -p '{"spec":{"template":{"metadata":{"annotations":{"cluster-autoscaler.kubernetes.io/safe-to-evict": "false"}}}}}'
$ kubectl -n kube-system edit deployment.apps/cluster-autoscaler
      - command:
        - ./cluster-autoscaler
        - --v=4
        - --stderrthreshold=info
        - --cloud-provider=aws
        - --skip-nodes-with-local-storage=false
        - --expander=least-waste
        - --node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/myeks-custom
        - --balance-similar-node-groups
        - --skip-nodes-with-system-pods=false
        image: k8s.gcr.io/autoscaling/cluster-autoscaler:v1.22.6

์ˆ˜์ •

  • --node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/myeks-custom
  • --balance-similar-node-groups
  • --skip-nodes-with-system-pods=false
  • image: k8s.gcr.io/autoscaling/cluster-autoscaler:v1.22.2
$ kubectl set image deployment cluster-autoscaler -n kube-system cluster-autoscaler=k8s.gcr.io/autoscaling/cluster-autoscaler:v1.22.2

์ƒ˜ํ”Œ ์ฝ”๋“œ

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myweb-deploy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: myweb
          image: ghcr.io/c1t1d0s7/go-myweb:alpine
          ports:
            - containerPort: 8080
          resources:
            requests:
              cpu: 200m
              memory: 200M
            limits:
              cpu: 200m
              memory: 200M

โœ”๏ธCloudWatch Container Insight

ClusterName=myeks-custom
RegionName=ap-northeast-2
FluentBitHttpPort='2020'
FluentBitReadFromHead='Off'
[[ ${FluentBitReadFromHead} = 'On' ]] && FluentBitReadFromTail='Off'|| FluentBitReadFromTail='On'
[[ -z ${FluentBitHttpPort} ]] && FluentBitHttpServer='Off' || FluentBitHttpServer='On'
curl https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/quickstart/cwagent-fluent-bit-quickstart.yaml | sed 's/{{cluster_name}}/'${ClusterName}'/;s/{{region_name}}/'${RegionName}'/;s/{{http_server_toggle}}/"'${FluentBitHttpServer}'"/;s/{{http_server_port}}/"'${FluentBitHttpPort}'"/;s/{{read_from_head}}/"'${FluentBitReadFromHead}'"/;s/{{read_from_tail}}/"'${FluentBitReadFromTail}'"/' | kubectl apply -f - 

โœ”๏ธ ํด๋Ÿฌ์Šคํ„ฐ ์‚ญ์ œ

eksctl delete cluster -f .\myeks.yaml --force --disable-nodegroup-eviction
profile
How R U Today :)

0๊ฐœ์˜ ๋Œ“๊ธ€