1 분 소요

Service

kubernetes Service

동일한 서비스를 제공하는 Pod 그룹의 단일 진입점을 제공

파드 label을 기준으로 파드들을 하나의 Virtual ip (Load Balancer IP라고 도 함)로 묶음

따라서, sevice는 load balancer로서 replicas에 작업을 균등하게 분배하는 역할을 한다.

service

  • definition 예시
# Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webui
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.14
# Service

apiVersion: v1
kind: Service
metadata:
  name: webui-svc
spec:
  clusterIP: 10.96.100.100(생략가능. 생략하면 Virtual IP 자동 생성)
  selector:
    app: webui
  ports:
  - protocol: TCP
    port: 80 (Cluster IP의 port)
    targetPort: 80 (service로 묶인 pod들의 port)

Service 조회

  • kubectl get svc

Service Type

ClusterIP(default)

  • Pod 그룹의 Virtual IP 생성
  • 클러스터 내부에서만 접근 가능

NodePort

  • clusterIP를 아우르는 상위 개념임

  • ClusterIP가 생성된 후 모든 Worker Node에 외부에서 접속 가능한 포트가 예약

LoadBalancer

  • 클라우드 인프라(AWS, Azure, GCP 등)나 오픈스택 클라우드에 적용
  • LoadBalancer를 자동으로 프로 비전하는 기능을 지원

ExternalName

  • 클러스터 안에서 외부에 접속 시 사용할 도메인을 등록해서 사용
  • 클러스터 도메인이 실제 외부 도메인으로 치환되어 동작

NodePort 예시

apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo
spec:
  replicas: 3
  selector:
    matchLabels:
      app: echo
  template:
    metadata:
      labels:
        app: echo
    spec:
      containers:
        - name: echo
          image: ghcr.io/subicura/echo:v1

---
apiVersion: v1
kind: Service
metadata:
  name: echo
spec:
  type: NodePort
  ports:
    - port: 3000
      protocol: TCP
      nodePort: 32000
  selector:
    app: echo

Headless Service

  • 로드밸런싱이 필요없거나 단일 서비스 IP가 필요 없는 경우에 헤드리스 서비스를 사용
  • 헤드리스 서비스에 셀렉터(selector)를 설정하면 api를 통해서 확인할 수 있는 엔드포인트(Endpoints)가 만들어 짐
  • Service와 연결된 Pod의 endpoint로 DNS 레코드가 생성됨
    • Control Plane의 coreDNS에 레코드가 저장되어 Pod들의 endpoint에 DNS resolving service 지원가능
  • Pod의 DNS 주소: pod-ip-addr.namespace.pod.cluster.local
  • Service의 clusterIP를 None으로 지정해주면 headless Service임
# headless Service  

apiVersion: v1
kind: Service
metadata:
  name: headless-service
spec:
  type: ClusterIP
  clusterIP: None
  selector:
    app: webui
  ports:
  - protocol: TCP
    port: 80 
    targetPort: 80 

댓글남기기