ETC

Kubernetes - 명령어 모음

개발 일기92 2025. 1. 12. 18:37

1. Kubernetes 컨텍스트 및 클러스터 관리

 

  • 현재 컨텍스트 설정 또는 보기
kubectl config current-context        # Show the current context
kubectl config use-context <context> # Switch to a different context
kubectl config get-contexts           # List all available contexts

 

  • 클러스터 정보 확인
kubectl cluster-info                   # Display information about the cluster
kubectl get nodes                      # List all nodes in the cluster
kubectl describe node <node-name>      # Detailed information about a specific node


2. 자원 관리

  • 리소스 생성
kubectl create -f <file.yaml>          # Create resources defined in a YAML/JSON file
kubectl apply -f <file.yaml>           # Apply changes from a file (create/update)
kubectl run <name> --image=<image>     # Create a pod with a specific image

 

  • 리소스 보기
kubectl get pods                       # List all pods
kubectl get deployments                # List all deployments
kubectl get services                   # List all services
kubectl get configmaps                 # List all ConfigMaps
kubectl get secrets                    # List all Secrets
kubectl get all                        # List all resources in the namespace

 

  • 리소스 설명
kubectl describe pod <pod-name>        # Detailed information about a pod
kubectl describe deployment <name>     # Detailed information about a deployment
kubectl describe service <name>        # Detailed information about a service

 

  • 리소스 삭제
kubectl delete pod <pod-name>          # Delete a specific pod
kubectl delete deployment <name>       # Delete a specific deployment
kubectl delete -f <file.yaml>          # Delete resources defined in a YAML/JSON file
kubectl delete namespace <namespace>   # Delete a namespace

 

 

3. 로그 및 디버깅

  • 로그 보기
kubectl logs <pod-name>                # View logs of a pod
kubectl logs <pod-name> -c <container> # View logs for a specific container in a pod
kubectl logs -f <pod-name>             # Follow logs of a pod in real-time

 

  • 디버깅 리소스
kubectl exec -it <pod-name> -- /bin/bash   # Open a shell in a running pod
kubectl exec <pod-name> -- <command>       # Execute a command in a running pod
kubectl port-forward <pod-name> 8080:80    # Forward a local port to a pod's port
kubectl get events                         # View cluster events



4. 네임스페이스

  • 네임스페이스 관리
kubectl get namespaces                # List all namespaces
kubectl create namespace <namespace>  # Create a new namespace
kubectl delete namespace <namespace>  # Delete a namespace
  • 네임스페이스 전환
kubectl config set-context --current --namespace=<namespace>  # Set default namespace



5. 리소스 확장 및 업데이트

  • 배포 확장
kubectl scale deployment <name> --replicas=<number>  # Scale replicas
  • 롤링 업데이트
kubectl rollout status deployment/<name>            # Check the status of a rollout
kubectl rollout undo deployment/<name>              # Roll back to the previous version
kubectl rollout restart deployment/<name>           # Restart a deployment


6. ConfigMap 및 비밀

  • configmap
kubectl create configmap <name> --from-literal=key=value    # Create ConfigMap from a literal
kubectl create configmap <name> --from-file=<file>         # Create ConfigMap from a file
kubectl describe configmap <name>                         # Describe a ConfigMap
  • 시크릿 오브젝트
kubectl create secret generic <name> --from-literal=key=value # Create a secret from a literal
kubectl describe secret <name>                                # Describe a Secret


7. YAML 구성

  • 리소스를 YAML로 내보내기
kubectl get pod <pod-name> -o yaml          # Get a pod's YAML definition
kubectl get deployment <name> -o yaml       # Get a deployment's YAML definition

 

  • YAML 파일의 변경 사항 적용
kubectl apply -f <file.yaml>                # Apply changes from a YAML file



8. 기타

  • 리소스 사용량 보기
kubectl top nodes                          # View resource usage of nodes
kubectl top pods                           # View resource usage of pods
  • 실패한 pod 디버그
kubectl describe pod <pod-name>            # Check events related to the pod
kubectl logs <pod-name>                    # Check pod logs
kubectl exec -it <pod-name> -- /bin/bash   # Access the pod for manual inspection
  • 드라이런 모드
kubectl apply -f <file.yaml> --dry-run=client  # Check what will happen without making changes