[k8s] 쿠버네티스 기초 정리

Woong·2022년 9월 13일
0

Docker, k8s

목록 보기
7/9

Pod

  • 컨테이너 애플리케이션의 기본 단위
  • 1개 이상의 컨테이너로 구성된 컨테이너의 집합
    • 같은 pod 내 컨테이너들은 네임스페이스를 공유한다.
  • yml 파일을 통해 pod 생성, 삭제 등 가능
    • apiVersion : 오브젝트 API 버전
    • kind : 리소스 종류.
      • Pod, Service, ReplicaSet, Deployment
    • metadata : name 등 리소스 메타데이터
      • metadata.name 은 고유해야한다.
    • spec : 리소스 생성을 위한 상세 정보
      • image (컨테이너 이미지), ports

Replica Set

  • 일정 개수의 pod 를 유지하는 컨트롤러
    • 정해진 수의 동일한 pod 가 항상 실행되도록 관리
    • 노드에서 pod 사용 불가시 다른 노드에서 pod 다시 생성
  • 노드를 직접 관리하는 대신 replica set을 통해 관리
    • kind 값이 ReplicaSet
    • spec.replicas : 동일한 pod 를 몇개 유지시킬지 설정
    • spec.template : pod 생성시 사용할 템플릿 정의. pod spec, pod template이라 지칭
    • spec.selector.matchLabel : 정의된 label 을 라벨 셀렉터로 매칭
      • pods 의 metadata.labels 에 정의된 label 과 동일하면 매칭
        • ※ replica set 자체의 metadata.labels 가 아니니 주의
      • 예제의 경우 app: my-nginx-pods-label 을 기준으로 매칭
  • ex)
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replcaset-nginx
spec:
  # replica set 정의
  replicas: 3
  selector:
    matchLabels:
      app: my-nginx-pods-label
  template:
    #pod 정의
    metadata:
      name: my-nginx-pod
      labels:
        app: my-nginx-pods-label
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Deployment

  • Replica set, pod 정의, 배포를 관리하는 오브젝트
    • Replica set 의 상위 오브젝트
      • Replica set 은 단순히 pod 수 유지만 하고, Deployment 는 여기에 버전 관리 등 기능 추가
    • 컨테이너 애플리케이션의 업데이트와 배포 관리
      • revision(변경사항) 을 저장해 롤백하거나, pod 롤링 업데이트 전략을 지정해 무중단 배포 등
    • kind 값은 Deployment
  • ex)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx-deployment
spec:
  # replica set 정의
  replicas: 3
  selector:
    matchLabels:
      app: my-nginx
  template:
    #pod 정의
    metadata:
      name: my-nginx-pod
      labels:
        app: my-nginx 
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Service

  • pod 를 연결하고 외부에 노출하는데 필요한 오브젝트
    • pod IP는 영속적이지 않고, 항상 변할 수 있음
    • deployment 생성시에는 pod 를 외부 노출하지 않고, 내부 포트만 정의함
    • 외부로 노출하기 위해선 service 오브젝트가 필요
  • pod 에 접근하기 위한 규칙을 정의하는 오브젝트
    • 여러 pod 에 접근 가능한 고유한 domain name 부여
    • 여러 pod 접근시, 로드 밸런서 기능 수행
    • 로드 밸런서, 클러스터 노드의 포트 등을 통해 pod 를 외부로 노출
  • service type
    • clusterIP 타입 : 쿠버네티스 내부에서만 pod 접근할 때 사용 (default)
      • 외부에서 포트 접근 불가
    • NodePort 타입 : pod 에 접근할 수 있는 포트를 클러스터 모든 노드에 개방
      • 외부에서 포트 접근 가능
      • 접근 포트는 기본 랜덤 지정이고, nodePort 로 특정 포트 접근 설정 가능
    • LoadBalance 타입 : 로드밸런서를 동적으로 프로비저닝해 pod에 연결
      • 외부에서 pod 접근 가능
      • 일반적으로 클라우드 플랫폼 환경에서만 사용 가능
  • ex)
    • port: 클러스터 내부에서 노출되는 port
    • targetPort : pod 내 application port
    • nodePort: (NodePort 에서 사용) 클러스터 외부에서 접근할 수 있도록 노드에 개방된 port
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  type: NodePort
  ports:
  - targetPort: 80
    port: 80
    nodePort: 30008
  selector:
    app: my-nginx

kubectl 명령어 정리

  • kubectl apply : Pod, replica set 등 리소스 생성 혹은 업데이트
    • ex) kubectl apply -f nginx-pod.yaml
  • kubectl describe : 리소스 상세 정보 조회
    • ex) kubectl describe pods my-nginx-pod
  • kubectl exec : pod 컨테이너에 명령어 전달
    • docker 처럼 -it 옵션으로 쉘 유지 가능
    • -c : 명령을 실행할 컨테이너 지정
      • ex) kubectl exec -it my-nginx-pod -c my-second-container bash
  • kubectl logs : pod 로그 조회
  • kubectl delete : pod 삭제
    • ex) kubectl delete -f nginx-pod.yaml (yaml 파일로 삭제)
    • ex) kubectl delete pod my-nginx-pod (pod 이름으로 삭제)
  • kubectl api-resouces : 명령어 줄임말 조회
    • pods 대신 po, replicasets 대신 rs 등
  • kubectl get : 오브젝트 정보 조회
    • ex) kubectl get pods, kubectl get deployment
    • kubectl get pods --show-labels : pod 목록을 label 과 함께 출력

reference

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN