쿠버네티스에서 Service는 라벨 셀렉터를 사용해 라벨에 해당하는 Pod들을 서비스에 매칭시키는데, 만약 새로 추가된 Pod가 해당하는 라벨을 달고있다면 Service는 해당 Pod로 트래픽을 보내는 방법을 알게된다.
이게 가능한 이유는 Service가 실제로는 Endpoints라는 오브젝트에 해당 하는 Pod들을 매핑 시키는데, Service는 이 Endpoints에 매핑된 Pod들의 IP정보를 가지고 Pod에게 트래픽을 전달하게된다.
Endpoints
Endpoints는 Service가 트래픽을 전달하고자 하는 Pod의 집합이다.
Endpoints는 실제로 Endpoints Controller에 의해 관리되는데, 이 Endpoints Controller는 마스터 노드의 Controller Manager에 의해 관리된다.
Endpoints Controller는 API Server를 감시하고있다가 Service에 유효한 Pod가 추가되면 해당 Pod를 Endpoints 목록에 추가하고, Pod가 삭제되면 해당 Pod를 Endpoints 목록에서 삭제한다.
Endpoints 수동 설정
Endpoints는 Endpoints Controller에 의해 자동으로 설정되지만 수동으로 설정해야 하는 경우도 있다.
Service에 라벨 셀렉터가 없는 경우에는 Pod를 할당할 수 없기 때문에 Endpoints가 생성되지 않는데, 이 경우에는 Endpoints를 수동으로 생성해야한다.
Endpoints를 수동으로 생성해야하는 경우
•
Service를 통해 클러스터 외부에 있는 서버로 트래픽을 보내야 하는 경우
•
한 Service에서 다른 Namespace 또는 다른 클러스터의 Service를 지정하려고 하는 경우
Example
•
생성시 주의할점은 Endpoints의 metadata.name과 ports.name이 Service와 일치해야 한다.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: NodePort
ports:
- name: http
nodePort: 30001
port: 80
targetPort: 80
protocol: TCP
---
apiVersion: v1
kind: Endpoints
metadata:
name: my-service
subsets:
- addresses:
- ip: 52.78.168.73 # 외부 웹 서버 IP 주소
ports:
- name: http
port: 80
YAML
복사