Sangmun

쿠버네티스 오브젝트 본문

개발/kubernetes

쿠버네티스 오브젝트

상상2 2023. 2. 15. 20:23

쿠버네티스로 애플리케이션을 배포하고 관리하기

출처 : https://blog.christophetd.fr/using-k3s-for-command-and-control-on-compromised-linux-hosts/

쿠버네티스를 사용하여 사용자의 의도대로 어플리케이션을 배포하는 방법은 쿠버네티스 오브젝트를 정의하여 Master node(API Server)에 명령을 내리는것이다.

이때 명령을 내리는 표현방식은 yaml형식의 파일을 작성하여 REST API로 마스터 노드에 전달을 하면 된다.

 

쿠버네티스 오브젝트란 "쿠버네티스 클러스터를 이용해 애플리케이션을 배포하고 운영하기 위해 필요한 모든 쿠버네티스 리소르스를 의미하며

 

쿠버네티스 오브젝트가 될 수 있는 것들은 아래와 같다.

Pod : 애플리케이션

Replicaset : 복제 횟수

Node, Namespace : 어디에(서버 등)

Deployment : 배포 방식

Service, Endpoints : 트래픽 로드밸린싱

 

이러한 오브젝트에 대한 요구사항을 yaml 파일로 작성해서 master node에 api 형식으로 보내게 된다.

 

아래의 spec 이하에 작성된 내용이 이용자가 최종적으로 원하는 상태이며 간단한 설명을 하자면 nginx 이미지를 다운로드를 받고 80번 포트를 오픈한 컨테이너를 2개 생성하라는 의미이다.

다른 필드의 의미는 아래와 같다.

  • apiVersion : 오브젝트를 생성할 떄 사용하는 API 버전
  • kind : 생성하고자 하는 오브젝트 종류
  • metadata : 오브젝트를 구분 지을 수 있는 정보 (name, label 등)
  • spec : 사용자가 원하는 오브젝트 상태
# yaml 파일 예시
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
  spec:
    containers:
    - name: nginx
      image: nginx:1.14.2
      ports:
      - containerPort: 80

또한 status 필드로 현재의 쿠버네티스 클러스터의 상태를 확인할 수 있다. 

# status 필드 예시
status:
  availableReplicas: 2
  conditions:
    - lastTransitionTime: '2022-02-06T12:28:39Z'
      lastUpdateTime: '2022-02-06T12:28:39Z'
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: 'True'
      type: Available
    - lastTransitionTime: '2022-02-06T12:28:16Z'
      lastUpdateTime: '2022-02-06T12:28:39Z'
      message: ReplicaSet "my-app-5b7548d6b" has successfully progressed.
      reason: NewReplicaSetAvailable
      status: 'True'
      type: Progressing
  observedGeneration: 1
  readyReplicas: 2
  replicas: 2
  updatedReplicas: 2

 

따라서 쿠버네티스를 이용하여 클러스터를 구성하는 과정을 아래를 목적으로 한다고 할 수 있다.

spec(쿠버네티스가 달성해야할 목표) == status(오브젝트의 현재 상태)

 

1. 사용자가 쿠버네티스 오브젝트 yaml 파일 작성(yaml 파일안에 spec 필드가 요구사항)

2. 쿠버네티스 API를 이용해서 쿠버네티스에 생성 요청

3. 쿠버네티스 API 서버가 오브젝트 파일의 spec 파일을 읽고 오브젝트 생성

4. 쿠버네티스 ControllerManager가 spec과 status를 비교하면서 계속 조정하고 상태를 업데이트함

 

Kubectl 기본 명령어

Kubectl api-resources

- 쿠버네티스 클러스터에서 사용가능한 오브젝트 목록 조회

 

kubectl explain <type>

- 쿠버네티스 오브젝트의 설명과 1레벨 속성들의 설명(apiVersion, kind, metadata 등)

 

kubectl explain <type>.<fieldNmae>[.<fieldName>]

- ex) kubectl explain pods.spec.containers

- 쿠버네티스 오브젝트 속성들의 구체적인 설명(json 형식)

 

kubectl get nodes

- 쿠버네티스 클러스터에서 속한 노드 목록 조회

 

kubectl scale -f <object-file-name> --replicas=#

- kubectl scale -f deployment.yaml --replicas=3

- 애플리케이션 배포 개수를 조정

 

kubectl diff -f <object-file-name>

- kubectl diff -f deployment.yaml

- 현재 실행 중인 오브젝트 설정과 입력한 파일의 차이점 분석

 

kubectl edit <type>/<name>

- kubectl edit deployment/nginx-deployment: replicas를 4로 변경

- 쿠버네티스 오브젝트의 spec을 editor로 편집

 

kubectl port-forward <type>/<name> <local-port>:<container-port>

- 로컬에서 쿠버네티스 오브젝트(pod or container)에 통신할 수 있도록 포트 포워딩

 

kubectl attach <type>/<name> -c <container-name>

- 현재 실행중인 컨테이너 프로세스에 접속하여 로그 확인

 

kubectl logs <type>/<name> -c <container-name> -f

- 현재 실행중인 컨테이너 프로세스에 모든 로그 출력 (-f : watch 모드)

 

 

출처 : 패스트캠퍼스

 

'개발 > kubernetes' 카테고리의 다른 글

kubernetes label and selector  (0) 2023.02.15
kubernetes pod  (0) 2023.02.15
Comments