GitHub Actions 연동 1 mkdir 2 cd 2 # cluster.yaml 파일 생성 cat < cluster.yaml apiVersion: eksctl.io/v1alpha5 kind: ClusterConfig metadata: name: my-eks-cluster region: ap-northeast-2 managedNodeGroups: - name: standard-nodes instanceType: t3.small desiredCapacity: 3 volumeSize: 20 EOF # 클러스터 생성 (약 15분 소요) eksctl create cluster -f cluster.yaml 2 Ec2 로그인 # 터미널1 watch -d kubectl get no,svc,pods,deploy,rs # 터미널2 # 무료계정 = t3.small # S3 버킷 생성 (버킷명은 본인의 프로젝트에 맞게 수정하세요) aws s3 mb s3://my-eks-terraform-state-2026 # DynamoDB 테이블 생성 (상태 잠금용) aws dynamodb create-table \ --table-name terraform-lock-table \ --attribute-definitions AttributeName=LockID,AttributeType=S \ --key-schema AttributeName=LockID,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 3 cat < main.tf terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } backend "s3" { bucket = "my-eks-terraform-state-2026" key = "eks/terraform.tfstate" region = "ap-northeast-2" dynamodb_table = "terraform-lock-table" } } provider "aws" { region = "ap-northeast-2" } # 기존 VPC나 새 VPC를 사용하여 EKS 노드 그룹을 정의하는 코드를 이곳에 작성합니다. EOF 4 # 디렉토리 생성 mkdir -p .github/workflows cat < .github/workflows/terraform.yml name: "Terraform CI/CD" on: push: branches: [ "main" ] pull_request: branches: [ "main" ] jobs: terraform: name: "Terraform" runs-on: ubuntu-latest env: AWS_ACCESS_KEY_ID: \${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: \${{ secrets.AWS_SECRET_ACCESS_KEY }} steps: - name: Checkout uses: actions/checkout@v3 - name: Setup Terraform uses: hashicorp/setup-terraform@v2 - name: Terraform Init run: terraform init - name: Terraform Format run: terraform fmt -check - name: Terraform Plan run: terraform plan -input=false - name: Terraform Apply if: github.ref == 'refs/heads/main' && github.event_name == 'push' run: terraform apply -auto-approve -input=false EOF AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY 5 # 1. 현재 디렉토리의 모든 파일 스테이징 git add . # 2. 첫 번째 커밋 생성 git commit -m "Initial commit: Setup Terraform and GitHub Actions" # 3. 기본 브랜치 이름을 main으로 변경 git branch -M main # 1. 혹시 주소가 틀렸을 수 있으니 삭제 후 다시 등록 (선택 사항) git remote remove origin git remote add origin https://github.com/topasvga1/s1.git # 2. GitHub로 푸시 git push -u origin main 6 # 트러블 슈팅 error: failed to push some refs to 'https://github.com/topasvga1/s1.git' hint: Updates were rejected because the remote contains work that you do not hint: have locally. This is usually caused by another repository pushing to hint: the same ref. If you want to integrate the remote changes, use hint: 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. # 로컬의 main 브랜치 내용을 원격(GitHub)으로 강제 전송 git push -u origin main --force # 원격의 변경 사항을 로컬로 가져와서 합치기 git pull origin main --rebase # 합쳐진 후 다시 푸시 git push -u origin main 6 cat < main.tf terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } helm = { source = "hashicorp/helm" version = "~> 2.11" } } backend "s3" { bucket = "my-eks-terraform-state-2026" key = "eks/terraform.tfstate" region = "ap-northeast-2" dynamodb_table = "terraform-lock-table" } } provider "aws" { region = "ap-northeast-2" } # EKS 클러스터 정보 가져오기 (이미 생성된 클러스터가 있다면 해당 이름을 넣으세요) data "aws_eks_cluster" "cluster" { name = "my-eks-cluster" } data "aws_eks_cluster_auth" "cluster" { name = "my-eks-cluster" } # Helm 프로바이더 설정: Terraform이 EKS에 접속할 수 있게 합니다. provider "helm" { kubernetes { host = data.aws_eks_cluster.cluster.endpoint cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority[0].data) token = data.aws_eks_cluster_auth.cluster.token } } # Helm을 이용한 Argo CD 자동 설치 resource "helm_release" "argocd" { name = "argocd" repository = "https://argoproj.github.io/argo-helm" chart = "argo-cd" namespace = "argocd" create_namespace = true version = "5.46.7" # 최신 안정 버전 확인 필요 set { name = "server.service.type" value = "LoadBalancer" # 외부 접속을 위해 로드밸런서로 설정 } } EOF 7 # 1. 코드 변경사항 스테이징 git add main.tf # 2. 커밋 메시지 작성 git commit -m "Add Argo CD installation via Terraform Helm" # 3. GitHub로 푸시 (이 순간 Actions가 실행됩니다) git push origin main topasvga1 ghp_o6Kl # 1. 초기화 (S3 백엔드 연결 확인)ye terraform init # 2. 변경 사항 확인 (Argo CD가 왜 안 생겼는지 알려줍니다) terraform plan terraform apply 8 ----------- 테라폼 설치 # 1. yum-utils 설치 (리포지토리 관리를 위해 필요) sudo yum install -y yum-utils # 2. HashiCorp 리포지토리 추가 sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo # 3. 테라폼 설치 sudo yum -y install terraform -------------------- # 1. ArgoCD 관련 리소스 일괄 삭제 kubectl delete -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml # 2. argocd 네임스페이스 삭제 kubectl delete namespace argocd ----------------------------- # Argo CD 로드밸런서 주소 확인 kubectl get svc -n argocd argocd-server # 초기 비밀번호 확인 (아이디: admin) kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d; echo ------------- 2. 깃허브 액션 실습 1 # main.tf 파일에서 노드 그룹 설정을 찾아 instance_types를 수정합니다. # 예: t3.medium -> t3.large (또는 반대) , c7i-flex.large sed -i 's/t3.small/t3.micro/g' main.tf 2 # 1. 상태 확인 git status # 2. 변경사항 저장 git add main.tf git commit -m "chore: upgrade worker node instance type to t3.large" # 3. GitHub로 전송 git push origin main topasvga1 ghp_o6KluGjdKaxkcG 3 # 1. 새 노드가 생성되고 있는지 확인 kubectl get nodes -w # 2. 노드의 상세 정보에서 인스턴스 타입 확인 kubectl get nodes -o custom-columns=NAME:.metadata.name,TYPE:.status.nodeInfo.architecture,INSTANCE:.metadata.labels."beta\.kubernetes\.io/instance-type" 4 # 노드 이름과 인스턴스 타입을 표 형태로 출력 kubectl get nodes -o custom-columns=NAME:.metadata.name,TYPE:.metadata.labels."node\.kubernetes\.io/instance-type" 5 # 1. 새 버킷 생성 (이름 뒤에 날짜 등을 붙여 고유하게 만드세요) aws s3 mb s3://my-new-eks-state-unique-123 # 2. main.tf 수정 sed -i 's/my-eks-terraform-state-2026/my-new-eks-state-unique-123/g' main.tf # 3. 다시 푸시 git add main.tf git commit -m "fix: use new s3 bucket for backend" git push origin main 6 node 교체 실시간 확인 # 새로운 t3.large 노드가 추가되고 기존 노드가 사라지는 과정을 실시간 확인 (Ctrl+C로 종료) kubectl get nodes -o custom-columns=NAME:.metadata.name,TYPE:.metadata.labels."node\.kubernetes\.io/instance-type",STATUS:.status.conditions[-1].type -w ----------- 3. 재테스트 1 # 현재 설정된 인스턴스 타입을 확인합니다. grep -A 5 "eks_managed_node_groups" main.tf || grep "instance_types" main.tf # t3.small을 c7i-flex.large 로 변경해 보세요) # 무료 계정으로 가능한 서버 t3.micro t3.small c7i-flex.large 2/4 m7i-flex.large 2/8 2 # 1. 변경된 파일 스테이징 git add main.tf # 2. 커밋 생성 git commit -m "feat: upgrade worker node type to t3.medium" # 3. GitHub로 푸시 (토큰 입력 필요) git push origin main 3 # 새로운 사양의 노드가 생성되고 기존 노드가 제거되는 과정을 실시간 확인 (Ctrl+C로 종료) kubectl get nodes -o custom-columns=NAME:.metadata.name,TYPE:.metadata.labels."node\.kubernetes\.io/instance-type",STATUS:.status.conditions[-1].type -w ---------------- 모두 삭제법 terraform destroy -auto-approve eksctl delete cluster --name my-eks-cluster