EKS 리소스 부하 분산
NLB(Network Load Balancer)
- Service 리소스의 LoadBalancer 타입을 감시하여, 필요 시 NLB를 생성하여 TCP/UDP 트래픽을 관리
- 낮은 지연 시간과 고성능이 요구되는 애플리케이션에 적합
ALB(Application Load Balancer)
- AWS Load Balancer Controller는 Kubernetes의 Ingress 리소스를 감시하여, 필요한 경우 ALB를 자동으로 생성하고 관리
- HTTP/HTTPS 트래픽을 여러 파드에 걸쳐 로드밸런싱 할 수 있음
- ALB Ingress Controller는 Kubernetes Ingress 리소스를 활용하여 트래픽 라우팅 규칙을 정의
- 들어오는 요청이 ALB Ingress Controller에 도달하면 요청의 호스트와 경로를 평가하여 클러스터 내에서 적절한 백엔드 서비스를 결정 그런 다음 컨트롤러는 AWS Application Load Balancer를 사용하여 정의된 규칙에 따라 지정된 백엔드 서비스에 트래픽을 분산
aws-load-balancer-controller 배포
- AWS Ingress Controller
- Kubernetes 클러스터에서 AWS Elastic Load Balancers (ALB 및 NLB)를 자동으로 관리하고, 이를 통해 클러스터 내 애플리케이션을 외부로 노출
helm 을 활용하여, Load Balancer Controller 생성하기
- helm 설치
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
helm --help
- Amazon EKS 클러스터 정보 수집 후 변수에 저장
- cluster_name : 클러스터 이름
- oidc_id : OIDC 자격 증명 ID
export cluster_name=demo-eks
oidc_id=$(aws eks describe-cluster --name $cluster_name --query "cluster.identity.oidc.issuer" --output text | cut -d '/' -f 5)
echo $oidc_id
AWS Load Balancer 설치
https://docs.aws.amazon.com/ko_kr/eks/latest/userguide/lbc-helm.html
Helm를 사용하여 AWS Load Balancer Controller 설치 - Amazon EKS
AWS Management Console에서 정책을 보는 경우 콘솔에 ELB 서비스에 대한 경고는 표시되지만 ELB v2 서비스에 대한 경고는 표시되지 않습니다. 이는 정책의 작업 중 일부가 ELB v2에는 있지만 ELB에는 없기
docs.aws.amazon.com
- 사용자 대신 AWS API를 호출할 수 있는 AWS Load Balancer Controller의 IAM 정책 다운로드
- 다운로드 한 정책을 사용하여 IAM 정책 생성하기
$ curl -O https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.7.2/docs/install/iam_policy.json
$ aws iam create-policy --policy-name AWSLoadBalancerControllerIAMPolicy --policy-document file://iam_policy.json
{
"Policy": {
"PolicyName": "AWSLoadBalancerControllerIAMPolicy",
"PolicyId": "ANPA4AQ3TYVKXLWJXSWUW",
"Arn": "arn:aws:iam::825765381461:policy/AWSLoadBalancerControllerIAMPolicy",
"Path": "/",
"DefaultVersionId": "v1",
"AttachmentCount": 0,
"PermissionsBoundaryUsageCount": 0,
"IsAttachable": true,
"CreateDate": "2025-01-29T04:09:09+00:00",
"UpdateDate": "2025-01-29T04:09:09+00:00"
}
}
web page 에서 생성한 정책 확인하기
$ eksctl create iamserviceaccount \
--cluster=demo-eks \
--namespace=kube-system \
--name=aws-load-balancer-controller \
--role-name AmazonEKSLoadBalancerControllerRole \
--attach-policy-arn=arn:aws:iam::825765381461:policy/AWSLoadBalancerControllerIAMPolicy \
--approve
2025-01-29 05:17:30 [ℹ] 1 existing iamserviceaccount(s) (kube-system/aws-load-balancer-controller) will be excluded
2025-01-29 05:17:30 [ℹ] 1 iamserviceaccount (kube-system/aws-load-balancer-controller) was excluded (based on the include/exclude rules)
2025-01-29 05:17:30 [!] serviceaccounts that exist in Kubernetes will be excluded, use --override-existing-serviceaccounts to override
2025-01-29 05:17:30 [ℹ] no tasks
서비스 어카운트 생성 확인
$ kubectl get sa -n kube-system | grep load
aws-load-balancer-controller 0 32s
$ kubectl describe -n kube-system sa aws-load-balancer-controller
Name: aws-load-balancer-controller
Namespace: kube-system
Labels: app.kubernetes.io/managed-by=eksctl
Annotations: eks.amazonaws.com/role-arn: arn:aws:iam::<ACCOUNT_ID>:role/AmazonEKSLoadBalancerControllerRole
Image pull secrets: <none>
Mountable secrets: <none>
Tokens: <none>
Events: <none>
AWS Load Balancer Controller 설치
- eks helm repository 등록
$ helm repo add eks https://aws.github.io/eks-charts
$ helm repo update eks
- AWS Load Balancer Controller를 설치
$ helm install aws-load-balancer-controller eks/aws-load-balancer-controller -n kube-system \
--set clusterName=[클러스터이름] --set serviceAccount.create=false --set serviceAccount.name=aws-load-balancer-controller
$ helm install aws-load-balancer-controller eks/aws-load-balancer-controller -n kube-system \
--set clusterName=demo-eks --set serviceAccount.create=false --set serviceAccount.name=aws-load-balancer-controller
NAME: aws-load-balancer-controller
LAST DEPLOYED: Wed Jan 29 05:42:20 2025
NAMESPACE: kube-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
AWS Load Balancer controller installed!
- AWS Load Balancer Controller 배포상태 확인
$ kubectl get deployment -n kube-system aws-load-balancer-controller
NAME READY UP-TO-DATE AVAILABLE AGE
aws-load-balancer-controller 0/2 0 0 2m18s
$ kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
aws-load-balancer-controller-7d6bb464b6-tq4kr 1/1 Running 0 115s
aws-load-balancer-controller-7d6bb464b6-ts5tp 1/1 Running 0 115s
NLB 서비스 예시
https://docs.aws.amazon.com/ko_kr/eks/latest/userguide/network-load-balancing.html
Network Load Balancer를 사용하여 TCP 및 UDP 트래픽 라우팅 - Amazon EKS
이전 버전과의 호환성을 위해 service.beta.kubernetes.io/aws-load-balancer-type: "nlb-ip" 주석이 여전히 지원됩니다. 그러나 service.beta.kubernetes.io/aws-load-balancer-type: "nlb-ip" 대신 새 로드 밸런서에 이전 주석을
docs.aws.amazon.com
샘플 애플리케이션 배포 실습
$ cat > echo-service-nlb.yaml
# 파드 배포
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-echo
spec:
replicas: 3
selector:
matchLabels:
app: deploy-websrv
template:
metadata:
labels:
app: deploy-websrv
spec:
containers:
- name: akos-websrv
image: k8s.gcr.io/echoserver:1.5
ports:
- containerPort: 8080
---
# NLB 배포
# Kubernetes에서 AWS Network Load Balancer(NLB)를 설정할 때 annotations 사용
apiVersion: v1
kind: Service
metadata:
name: svc-nlb-ip-type
annotations:
service.beta.kubernetes.io/aws-load-balancer-nlb-target-type: ip
service.beta.kubernetes.io/aws-load-balancer-scheme: internet-facing
service.beta.kubernetes.io/aws-load-balancer-healthcheck-port: "8080"
service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: "true"
spec:
ports:
- port: 80
targetPort: 8080
protocol: TCP
type: LoadBalancer
loadBalancerClass: service.k8s.aws/nlb
selector:
app: deploy-websrv
- 디플로이먼트 & 서비스 배포
$ kubectl apply -f echo-service-nlb.yaml
$ kubectl get pod -o wide
$ kubectl get targetgroupbindings
- 배포 확인, 배포된 파드와 해당 파드들을 타겟 그룹으로 가지는 서비스 (NLB)를 확인
- kubectl 명령어로 확인
$ kubectl get pods,svc,ep
- 브라우저로 NLB 접속
ALB 서비스
https://docs.aws.amazon.com/ko_kr/eks/latest/userguide/alb-ingress.html
Application Load Balancer를 사용하여 애플리케이션 및 HTTP 트래픽 라우팅 - Amazon EKS
IPv6 Pods로 로드 밸런싱하는 경우, 인그레스 사양에 다음 주석을 추가합니다. IPv6를 통해서는 인스턴스 대상이 아닌 IP 대상으로만 로드 밸런싱할 수 있습니다. 이 주석이 없으면 로드 밸런싱은 IPv
docs.aws.amazon.com
파드 배포 및 Ingress 배포
$ cat > 2048_full.yaml
apiVersion: v1
kind: Namespace
metadata:
name: game-2048
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: game-2048
name: deployment-2048
spec:
selector:
matchLabels:
app.kubernetes.io/name: app-2048
replicas: 2
template:
metadata:
labels:
app.kubernetes.io/name: app-2048
spec:
containers:
- image: public.ecr.aws/l6m2t8p7/docker-2048:latest
imagePullPolicy: Always
name: app-2048
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
namespace: game-2048
name: service-2048
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
type: NodePort
selector:
app.kubernetes.io/name: app-2048
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: game-2048
name: ingress-2048
annotations:
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
spec:
ingressClassName: alb
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: service-2048
port:
number: 80
- 서비스 동작 및 확인
$ kubectl apply -f 2048_full.yaml
$ kubectl get pod -n game-2048 -o wide
$ kubectl get targetgroupbindings -n game-2048
- Ingress 확인
$ kubectl describe ingress -n game-2048 ingress-2048
- web 에서 확인
해당 주소로 web 접속하면 2048 게임 실행됨
'DevOps > Kubernetes' 카테고리의 다른 글
[Kubernetes] Kubernetes NodePort란? (0) | 2025.02.28 |
---|---|
[Kubernetes] YAML에서 Deploy(Deployment)와 Service의 차이 (0) | 2025.02.26 |
[EKS] EKS 애플리케이션 배포 실습 (0) | 2025.02.13 |
[Kubernetes] Kubernetes에서 PV(Persistent Volume)와 PVC(Persistent Volume Claim)의 관계 (0) | 2025.02.12 |
[EKS] kubectl 설치 & eksctl 명령어 설치 후 EKS 생성 test (0) | 2025.02.11 |