Kubernetes - kubectl 명령어 실습

category DevOps/Kubernetes 2021. 11. 14. 21:57
728x90
반응형

kubectl 개념

kubectl 이란?

쿠버네티스 API에 요청할때 사용하는 명령어

kubectl 명령어 구조

kubectl [command] [TYPE] [NAME] [flags]
  • command : 자원에 실행할 명령 (create, get, delete, edit ...)
  • TYPE : 자원의 타입 (node, pod, service ...)
  • NAME : 자원의 이름
  • flags : 부가적으로 설정할 옵션 (--help, -o options)
예) kubectl get pod webserver -o wide
webserver 이름의 POD를 상세히 조회한다.

 

kubectl - node 명령어

node 조회하기

root@master:~# kubectl get nodes
NAME                 STATUS   ROLES                  AGE     VERSION
master.example.com   Ready    control-plane,master   2d22h   v1.22.3
node1.example.com    Ready    <none>                 2d21h   v1.22.3
node2.example.com    Ready    <none>                 2d21h   v1.22.3

 

node 상세히 조회하기

root@master:~# kubectl get nodes -o wide
NAME                 STATUS   ROLES                  AGE     VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
master.example.com   Ready    control-plane,master   2d22h   v1.22.3   10.100.0.104   <none>        Ubuntu 20.04.3 LTS   5.11.0-40-generic   docker://20.10.10
node1.example.com    Ready    <none>                 2d21h   v1.22.3   10.100.0.101   <none>        Ubuntu 20.04.3 LTS   5.11.0-40-generic   docker://20.10.10
node2.example.com    Ready    <none>                 2d21h   v1.22.3   10.100.0.102   <none>        Ubuntu 20.04.3 LTS   5.11.0-40-generic   docker://20.10.10

node 정보 조회하기

root@master:~# kubectl describe node master.example.com 
Name:               master.example.com
Roles:              control-plane,master
...
Events:              <none>

 

kubectl - pod 명령어

pod 생성

root@master:~# kubectl run webserver --image=nginx:1.14 --port 80
pod/webserver created

pod 조회

root@master:~# kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
webserver   1/1     Running   0          27s

pod 상세히 조회

root@master:~# kubectl describe pod webserver 
Name:         webserver
Namespace:    default
Priority:     0
Node:         node1.example.com/10.100.0.101
Start Time:   Sun, 14 Nov 2021 20:41:56 +0900
Labels:       run=webserver
Annotations:  <none>
Status:       Running
IP:           10.36.0.1
...
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  56s   default-scheduler  Successfully assigned default/webserver to node1.example.com
  Normal  Pulling    54s   kubelet            Pulling image "nginx:1.14"
  Normal  Pulled     41s   kubelet            Successfully pulled image "nginx:1.14" in 13.424085903s
  Normal  Created    37s   kubelet            Created container webserver
  Normal  Started    37s   kubelet            Started container webserver

pod 정보 조회

root@master:~# kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
webserver   1/1     Running   0          75s

pod webserver 확인해보기

root@master:~# curl -XGET http://10.36.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

kubectl - deployment 명령어

pod deployment 생성 

root@master:~# kubectl create deployment mainui --image=httpd --replicas=3
deployment.apps/mainui created

pod deployment 조회

root@master:~# kubectl describe deployments.apps mainui
Name:                   mainui
Namespace:              default
CreationTimestamp:      Sun, 14 Nov 2021 20:51:04 +0900
Labels:                 app=mainui
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=mainui
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
...
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  70s   deployment-controller  Scaled up replica set mainui-d77bf4d8f to 3

pod 조회

root@master:~# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
mainui-d77bf4d8f-6wm58   1/1     Running   0          2m15s   10.36.0.2   node1.example.com   <none>           <none>
mainui-d77bf4d8f-6xtdn   1/1     Running   0          2m15s   10.44.0.2   node2.example.com   <none>           <none>
mainui-d77bf4d8f-88n5x   1/1     Running   0          2m15s   10.44.0.1   node2.example.com   <none>           <none>
webserver                1/1     Running   0          11m     10.36.0.1   node1.example.com   <none>           <none>

pod mainui 확인해보기

root@master:~# curl -XGET http://10.36.0.2
<html><body><h1>It works!</h1></body></html>

 

kubectl - yaml, json 포멧 출력해보기

yaml 포멧으로 출력

root@master:~# kubectl get pod webserver -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-11-14T11:41:56Z"
  labels:
    run: webserver
  name: webserver
  namespace: default
  resourceVersion: "38320"
  uid: 94242f9b-7319-48f2-89c6-31c94975e132
  ...

json 포멧으로 출력

root@master:~# kubectl get pod webserver -o json
{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "creationTimestamp": "2021-11-14T11:41:56Z",
        "labels": {
            "run": "webserver"
        },
        "name": "webserver",
        "namespace": "default",
        "resourceVersion": "38320",
        "uid": "94242f9b-7319-48f2-89c6-31c94975e132"
    },
    ...
}

 

kubectl - container 내부 접속

root@master:~# kubectl exec webserver -it -- /bin/bash
root@webserver:/# ls
bin  boot  dev	etc  home  lib	lib64  media  mnt  opt	proc  root  run  sbin  srv  sys  tmp  usr  var

 

kubectl - pod 편집

root@master:~# kubectl edit deployment.apps mainui

 

kubectl - pod 삭제

root@master:~# kubectl delete pod webserver
pod "webserver" deleted
root@master:~# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
mainui-d77bf4d8f-4m6q7   1/1     Running   0          5m35s
mainui-d77bf4d8f-6wm58   1/1     Running   0          18m
mainui-d77bf4d8f-6xtdn   1/1     Running   0          18m
mainui-d77bf4d8f-88n5x   1/1     Running   0          18m
mainui-d77bf4d8f-tn9zw   1/1     Running   0          5m35s
root@master:~# kubectl delete deployments.apps mainui 
deployment.apps "mainui" deleted
root@master:~# kubectl get pods
NAME                     READY   STATUS        RESTARTS   AGE
mainui-d77bf4d8f-4m6q7   1/1     Terminating   0          5m51s
mainui-d77bf4d8f-6wm58   1/1     Terminating   0          18m
mainui-d77bf4d8f-6xtdn   1/1     Terminating   0          18m
mainui-d77bf4d8f-88n5x   1/1     Terminating   0          18m
mainui-d77bf4d8f-tn9zw   1/1     Terminating   0          5m51s
root@master:~# kubectl get pods
No resources found in default namespace.

 

kubectl - yaml 파일로 pod 생성

root@master:~# kubectl create -f webserver-pod.yaml
pod/webserver created
root@master:~# kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
webserver   1/1     Running   0          4s
root@master:~# kubectl get pods -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
webserver   1/1     Running   0          8s    10.36.0.1   node1.example.com   <none>           <none>
root@master:~#
728x90
반응형