상세 컨텐츠

본문 제목

25.04.14. 쿠버네티스 실습 정리

AWS CLOUD SCHOOL 9기

by AI Engineer crystal 2025. 4. 14. 15:18

본문

이번 실습에서는 쿠버네티스의 핵심 리소스 관리Pod 생성부터 배포 전략, Docker 이미지 제작 및 푸시까지 다양한 내용을 다뤘습니다. 이 글에서는 실습 과정을 정리하고, 핵심 개념을 쉽게 이해할 수 있도록 설명드리겠습니다.


📌 목차

  1. kubectl api-resources 이해하기
  2. Namespace 생성 및 삭제
  3. 첫 번째 Pod 생성
  4. Pod 상태 확인 방법
  5. 외부 접근: port-forward
  6. Pod 내부 접속 및 테스트
  7. Taint / Toleration
  8. Docker 이미지 제작 및 배포

kubectl api-resources 이해하기

kubectl api-resources

쿠버네티스의 리소스들은 Object 형태의 클래스처럼 사용됩니다.
예: pods, services, deployments 등

  • ShortName: po, svc, deploy 등
  • Namespaced 여부: true → 특정 네임스페이스에서만 동작
  • Kind: YML 파일 작성 시 kind: 필드에 사용됨

Namespace 생성 및 삭제

kubectl create ns testns
kubectl get ns

Namespace는 리소스를 격리하여 관리할 수 있게 해줍니다.
YAML을 이용한 선언형 방식:

apiVersion: v1
kind: Namespace
metadata:
  name: testns

삭제는 다음과 같이:

kubectl delete -f .

첫 번째 Pod 생성

apiVersion: v1
kind: Pod
metadata:
  name: mypod
  namespace: testns
spec:
  containers:
    - name: myctn
      image: nginx
kubectl apply -f testns.yml
kubectl get pod -n testns

Pod 상태 확인 방법

kubectl logs mypod -n testns
kubectl describe pod mypod -n testns
  • logs: 컨테이너 로그 확인
  • describe: 이벤트, 상태, 오류 로그 등 상세 정보 확인

외부 접근: port-forward

kubectl port-forward mypod -n testns 8001:80
  • 외부에서 localhost:8001으로 접속 가능
  • 종료: Ctrl + C 또는 ps, kill 명령어로 종료
ps -ef | grep port-forward
kill -9 <PID>

Pod 내부 접속 및 테스트

kubectl exec -it mypod -n testns -- /bin/bash

내부 웹서버 테스트:

curl -s localhost | head -5

Taint / Toleration

  • Taint: 노드에 "이쪽에 오지마!" 설정
  • Toleration: 해당 taint를 무시하고 배치하도록 Pod 설정

1. 노드에 taint 적용

kubectl taint nodes node1 special=only:NoSchedule

2. toleration + nodeSelector 포함 Pod 정의

apiVersion: v1
kind: Pod
metadata:
  name: httpdpod
spec:
  containers:
    - name: httpdctn
      image: httpd
  tolerations:
    - key: "special"
      operator: "Equal"
      value: "only"
      effect: "NoSchedule"
  nodeSelector:
    kubernetes.io/hostname: node1
kubectl apply -f httpdpod.yml
kubectl get pod -o wide

3. taint 제거

kubectl taint node node1 special-

Docker 이미지 제작 및 배포

디렉토리 구성

mkdir blue green
touch blue/{Dockerfile,index.html}
touch green/{Dockerfile,index.html}

Docker 이미지 빌드 및 푸시

docker build -t brian24/aws9:blue ./blue
docker build -t brian24/aws9:green ./green

docker push brian24/aws9:blue
docker push brian24/aws9:green

✅ 마무리

이번 실습에서는 쿠버네티스의 기본 오브젝트 구조를 이해하고, 실제 Pod를 만들고 관리하는 방법, taint와 toleration으로 노드 배치를 제어하는 방법, Docker 이미지를 직접 만들어 쿠버네티스에서 활용하는 방법까지 배웠습니다.

쿠버네티스는 선언형으로 리소스를 관리한다는 점이 핵심입니다.

관련글 더보기