학습 내용

kubectl 기본 구조

kubectl [command] [type] [name] [flags]
요소설명예시
command수행할 동작get, create, apply, delete, describe
type리소스 종류pod, deployment, service, node
name리소스 이름my-pod, nginx
flags옵션-n, -o, —all-namespaces

주요 명령어

명령어설명
apply파일 기반으로 리소스 생성/업데이트
get리소스 목록 조회
describe리소스 상세 정보
delete리소스 삭제
exec컨테이너에 명령 실행
logs컨테이너 로그 확인

실습

kubectl 자동완성 설정

# bash 자동완성
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
 
# 별칭 설정
alias k=kubectl
complete -F __start_kubectl k

Pod 생성 - 명령형

# 즉시 생성
kubectl run nginx --image=nginx
kubectl run nginx --image=nginx:1.25 --port=80
 
# 실행 중인 Pod 확인
kubectl get pods
kubectl get pods -o wide     # IP, 노드 정보 포함
kubectl get pods -o yaml     # 전체 YAML 출력
kubectl get pods -w          # 변화 감지 (watch)

Pod 생성 - YAML 파일

# nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.25
    ports:
    - containerPort: 80
kubectl apply -f nginx-pod.yaml
kubectl get pods
kubectl describe pod nginx-pod
kubectl delete pod nginx-pod
kubectl delete -f nginx-pod.yaml

Pod 내부 접속 및 로그

kubectl exec -it nginx-pod -- /bin/bash
kubectl exec nginx-pod -- ls /etc/nginx
kubectl logs nginx-pod
kubectl logs nginx-pod -f   # 실시간

리소스 조회 팁

# 지원하는 리소스 종류 전체 확인
kubectl api-resources
 
# 특정 리소스 필드 설명
kubectl explain pod
kubectl explain pod.spec
kubectl explain pod.spec.containers

체크리스트

  • kubectl [command] [type] [name] [flags] 구조 이해
  • kubectl 자동완성 설정
  • 명령형과 YAML 파일로 Pod 생성 두 가지 방법
  • describe / logs / exec 활용
  • kubectl explain 으로 필드 확인하는 법

참고 링크