본문 바로가기

About/Kubernetes

[k8s][따배쿠] 쿠버네티스 아키텍처 - yaml 템플릿과 API Version

https://www.youtube.com/watch?v=9kk_C4nUmWc&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c&index=9 

본 포스팅은 따배쿠(따라하면서 배우는 쿠버네티스) 4-2. 쿠버네티스 아키텍처 - yaml 템플릿과 API 편을 보고 정리한 내용입니다.

 


YAML 템플릿


YAML 템플릿이란

https://ko.wikipedia.org/wiki/YAML

 

YAML - 위키백과, 우리 모두의 백과사전

YAML은 XML, C, 파이썬, 펄, RFC2822에서 정의된 e-mail 양식에서 개념을 얻어 만들어진 '사람이 쉽게 읽을 수 있는' 데이터 직렬화 양식이다. 2001년에 클라크 에반스가 고안했고, Ingy dot Net 및 Oren Ben-Kiki

ko.wikipedia.org

- 사람이 쉽게 읽을 수 있는 데이터 직렬화 양식

- HTML, XML과 같은 Markup Language

- 기본 문법

  • 구조화된 데이터를 표현하기 위한 데이터 포맷
  • Python 처럼 들여쓰기로 데이터 계층을 표기
  • 들여쓰기를 할 때에는 Tab이 아닌 Space Bar를 사용
  • 가독성이 좋아 설정 파일에 적합한 형식
  • Scalar 문법 : ':'를 기준으로 key: value 설정
  • 배열 문법 : '-' 문자로 여러 개를 나열
  • 공식 사이트 : https://yaml.org/
 

The Official YAML Web Site

 

yaml.org

YAML 파일 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
# nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod
  namespace: orange
spec:
  containers:
  - image: nginx:1.14 # Scalar 문법
    name: nginx
    ports:  # 배열 문법
    - containerPort: 80
    - containerPort: 443
cs

 

API Version


- alpha -> beta -> stable

- kubernetes Object 정의 시 apiVersion 필요

- kubernetes가 update하는 API가 있으면 새로운 API가 생성됨

- API Object의 종류 및 버전

  • Deployment : apps/v1
  • Pod : v1
  • ReplicaSet : apps/v1
  • ReplicationController : v1
  • Service : v1
  • PersistentVolume : v1

따라서 만약 Pod를 생성할 때 다음과 같이 apiVersion의 값이 v1이 아니라 apps/v1으로 잘못 설정하게 되는 경우

1
2
3
4
5
6
7
8
9
10
11
12
13
# nginx.yaml
apiVersion: apps/v1 # Pod의 apiVersion은 v1
kind: Pod
metadata:
  name: mypod
  namespace: orange
spec:
  containers:
  - image: nginx:1.14 # Scalar 문법
    name: nginx
    ports:  # 배열 문법
    - containerPort: 80
    - containerPort: 443
cs

 

해당 yaml로 리소스를 생성하게되면 다음과 같은 에러가 발생합니다.

apiVersion 에러

그렇다면 모든 리소스의 apiVersion을 외워야 할까요? 그렇지 않습니다

kubectl의 explain 명령어를 통해 리소스의 Documentation을 출력할 수 있는데, 여기에 apiVersion이 명시되어 있습니다.

1
$ kubectl explane [오브젝트명] 
cs

 

예를 들어 Pod의 Documentation은 다음과 같습니다. (3번째 줄에 VERSION 출력)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@k8s-master jh]$ kubectl explain pod
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's 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
 
cs

쿠버네티스의 YAML 파일과 API Version에 대하여 알아보았습니다.