1 cloudforamtion으로 명령서버 1대 생성 role 부여 aws s3 ls 2 # eks 생성 2개 Agile EKS Multi-Cluster 템플릿 (eks-agile-stack.yaml) ------- mkdir 1 cd 1 cat < eks-agile-stack.yaml AWSTemplateFormatVersion: '2010-09-09' Description: 'AWS EKS Multi-Cluster Infrastructure for Agile Development (Seoul Region) - T3 Small' Parameters: ClusterName1: Type: String Default: "Kubernetes-Cluster-1" ClusterName2: Type: String Default: "Kubernetes-Cluster-2" InstanceType: Type: String Default: "t3.small" Resources: # 1. VPC & Network (4 Availability Zones) VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 EnableDnsSupport: true EnableDnsHostnames: true Tags: [{Key: Name, Value: Agile-VPC}] InternetGateway: Type: AWS::EC2::InternetGateway VPCGatewayAttachment: Type: AWS::EC2::VPCGatewayAttachment Properties: VpcId: !Ref VPC InternetGatewayId: !Ref InternetGateway # Subnets (Seoul AZs: a, b, c, d) SubnetAZ1: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC AvailabilityZone: !Sub "\${AWS::Region}a" CidrBlock: 10.0.1.0/24 MapPublicIpOnLaunch: true SubnetAZ2: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC AvailabilityZone: !Sub "\${AWS::Region}b" CidrBlock: 10.0.2.0/24 MapPublicIpOnLaunch: true SubnetAZ3: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC AvailabilityZone: !Sub "\${AWS::Region}c" CidrBlock: 10.0.3.0/24 MapPublicIpOnLaunch: true SubnetAZ4: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC AvailabilityZone: !Sub "\${AWS::Region}d" CidrBlock: 10.0.4.0/24 MapPublicIpOnLaunch: true # Routing PublicRouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC DefaultRoute: Type: AWS::EC2::Route Properties: RouteTableId: !Ref PublicRouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref InternetGateway Subnet1Assoc: Type: AWS::EC2::SubnetRouteTableAssociation Properties: {RouteTableId: !Ref PublicRouteTable, SubnetId: !Ref SubnetAZ1} Subnet2Assoc: Type: AWS::EC2::SubnetRouteTableAssociation Properties: {RouteTableId: !Ref PublicRouteTable, SubnetId: !Ref SubnetAZ2} Subnet3Assoc: Type: AWS::EC2::SubnetRouteTableAssociation Properties: {RouteTableId: !Ref PublicRouteTable, SubnetId: !Ref SubnetAZ3} Subnet4Assoc: Type: AWS::EC2::SubnetRouteTableAssociation Properties: {RouteTableId: !Ref PublicRouteTable, SubnetId: !Ref SubnetAZ4} # 2. IAM Roles EKSClusterRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: {Service: [eks.amazonaws.com]} Action: ['sts:AssumeRole'] ManagedPolicyArns: - arn:aws:iam::aws:policy/AmazonEKSClusterPolicy EKSNodeRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: {Service: [ec2.amazonaws.com]} Action: ['sts:AssumeRole'] ManagedPolicyArns: - arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy - arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy - arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly # 3. EKS Cluster 1 Cluster1: Type: AWS::EKS::Cluster Properties: Name: !Ref ClusterName1 RoleArn: !GetAtt EKSClusterRole.Arn ResourcesVpcConfig: SubnetIds: [!Ref SubnetAZ1, !Ref SubnetAZ2] NodeGroup1: Type: AWS::EKS::Nodegroup Properties: ClusterName: !Ref Cluster1 NodegroupName: !Sub "\${ClusterName1}-Nodes" NodeRole: !GetAtt EKSNodeRole.Arn Subnets: [!Ref SubnetAZ1, !Ref SubnetAZ2] ScalingConfig: MinSize: 2 DesiredSize: 2 MaxSize: 4 InstanceTypes: [!Ref InstanceType] # 4. EKS Cluster 2 Cluster2: Type: AWS::EKS::Cluster Properties: Name: !Ref ClusterName2 RoleArn: !GetAtt EKSClusterRole.Arn ResourcesVpcConfig: SubnetIds: [!Ref SubnetAZ3, !Ref SubnetAZ4] NodeGroup2: Type: AWS::EKS::Nodegroup Properties: ClusterName: !Ref Cluster2 NodegroupName: !Sub "\${ClusterName2}-Nodes" NodeRole: !GetAtt EKSNodeRole.Arn Subnets: [!Ref SubnetAZ3, !Ref SubnetAZ4] ScalingConfig: MinSize: 2 DesiredSize: 2 MaxSize: 4 InstanceTypes: [!Ref InstanceType] Outputs: Cluster1Arn: Value: !GetAtt Cluster1.Arn Cluster2Arn: Value: !GetAtt Cluster2.Arn EOF 2 # t3.smll 로 eks 2개 생성 aws cloudformation create-stack \ --stack-name Agile-EKS-Stack \ --template-body file://eks-agile-stack.yaml \ --capabilities CAPABILITY_IAM \ --region ap-northeast-2 # 콘솔에서 클러스터, ec2 생성확인 # cloudformation에서 생성 확인 3 아키텍처 다이어그램은 AWS Seoul Region 내에서 Multi-AZ 및 Multi-Cluster 구성을 통해 가용성을 확보한 EKS(Elastic Kubernetes Service) 환경을 보여주고 있습니다. 이 구조를 구현하기 위한 AWS CloudFormation 템플릿의 핵심 구성 요소를 정리해 드립니다. 전체를 한 파일에 담기보다는 네트워크(VPC)와 클러스터(EKS) 계층으로 나누어 관리하는 것이 관례이므로, 이를 결합한 핵심 구조 위주로 작성했습니다. 1. 네트워크 및 가용 영역(AZ) 설정 다이어그램의 점선 박스로 표시된 4개의 Availability Zones와 이를 아우르는 VPC를 설정합니다. Parameters: VpcBlock: Type: String Default: 10.0.0.0/16 Resources: # VPC 생성 EKSVPC: Type: AWS::EC2::VPC Properties: CidrBlock: !Ref VpcBlock EnableDnsSupport: true EnableDnsHostnames: true Tags: - Key: Name Value: Agile-EKS-VPC # Public Subnets (다이어그램상 로드밸런서 및 외부 통신용) PubSubnet1: Type: AWS::EC2::Subnet Properties: VpcId: !Ref EKSVPC AvailabilityZone: !Select [ 0, !GetAZs '' ] CidrBlock: 10.0.1.0/24 2. Kubernetes Cluster 1 & 2 (EKS) 다이어그램의 주황색 박스에 해당하는 EKS Cluster 정의입니다. 두 개의 클러스터를 동일한 방식으로 선언하거나, Count를 사용하여 반복 생성할 수 있습니다. # Cluster 1 제어 평면 EKSCluster1: Type: AWS::EKS::Cluster Properties: Name: Kubernetes-Cluster-1 RoleArn: !GetAtt EKSClusterRole.Arn ResourcesVpcConfig: SubnetIds: - !Ref PubSubnet1 - !Ref PubSubnet2 # 4개의 AZ에 걸쳐 설정 # Cluster 2 제어 평면 EKSCluster2: Type: AWS::EKS::Cluster Properties: Name: Kubernetes-Cluster-2 RoleArn: !GetAtt EKSClusterRole.Arn ResourcesVpcConfig: SubnetIds: - !Ref PubSubnet3 - !Ref PubSubnet4 3. Auto Scaling Group (Managed Node Group) 다이어그램 하단의 칩 모양 아이콘(EC2 인스턴스)들을 관리하는 Auto Scaling Group입니다. AWS에서는 EKS Managed Node Group을 사용하는 것이 관리가 수월합니다. # Cluster 1의 노드 그룹 EKSNodeGroup1: Type: AWS::EKS::Nodegroup Properties: ClusterName: !Ref EKSCluster1 NodegroupName: Cluster1-ASG NodeRole: !GetAtt NodeInstanceRole.Arn ScalingConfig: MinSize: 2 DesiredSize: 4 MaxSize: 8 Subnets: - !Ref PubSubnet1 - !Ref PubSubnet2 InstanceTypes: - t3.medium 4. 로드 밸런서 (Traffic Distribution) 다이어그램 최상단의 보라색 아이콘은 두 클러스터로 트래픽을 분산하는 Application Load Balancer(ALB) 또는 Route 53 Traffic Policy 역할을 합니다. # 외부 유입 경로 (ALB) IngressLoadBalancer: Type: AWS::ElasticLoadBalancingV2::LoadBalancer Properties: Subnets: - !Ref PubSubnet1 - !Ref PubSubnet2 Type: application SecurityGroups: - !Ref LBSecurityGroup ------- 현 시 참고사항 IAM Role: EKS 클러스터와 노드 그룹이 AWS 리소스에 접근할 수 있도록 적절한 AWS::IAM::Role 설정이 반드시 선행되어야 합니다. Agile 환경의 핵심: 다이어그램 제목처럼 'Agile하게' 개발하려면 이 CloudFormation 템플릿을 CI/CD 파이프라인(GitHub Actions 등)에 연결하여 인프라 변경 사항을 즉각 반영하는 것이 중요합니다. Multi-Cluster 관리: 실제 운영 시에는 두 클러스터 간의 상태 동기화를 위해 ArgoCD나 Federation 도구를 함께 검토해 보시는 것을 추천드립니다. --- 아키텍처 다이어그램(Seoul Region, 4개의 AZ, Multi-Cluster EKS)을 기반으로 한 실행 가능한 Full CloudFormation 템플릿입니다. 이 템플릿은 네트워크 인프라(VPC, Subnets, IGW), IAM Roles, 그리고 두 개의 EKS 클러스터와 노드 그룹을 한 번에 생성합니다. ------- 사용 방법 템플릿 저장: 위 코드를 eks-agile-stack-t3-small.yaml 파일로 저장합니다. 배포: AWS CLI를 사용하여 배포합니다. (서울 리전 기준) # 생성 - free 계정 aws cloudformation create-stack \ --stack-name Agile-EKS-Stack \ --template-body file://eks-agile-stack-t3-small.yaml \ --capabilities CAPABILITY_IAM \ --region ap-northeast-2 확인: 배포에는 약 15~20분 정도 소요됩니다. 완료 후 AWS 콘솔의 EKS 메뉴에서 두 개의 클러스터가 정상 작동하는지 확인하세요. ----- 주요 설정 포인트 Multi-AZ: 다이어그램에 명시된 대로 4개의 가용 영역(2a, 2b, 2c, 2d)에 서브넷을 분산 배치했습니다. Resource Separation: Cluster 1은 AZ1/AZ2를 사용하고, Cluster 2는 AZ3/AZ4를 사용하도록 서브넷을 분산하여 가용성을 극대화했습니다. Scalability: 각 클러스터의 Nodegroup에 ScalingConfig를 적용하여 부하에 따라 자동으로 노드가 증설(Auto Scaling)되도록 구성했습니다. ------ AWS CLI를 사용하여 새로 생성한 클러스터의 인증 정보를 로컬로 가져와야 합니다. 이전 단계에서 생성한 클러스터 이름을 사용하여 다음 명령어를 실행해 주세요. (이미 리전은 ap-northeast-2로 설정되어 있다고 가정합니다.) # Cluster 1 연결 설정 aws eks update-kubeconfig --region ap-northeast-2 --name Kubernetes-Cluster-1 # Cluster 2 연결 설정 (필요한 경우) aws eks update-kubeconfig --region ap-northeast-2 --name Kubernetes-Cluster-2 kubectl get nodes ---------- # 현재 컨텍스트 확인 kubectl config current-context arn:aws:eks:ap-northeast-2:697016550159:cluster/Kubernetes-Cluster-2 # 컨텍스트 이동 kubectl config use-context arn:aws:eks:ap-northeast-2:697016550159:cluster/Kubernetes-Cluster-2 ------ # 컨텍스트 이동 유틸 sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens kubectx --------- # 컨텐스트 별명 # 두 번째 클러스터의 권한을 로컬로 가져오기 (예: Cluster-2) aws eks update-kubeconfig --region ap-northeast-2 --name Kubernetes-Cluster-2 # 클러스터 목록 확인 및 별명 지정 kubectx cluster1=arn:aws:eks:ap-northeast-2:697016550159:cluster/Kubernetes-Cluster-1 kubectx cluster2=arn:aws:eks:ap-northeast-2:697016550159:cluster/Kubernetes-Cluster-2 # 두번째 터미널 watch -d kubectl get ing,svc,deploy,pods ------------ # 클러스터 확인 kubectx cluster1 cluster2 # 클러스터 변경 (cluster2:N/A)[root@mgmt-server ~]# kubectx cluster1 Switched to context "cluster1". ---------- # 마리오 게임 배포 -cluster 1 cat <