02. YAML ๋˜๋Š” JSON ๋””์Šคํฌ๋ฆฝํ„ฐ๋กœ Pod ์ƒ์„ฑํ•˜๊ธฐ

2021. 4. 3. 21:51ใ†๐ŸŽฏ OpenSource/K8S

โ€ป ๋ณธ ๊ฒŒ์‹œ๊ธ€์€ ๋„์„œ <์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์ธ ์•ก์…˜>์„ ํ† ๋Œ€๋กœ ์ •๋ฆฌ/ํ•™์Šตํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

Pod, Deployment, Service ๋“ฑ๊ณผ ๊ฐ™์€ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ๋ฆฌ์†Œ์Šค๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค REST API ์—”๋“œํฌ์ธํŠธ์— ์ƒ์„ฑํ•˜๋ ค๋Š” ๋ฆฌ์†Œ์Šค์˜ ๋ช…์„ธ์„œ๋ฅผ ์ „์†กํ•˜์—ฌ ์ƒ์„ฑํ•œ๋‹ค.

์ด๋•Œ ๋ช…์„ธ์„œ๋Š” yaml, json ๊ณผ ๊ฐ™์€ ํŒŒ์ผ ํ˜•์‹์„ ๊ฐ€์ง„๋‹ค.

 

3.2.1 ๊ธฐ์กด pod์˜ yaml ๋””์Šคํฌ๋ฆฝํ„ฐ

-o yaml ์˜ต์…˜์œผ๋กœ pod์— ๋Œ€ํ•œ yaml ์ •์˜๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

root@master001:~# kubectl get po kubia-manual -o yaml

yaml ์ •์˜๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์„ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋”๋ณด๊ธฐ

root@master001:~# kubectl run kubia --image=luksa/kubia --port=8080 --generator=run/v1

root@master001:~# kubectl get po kubia -o yaml

apiVersionv1   # ํ•ด๋‹น object๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” kubernetes api ๋ฒ„์ „

kindPod        # ์ƒ์„ฑํ•  object์˜ ์œ ํ˜•

metadata:  # object์˜ ์ด๋ฆ„, uid, namespace, label ๋“ฑ์— ๋Œ€ํ•œ metadata

  annotations:

    cni.projectcalico.org/podIP172.30.254.29/32

    cni.projectcalico.org/podIPs172.30.254.29/32

  creationTimestamp"2021-04-03T13:14:06Z"

  labels:

    runkubia

  managedFields:

  - apiVersionv1

    fieldsTypeFieldsV1

    fieldsV1:

      f:metadata:

        f:labels:

          .: {}

          f:run: {}

      f:spec:

        f:containers:

          k:{"name":"kubia"}:

            .: {}

            f:image: {}

            f:imagePullPolicy: {}

            f:name: {}

            f:ports:

              .: {}

              k:{"containerPort":8080,"protocol":"TCP"}:

                .: {}

                f:containerPort: {}

                f:protocol: {}

            f:resources: {}

            f:terminationMessagePath: {}

            f:terminationMessagePolicy: {}

        f:dnsPolicy: {}

        f:enableServiceLinks: {}

        f:restartPolicy: {}

        f:schedulerName: {}

        f:securityContext: {}

        f:terminationGracePeriodSeconds: {}

    managerkubectl-run

    operationUpdate

    time"2021-04-03T13:14:06Z"

  - apiVersionv1

    fieldsTypeFieldsV1

    fieldsV1:

      f:metadata:

        f:annotations:

          .: {}

          f:cni.projectcalico.org/podIP: {}

          f:cni.projectcalico.org/podIPs: {}

    managercalico

    operationUpdate

    time"2021-04-03T13:14:07Z"

  - apiVersionv1

    fieldsTypeFieldsV1

    fieldsV1:

      f:status:

        f:conditions:

          k:{"type":"ContainersReady"}:

            .: {}

            f:lastProbeTime: {}

            f:lastTransitionTime: {}

            f:status: {}

            f:type: {}

          k:{"type":"Initialized"}:

            .: {}

            f:lastProbeTime: {}

            f:lastTransitionTime: {}

            f:status: {}

            f:type: {}

          k:{"type":"Ready"}:

            .: {}

            f:lastProbeTime: {}

            f:lastTransitionTime: {}

            f:status: {}

            f:type: {}

        f:containerStatuses: {}

        f:hostIP: {}

        f:phase: {}

        f:podIP: {}

        f:podIPs:

          .: {}

          k:{"ip":"172.30.254.29"}:

            .: {}

            f:ip: {}

        f:startTime: {}

    managerkubelet

    operationUpdate

    time"2021-04-03T13:14:42Z"

  namekubia

  namespacedefault

  resourceVersion"6920138"

  uid73cae115-aeb9-4618-804d-38c08ed4e8cf

spec:     # ํ•ด๋‹น ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ •์˜/๋‚ด์šฉ (pod์— ์†ํ•œ container ์ •๋ณด, volume ๋“ฑ)

  containers:

  - imageluksa/kubia

    imagePullPolicyAlways

    namekubia

    ports:

    - containerPort8080

      protocolTCP

    resources: {}

    terminationMessagePath/dev/termination-log

    terminationMessagePolicyFile

    volumeMounts:

    - mountPath/var/run/secrets/kubernetes.io/serviceaccount

      namedefault-token-xgl85

      readOnlytrue

  dnsPolicyClusterFirst

  enableServiceLinkstrue

  nodeNameworker002

  preemptionPolicyPreemptLowerPriority

  priority0

  restartPolicyAlways

  schedulerNamedefault-scheduler

  securityContext: {}

  serviceAccountdefault

  serviceAccountNamedefault

  terminationGracePeriodSeconds30

  tolerations:

  - effectNoExecute

    keynode.kubernetes.io/not-ready

    operatorExists

    tolerationSeconds300

  - effectNoExecute

    keynode.kubernetes.io/unreachable

    operatorExists

    tolerationSeconds300

  volumes:

  - namedefault-token-xgl85

    secret:

      defaultMode420

      secretNamedefault-token-xgl85

status:      # pod์™€ ๊ทธ ์•ˆ์˜ container์˜ ์ƒ์„ธํ•œ ์ƒํƒœ

  conditions:

  - lastProbeTimenull

    lastTransitionTime"2021-04-03T13:14:06Z"

    status"True"

    typeInitialized

  - lastProbeTimenull

    lastTransitionTime"2021-04-03T13:14:42Z"

    status"True"

    typeReady

  - lastProbeTimenull

    lastTransitionTime"2021-04-03T13:14:42Z"

    status"True"

    typeContainersReady

  - lastProbeTimenull

    lastTransitionTime"2021-04-03T13:14:06Z"

    status"True"

    typePodScheduled

  containerStatuses:

  - containerIDdocker://7fd367dc382a19a2628ee52c5a8975f9d04071aec4f88fec2a1f6b4c76333d20

    imageluksa/kubia:latest

    imageIDdocker-pullable://luksa/kubia@sha256:3f28e304dc0f63dc30f273a4202096f0fa0d08510bd2ee7e1032ce600616de24

    lastState: {}

    namekubia

    readytrue

    restartCount0

    startedtrue

    state:

      running:

        startedAt"2021-04-03T13:14:41Z"

  hostIP10.10.11.21

  phaseRunning

  podIP172.30.254.29

  podIPs:

  - ip172.30.254.29

  qosClassBestEffort

  startTime"2021-04-03T13:14:06Z"

  • Metadata : ์ด๋ฆ„, namespace, label, pod ์— ๊ด€ํ•œ ๊ธฐํƒ€ ์ •๋ณด
  • Spec : pod ์ž์ฒด์— ๋Œ€ํ•œ ์‹ค์ œ ๋ช…์„ธ์ด๋ฉฐ, k8s cluster์—์„œ ์œ ์ง€ํ•  ์ƒํƒœ์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด๋‹ค. (pod container, volume, ๊ธฐํƒ€ ๋ฐ์ดํ„ฐ ๋“ฑ)
  • Status : ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ pod์— ๋Œ€ํ•œ ํ˜„์žฌ ์ •๋ณด (Pod์˜ ์ƒํƒœ, ๊ฐ container ์„ค๋ช…๊ณผ ์ƒํƒœ, pod ๋‚ด๋ถ€ ip, ๊ธฐํƒ€ ์ •๋ณด ๋“ฑ)

Status์˜ ๊ฒฝ์šฐ object์˜ ์ƒ์„ฑ ๋ช…๋ น์ด ๋‚ด๋ ค์ง„ ์ดํ›„์˜ ๋ฆฌ์†Œ์Šค ์ƒํƒœ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

๋”ฐ๋ผ์„œ ์ƒˆ๋กœ์šด ๋ฆฌ์†Œ์Šค๋ฅผ ๋งŒ๋“ค๋•Œ status ๋ถ€๋ถ„์€ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

 

3.2.2 Pod๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฐ„๋‹จํ•œ YAML ์ž‘์„ฑํ•˜๊ธฐ

yaml ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜์—ฌ ์ƒˆ๋กœ์šด pod๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•œ๋‹ค.

root@master001:~/k8s_in_action/03_pod# cat kubia-manual.yaml
apiVersion: v1
kind: Pod
metadata:
  name: kubia-manual
spec:
  containers:
  - image: luksa/kubia
    name: kubia
    ports:
    - containerPort: 8080
      protocol: TCP
  • apiVersion : ๋ฆฌ์†Œ์Šค๋ฅผ ๋งŒ๋“œ๋ฉด์„œ ์‚ฌ์šฉํ•  API ๋ฒ„์ „. 
  • kind : Pod ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ฒ ๋‹ค๊ณ  ์ •์˜
  • metadata : pod์˜ ์ด๋ฆ„ ์ •์˜
  • spec/containers/image : ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€
  • spec/containers/ports : ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ˆ˜์‹ ํ•˜๋Š” ํฌํŠธ

 

   ๐Ÿ”น ์ปจํ…Œ์ด๋„ˆ ํฌํŠธ ์ง€์ •

     yaml ํŒŒ์ผ์—์„œ port๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„ ๋‹ค๋ฅธ pod๋“ค๊ณผ์˜ ํ†ต์‹ ์€ ๊ฐ€๋Šฅํ•˜๋‹ค.

     ํ•˜์ง€๋งŒ ๋ช…์‹œํ•จ์œผ๋กœ์จ ํ•ด๋‹น pod๊ฐ€ ์–ด๋–ค port๋กœ ๋…ธ์ถœ๋˜์–ด ์žˆ๋Š”์ง€ ๋น ๋ฅด๊ฒŒ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

   ๐Ÿ”น kubernetes API ์˜ค๋ธŒ์ ํŠธ ํ•„๋“œ ํ™•์ธํ•˜๊ธฐ

     object ์˜ ์†์„ฑ ํ™•์ธ

root@master001:~/k8s_in_action/03_pod# kubectl explain pods
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata     <Object>
     Standard object metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec <Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status       <Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   ๊ฐ ์†์„ฑ์— ๋Œ€ํ•œ ์„ธ๋ถ€ ์ •๋ณด ํ™•์ธ

root@master001:~/k8s_in_action/03_pod# kubectl explain pod.metadata
KIND:     Pod
VERSION:  v1

RESOURCE: metadata <Object>

DESCRIPTION:
     Standard objects metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

     ObjectMeta is metadata that all persisted resources must have, which
     includes all objects users must create.

FIELDS:
   annotations  <map[string]string>
     Annotations is an unstructured key value map stored with a resource that
     may be set by external tools to store and retrieve arbitrary metadata. They
     are not queryable and should be preserved when modifying objects. More
     info: http://kubernetes.io/docs/user-guide/annotations

   clusterName  <string>
     The name of the cluster which the object belongs to. This is used to
     distinguish resources with same name and namespace in different clusters.
     This field is not set anywhere right now and apiserver is going to ignore
     it if set in create or update request.
     
... ์ƒ๋žต

 

3.2.3 [ kubectl create ] Pod ์ƒ์„ฑํ•˜๊ธฐ

๋‹ค์Œ๊ณผ ๊ฐ™์ด yaml , json ๋ช…์„ธ์„œ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

root@master001:~/k8s_in_action/03_pod# kubectl create -f kubia-manual.yaml
pod/kubia-manual created

root@master001:~/k8s_in_action/03_pod# kubectl get po kubia-manual
NAME           READY   STATUS    RESTARTS   AGE
kubia-manual   1/1     Running   0          22s

 

์‹คํ–‰ ์ค‘์ธ pod์˜ ์ „์ฒด ์ •์˜ ํ™•์ธํ•˜๊ธฐ

์ด๋•Œ -o ์˜ต์…˜์„ yaml/json์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

root@master001:~/k8s_in_action/03_pod# kubectl get po kubia-manual -o yaml

 

3.2.4 Application log ๋ณด๊ธฐ

container๋กœ ๋™์ž‘ํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ๋กœ๊ทธ๋ฅผ ํ‘œ์ค€ ์ถœ๋ ฅ, ํ‘œ์ค€ ์—๋Ÿฌ๋กœ ๋‚จ๊ธด๋‹ค.

์ปจํ…Œ์ด๋„ˆ์˜ ๋กœ๊ทธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™•์ธํ•œ๋‹ค.

root@master001:~/k8s_in_action/03_pod# kubectl logs kubia-manual
Kubia server starting...

pod ์—์„œ -c ์˜ต์…˜์œผ๋กœ ํŠน์ • container์˜ ๋กœ๊ทธ๋งŒ ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค.

root@master001:~# kubectl logs kubia-manual -c kubia
Kubia server starting...

์ด๋•Œ pod๊ฐ€ ์‚ญ์ œ๋˜๋ฉด ๋กœ๊ทธ๋„ ์‚ญ์ œ๋œ๋‹ค. pod๊ฐ€ ์‚ญ์ œ๋œ ํ›„์—๋„ ๋กœ๊ทธ๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ค‘์•™์ง‘์ค‘์‹ ๋กœ๊น…์„ ์„ค์ •ํ•˜์—ฌ ๋ชจ๋“  ๋กœ๊ทธ๋ฅผ ์ค‘์•™ ์ €์žฅ์†Œ์— ์ €์žฅํ•˜๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

 

 

3.2.5 Pod์— ์š”์ฒญ ๋ณด๋‚ด๊ธฐ

 port forwarding์„ ํ•˜์—ฌ pod์— ์š”์ฒญ์„ ๋ณด๋‚ด ๋ณด๋„๋ก ํ•œ๋‹ค.

 

   ๐Ÿ”น ๋กœ์ปฌ ๋„คํŠธ์›Œํฌ ํฌํŠธ๋ฅผ pod์˜ ํฌํŠธ๋กœ ํฌ์›Œ๋”ฉ ํ•˜๊ธฐ

    service๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ํŠน์ • pod์™€ ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋กœ์ปฌ node์˜ ํฌํŠธ์™€ pod์˜ ํฌํŠธ๋ฅผ ๋งคํ•‘ํ•˜์—ฌ ํฌํŠธ ํฌ์›Œ๋”ฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•˜๋ฉด ๋กœ์ปฌ ๋จธ์‹ ์˜ 8888 port๋กœ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์„ ํ•ด๋‹น pod์˜ 8080 port๋กœ ํ–ฅํ•˜๊ฒŒ ํ•œ๋‹ค.

root@master001:~# kubectl port-forward kubia-manual 8888:8080 &
[1] 2949791

root@master001:~# curl localhost:8888
Handling connection for 8888
You ve hit kubia-manual

root@master001:~# kubectl logs kubia-manual
Kubia server starting...
Received request from ::ffff:127.0.0.1