[kubernetes] 쿠버네티스(kubernetes) 팟(pod)

김수연·2022년 5월 2일
0

Kubernetes

목록 보기
1/2
  1. pod이란
  2. pod 생성하기
    8080포트 /에 http request를 보내면 hello world 문자열을 내려주는 백엔드 애플리케이션을 실행하는 이미지를 만들어준다.(spring)

pod를 만드는 2가지 방법
(1) kubectl run
kubectl run 명령어를 통해 pod를 생성

# run 명령어를 통해 pod를 생성하고 image는 jaeho310/helloworld:1 을 사용
$ kubectl run mypod --image=jaeho310/helloworld:1
pod/mypod created

(2) yaml 파일로 상세스펙을 정의하여 생성

  1. pod 확인하기
    (1) pod 생성 확인하기
# pod의 생성 및 상태를 확인
$ kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          116s

(2) describe 명령어를 사용해 pod의 상세내역 확인하기

# pod의 상태를 자세하게 확인
$ kubectl describe pod mypod
Name:         mypod
Namespace:    default
Priority:     0
Node:         minikube/192.168.49.2
Start Time:   Mon, 02 May 2022 16:25:56 +0900
Labels:       run=mypod
Annotations:  <none>
Status:       Running
IP:           172.17.0.3
IPs:
  IP:  172.17.0.3
Containers:
  mypod:
    Container ID:   docker://fdc5362f5a7f7fdd17e738e6b39da40a42cd130b4679bb038735b20142209925
    Image:          jaeho310/helloworld:1
    Image ID:       docker-pullable://jaeho310/helloworld@sha256:c227f6acd0b6ec6e0329f484eeb4725bf8263fa6e6d0edb0d4412a026e414dfc
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 02 May 2022 16:27:25 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-pkthz (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-pkthz:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  2m42s  default-scheduler  Successfully assigned default/mypod to minikube
  Normal  Pulling    2m42s  kubelet            Pulling image "jaeho310/helloworld:1"
  Normal  Pulled     74s    kubelet            Successfully pulled image "jaeho310/helloworld:1" in 1m27.9581499s
  Normal  Created    73s    kubelet            Created container mypod
  Normal  Started    73s    kubelet            Started container mypod

(3) 쿠버네티스 클러스터의 이벤트로그로 확인하기
pod를 여러개 생성한 경우 get event 명령어로 전체 pod를 확인할 수 있다.

# kubectl get event를 입력해 확인하는 방법
$ kubectl get event
LAST SEEN   TYPE     REASON                    OBJECT          MESSAGE
14m         Normal   Starting                  node/minikube   Starting kubelet.
14m         Normal   NodeHasSufficientMemory   node/minikube   Node minikube status is now: NodeHasSufficientMemory
14m         Normal   NodeHasNoDiskPressure     node/minikube   Node minikube status is now: NodeHasNoDiskPressure
14m         Normal   NodeHasSufficientPID      node/minikube   Node minikube status is now: NodeHasSufficientPID
14m         Normal   NodeAllocatableEnforced   node/minikube   Updated Node Allocatable limit across pods
14m         Normal   NodeReady                 node/minikube   Node minikube status is now: NodeReady
14m         Normal   RegisteredNode            node/minikube   Node minikube event: Registered Node minikube in Controller
13m         Normal   Starting                  node/minikube
4m24s       Normal   Scheduled                 pod/mypod       Successfully assigned default/mypod to minikube
4m24s       Normal   Pulling                   pod/mypod       Pulling image "jaeho310/helloworld:1"
2m56s       Normal   Pulled                    pod/mypod       Successfully pulled image "jaeho310/helloworld:1" in 1m27.9581499s
2m55s       Normal   Created                   pod/mypod       Created container mypod
2m55s       Normal   Started                   pod/mypod       Started container mypod

(4) pod 컨테이너의 애플리케이션 로그 확인하기
kubectl logs 명령어를 통해 확인

  • 해당 로그는 스프링 애플리케이션이 시작될 때 뜨는 로그
$ kubectl logs mypod

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.1)

2022-05-02 07:27:26.420  INFO 1 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 11.0.1 on mypod with PID 1 (/app.jar started by root in /)
2022-05-02 07:27:26.423  INFO 1 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2022-05-02 07:27:27.820  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-05-02 07:27:27.834  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-05-02 07:27:27.835  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.46]
2022-05-02 07:27:27.916  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-05-02 07:27:27.917  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1426 ms
2022-05-02 07:27:28.529  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-05-02 07:27:28.538  INFO 1 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.748 seconds (JVM running for 3.202)

(5) pod 컨테이너에 직접 접근해 curl 명령어로 확인하기

# pod의 컨테이너에 직접 접근(앞 부분이 #모양으로 바뀜)
$ kubectl exec mypod -it -- sh

# pod에 접근했다면 curl 명령어로 웹 애플리케이션이 동작하는지 확인(앞 부분이 #모양)
$ curl get -x 127.0.0.1:8080

# response로 hello world를 내려주는 것을 확인
hello world

# 루트 디젤토리 확인(앞 부분이 #모양)
$ ls
app.jar  boot  docker-java-home  home  lib32  libx32  mnt  proc  run   srv  tmp  var
bin      dev   etc               lib   lib64  media   opt  root  sbin  sys  usr

# 빠져나올 때 ctrl+p+q를 누르면 container를 그대로 두고 나옴
# exec attach failed: error on attach stdin: read escape sequence
command terminated with exit code 126
  1. pod 삭제하기
# 이름으로 삭제
$ kubectl delete pod mypod
pod "mypod" deleted

# 연습용 pod를 많이 생성했다면 모두 삭제
# 네임스페이스를 지정한적이 없으므로 모든 resource는 default 네임스페이스에 올라간다.
# default 네임스페이스에 있는 모든 pod을 삭제할 수도 있다. 
$ kubectl delete pod --all -n default

$ kubectl delete {resource} 명령어를 사용해서 쿠버네티스 리소스를 삭제할 수 있다.

삭제할때는-A --all 등을 사용해서 한번에 삭제할 수 있으나,

all이 어떤 부분을 의미하는지 정확하게 확인한 상태가 아니면 운영쪽에서는 사용을 안하는게 좋다.

profile
Quien no arriesga, no gana nada

0개의 댓글