Kubernetes

[Kubernetes] kubectl 기본 명령어 쉽게 정리

찌르비 2024. 2. 22. 12:19
반응형

 

  • kubectl get pods
  • kubectl describe
  • kubectl create 
  • kubectl apply
  • kubectl log 
  • kubectl exec  -it <pod-name> -- /bin/bash
  • kubectl delete

 

kubectl get pods

현재 실행 중인 모든 파드의 목록을 가져오는 데 사용된다.

Used to retrieve a list of resources. fetches a list of all currently running pods.

$ kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
mypod      1/1     Running   0          2m
another    2/2     Running   1          5m

 

 

 

kubectl describe pod <pod-name>

특정 파드에 대한 상세한 정보를 출력하는 데 사용된다.

Provides detailed information about a resource.

$ kubectl describe pod mypod
Name:         mypod
Namespace:    default
...
Containers:
  mycontainer:
    ...

 

 

 

kubectl create -f <yaml-file>

새로운 리소스를 생성한다. YAML 파일이나 JSON 파일에 정의된 Kubernetes 리소스를 읽어와 클러스터에 배포한다.

Creates a new resource. kubectl create -f <yaml-file> deploys the resource defined in a YAML file.

$ kubectl create -f mypod.yaml
pod/mypod created

 

 

kubectl apply -f <yaml-file>

클러스터에 새로운 리소스를 생성하거나 업데이트하는 데 사용된다.
(YAML 파일이나 JSON 파일 정의된 리소스)  

Applies changes or creates new resources.
kubectl apply -f <yaml-file> deploys or updates resources defined in a YAML file.

$ kubectl apply -f mypod.yaml
pod/mypod configured

 

 

kubectl apply vs kubectl create 차이

 

  리소스가 존재하지 않을 경우 리소스가 이미 존재할 경우
create ✅ 새로운 리소스가 생성 ⚠️ ERROR가 발생
apply ✅ 새로운 리소스가 생성 ✅ 새로운 업데이트 부분 반영

create는 아예 리소스가 없었을 때 사용하고.

apply는 이미 있는 리소스에 추가적인 업데이트가 있을 때 사용하면 된다

 

 

 

kubectl log <pod-name>

로그 확인 할 때 사용 kubectl logs <pod-name> -c <container-name>

$ kubectl logs mypod
This is the log output of mypod...

 

 

 

kubectl exec -it <pod-name> -- /bin/bash

클러스터 내에서 실행 중인 Pod에 들어가서 명령을 실행하는 데 사용

Executes a command inside a pod.

$ kubectl exec -it <pod-name> -- /bin/bash
root@mypod:/#

 

들어간 뒤에 현재 디렉토리를 확인하고 

pwd

 

나갈 때는 exit를 입력하면 된다

exit

 

 

 

kubectl delete <pod-name>

클러스터에서 Pod를 삭제하는 데 사용된다.

C:\>kubectl delete pod example
pod "example" deleted
반응형