k8s-kubectl CLI

전지현·2024년 11월 25일

k8s

목록 보기
1/3

1. kubectl 기본 구조

kubectl 명령어의 기본 형태는 다음과 같습니다:

kubectl [command] [TYPE] [NAME] [flags]
  • command: 실행할 작업 (예: get, create, delete, apply).
  • TYPE: 작업 대상 리소스 유형 (예: pod, service, deployment).
  • NAME: 작업할 특정 리소스 이름 (생략 가능).
  • flags: 추가 옵션 (예: -n은 네임스페이스 지정, -o는 출력 형식 설정).

2. 주요 kubectl 명령어

(1) 클러스터 상태 확인

  • 클러스터 연결 상태 확인:
    kubectl cluster-info
  • 모든 노드 보기:
    kubectl get nodes
  • 클러스터 리소스 상태 확인:
    kubectl get all

(2) 네임스페이스 관리

  • 네임스페이스 목록 보기:
    kubectl get namespaces
  • 특정 네임스페이스 설정:
    kubectl config set-context --current --namespace=<namespace-name>

(3) 리소스 생성 및 관리

  • Pod 생성 (YAML 파일 사용):
    kubectl apply -f pod.yaml
  • Pod 확인:
    kubectl get pods
  • Pod 상세 정보:
    kubectl describe pod <pod-name>
  • Pod 삭제:
    kubectl delete pod <pod-name>

(4) Deployment 관리

  • Deployment 생성:
    kubectl create deployment <deployment-name> --image=<image-name>
  • Deployment 목록 보기:
    kubectl get deployments
  • Deployment 업데이트:
    kubectl set image deployment/<deployment-name> <container-name>=<new-image>

(5) Service 관리

  • Service 목록 보기:
    kubectl get services
  • 특정 Service 노출:
    kubectl expose deployment <deployment-name> --type=NodePort --port=8080
  • Service 삭제:
    kubectl delete service <service-name>

(6) 로그 및 디버깅

  • Pod 로그 확인:
    kubectl logs <pod-name>
  • 실행 중인 Pod에 들어가기:
    kubectl exec -it <pod-name> -- /bin/bash
  • 리소스 이벤트 확인:
    kubectl get events

3. kubectl 명령어 연습

(1) 연습 환경 설정

  • Minikube 또는 Kind로 클러스터를 생성하고 실습 환경을 만듭니다.
    minikube start

(2) 샘플 애플리케이션 배포

  • 공식 예제 애플리케이션 배포:
    kubectl apply -f https://k8s.io/examples/application/deployment.yaml
  • 배포된 리소스 확인:
    kubectl get all
  • 애플리케이션 상태 점검:
    kubectl describe deployment nginx-deployment

(3) 자주 사용하는 실습 시나리오

  1. Pod 관리:

    • nginx 이미지를 사용해 Pod 생성:
      kubectl run my-nginx --image=nginx --restart=Never
    • Pod의 로그 확인:
      kubectl logs my-nginx
    • Pod 삭제:
      kubectl delete pod my-nginx
  2. Deployment 관리:

    • Deployment 업데이트:
      kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
  3. Service 관리:

    • 특정 Port로 애플리케이션 노출:
      kubectl expose deployment nginx-deployment --type=LoadBalancer --name=my-service --port=80

4. 학습 리소스


0개의 댓글