[Kubernetes] kubectl

🔠쿠버네티스 클러스터를 관리하는 동작 대부분은 kubctl 명령어로 실행
kubectl [command] [TYPE] [NAME] [flags]
  • command : 자원에 실행하려는 동작
    e.g : create, get, delete
  • TYPE : 자원 타입
    e.g : pods, service, ingress
  • NAME : 자원 이름
  • FLAG : 부가 옵션
#echo server 동작시키는 kubectl 명령
$ kubectl run echoserver --generator=run-pod/v1 --image="k8s.gcr.io/echoserver:1.10" --port8080
pot/echoserver created

kubectl get pods

$ kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
hello-minikube-84bcf5d5d8-rwvjg        1/1     Running   0          8h
kubernetes-bootcamp-5485cc6795-bzx9v   1/1     Running   0          6h55m
  • NAME : 파드 이름을 표시
  • READY : 파드의 준비 상태를 표시
    0/1 이면 현재 파드는 생성되었으나 사용할 준비가 되지 않음
    1/1 이면 파드가 생성되었고 사용할 준비가 끝났다는 뜻
  • STATUS : 파드의 현재 상태
    • Running
    • Terminating
    • ContainerCreating
  • RESTARTS : 해당 파드가 몇 번 재시작했는지를 표시
  • AGE : 파드 생성후 지난 시간

kubectl get services

$ kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   8h
  • NAME : 서비스 이름 표시
  • TYPE : 서비스 타입
  • CLUSTER-IP : 클러스터 안에서 사용되는 IP
  • EXTERNAL-IP : 클러스터 외부에서 접속할때 사용하는 IP
  • PORT(S) : 해당 서비스에 접속하는 포트를 표시
  • AGE : 자원을 생성한 후 얼마나 시간이 지났는지 나타냄

kubectl get deployments

$ kubectl get deployments
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
cozserver-deployment   2/2     2            2           26h
hello-minikube         1/1     1            1           2d1h
kubernetes-bootcamp    1/1     1            1           47h
  • NAME : 클러스터에 배포한 디플로이먼트 이름
  • READY : 파드의 준비 상태를 표시
    0/1 이면 현재 파드는 생성되었으나 사용할 준비가 되지 않음
    1/1 이면 파드가 생성되었고 사용할 준비가 끝났다는 뜻
  • UP-TO-DATE : 디플로이먼트 설정에 정의한 대로 동작 중인 신규 파드 개수
  • AVAILABLE : 서비스 가능한 파드 개
  • AGE : 디플로이먼 생성후 지난 시간

kubectl port-forward

$ kubectl port-forward svc/echoserver 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080

kubeconfig 환경 변수

kubectl은 기본적으로 $HOME/.kube/config 파일에서 클러스터, 인증, 컨텍스트 정보를 읽어 들입니다.
이러한 클러스터 구성 정보를 Kubeconfig

클러스터에서 사용할 수 있는 자원들은 아래와 같은 명령으로 확인 가능

$ kubectl api-resources
NAME                              SHORTNAMES   APIVERSION       NAMESPACED   KIND
bindings                                       v1               true         Binding
componentstatuses                 cs           v1               false        ComponentStatus
configmaps                        cm           v1               true         ConfigMap
endpoints                         ep           v1               true         Endpoints
events                            ev           v1               true         Event
limitranges                       limits       v1               true         LimitRange
namespaces                        ns           v1               false        Namespace
nodes                             no           v1               false        Node
persistentvolumeclaims            pvc          v1               true         PersistentVol...ntvolumes                 
pods                              po           v1               true         Pod
podtemplates                                   v1               true         PodTemplate
replicationcontrollers            rc           v1               true         Replicat...quotas
secrets                                        v1               true         Secret
serviceaccounts                   sa           v1               true         ServiceAccount
services                          svc          v1               true         Service
mutatingwebhookconfigurations                  ..on.k8s.io/v1   false        MutatingWebhookConfiguration
validatingwebhookconfigurations                ..on.k8s.io/v1   false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     ..ons.k8s.io/v1  false        CustomResourceDefinition
apiservices                                    ..on.k8s.io/v1   false        APIService
controllerrevisions                            apps/v1          true         ControllerRevision
daemonsets                        ds           apps/v1          true         DaemonSet
deployments                       deploy       apps/v1          true         Deployment
replicasets                       rs           apps/v1          true         ReplicaSet
statefulsets                      sts          apps/v1          true         StatefulSet
tokenreviews                                   ..on.k8s.io/v1   false        TokenReview
localsubjectaccessreviews                      ..on.k8s.io/v1   true         LocalSubjectAccessReview
selfsubjectaccessreviews                       ..on.k8s.io/v1   false        SelfSubjectAccessReview
selfsubjectrulesreviews                        ..on.k8s.io/v1   false        SelfSubjectRulesReview
subjectaccessreviews                           ..on.k8s.io/v1   false        SubjectAccessReview
horizontalpodautoscalers          hpa          autoscaling/v2   true         HorizontalPodAutoscaler
cronjobs                          cj           batch/v1         true         CronJob
jobs                                           batch/v1         true         Job
certificatesigningrequests        csr          ..tes.k8s.io/v1  false        CertificateSigningRequest
leases                                         ..on.k8s.io/v1   true         Lease
endpointslices                                 ..ry.k8s.io/v1   true         EndpointSlice
events                            ev           events.k8s.io/v1 true         Event
flowschemas                                    ..ver.k8s.io/v1beta3   false        FlowSchema
prioritylevelconfigurations                    ..er.k8s.io/v1beta3 false        PriorityLevelConfiguration
ingressclasses                                 networking.k8s.io/v1  false        IngressClass
ingresses                         ing          networking.k8s.io/v1  true         Ingress
networkpolicies                   netpol       networking.k8s.io/v1  true         NetworkPolicy
runtimeclasses                                 node.k8s.io/v1   false        RuntimeClass
poddisruptionbudgets              pdb          policy/v1        true         PodDisruptionBudget
clusterrolebindings                            ..on.k8s.io/v1   false        ClusterRoleBinding
clusterroles                                   ..on.k8s.io/v1   false        ClusterRole
rolebindings                                   ..on.k8s.io/v1   true         RoleBinding
roles                                          ..on.k8s.io/v1   true         Role
priorityclasses                   pc           ..ng.k8s.io/v1   false        PriorityClass
csidrivers                                     storage.k8s.io/v1 false        CSIDriver
csinodes                                       storage.k8s.io/v1 false        CSINode
csistoragecapacities                           storage.k8s.io/v1 true         CSIStorageCapacity
storageclasses                    sc           storage.k8s.io/v1 false        StorageClass
volumeattachments                              storage.k8s.io/v1 false        VolumeAttachment

Leave a Comment