외부 IP 주소를 노출하는 Service 만들기
외부 IP 주소를 노출하는 Service 만들기
이 페이지에서는 외부 IP 주소를 노출하는 쿠버네티스 Service 오브젝트를 만드는 방법을 보여줘요.
출처: 문서
본문
시작하기 전에
- kubectl을 설치한다.
- Google Kubernetes Engine이나 Amazon Web Services 같은 클라우드 제공자로 쿠버네티스 클러스터를 만든다. 이 튜토리얼은 클라우드 제공자가 필요한 외부 로드 밸런서를 만든다.
kubectl이 쿠버네티스 API 서버와 통신하도록 구성한다. 지침은 클라우드 제공자 문서를 참고한다.
목표 (Objectives)
- Hello World 애플리케이션의 인스턴스 다섯 개 실행하기.
- 외부 IP 주소를 노출하는 Service 오브젝트 만들기.
- Service 오브젝트를 사용해 실행 중인 애플리케이션에 접근하기.
다섯 개 파드에서 실행되는 애플리케이션용 Service 만들기
-
클러스터에서 Hello World 애플리케이션을 실행한다:
kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml위 명령은 Deployment와 관련 ReplicaSet을 만든다. ReplicaSet에는 Hello World 애플리케이션을 각각 실행하는 파드 다섯 개가 있다.
-
Deployment에 대한 정보를 표시한다:
kubectl get deployments hello-world kubectl describe deployments hello-world -
ReplicaSet 오브젝트에 대한 정보를 표시한다:
kubectl get replicasets kubectl describe replicasets -
배포를 노출하는 Service 오브젝트를 만든다:
kubectl expose deployment hello-world --type=LoadBalancer --name=my-service -
Service에 대한 정보를 표시한다:
kubectl get services my-service출력은 다음과 비슷합니다:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-service LoadBalancer 10.3.245.137 104.198.205.71 8080/TCP 54stype=LoadBalancer서비스는 외부 클라우드 제공자가 뒷받침하며, 이 예시에서는 다루지 않아요. 자세한 내용은 Service의type: LoadBalancer설정을 참고하세요.외부 IP 주소가 <pending>으로 표시되면 잠시 기다렸다가 같은 명령을 다시 입력해요.
-
Service에 대한 상세 정보를 표시한다:
kubectl describe services my-service출력은 다음과 비슷합니다:
Name: my-service Namespace: default Labels: app.kubernetes.io/name=load-balancer-example Annotations: <none> Selector: app.kubernetes.io/name=load-balancer-example Type: LoadBalancer IP: 10.3.245.137 LoadBalancer Ingress: 104.198.205.71 Port: <unset> 8080/TCP NodePort: <unset> 32377/TCP Endpoints: 10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 2 more... Session Affinity: None Events: <none>서비스가 노출한 외부 IP 주소(
LoadBalancer Ingress)를 기록해두세요. 이 예시에서 외부 IP 주소는 104.198.205.71이에요.Port와NodePort의 값도 기록해두세요. 이 예시에서Port는 8080,NodePort는 32377입니다. -
위 출력에서 서비스에 여러 엔드포인트(10.0.0.6:8080, 10.0.1.6:8080, 10.0.1.7:8080 + 2 more)가 있음을 볼 수 있어요. 이들은 Hello World 애플리케이션을 실행하는 파드의 내부 주소입니다. 이들이 파드 주소인지 확인하려면 다음 명령을 입력해요:
kubectl get pods --output=wide출력은 다음과 비슷합니다:
NAME ... IP NODE hello-world-2895499144-1jaz9 ... 10.0.1.6 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-2e5uh ... 10.0.1.8 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-9m4h1 ... 10.0.0.6 gke-cluster-1-default-pool-e0b8d269-5v7a hello-world-2895499144-o4z13 ... 10.0.1.7 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-segjf ... 10.0.2.5 gke-cluster-1-default-pool-e0b8d269-cpuc -
외부 IP 주소(
LoadBalancer Ingress)를 사용해 Hello World 애플리케이션에 접근한다:curl http://<external-ip>:<port>여기서
<external-ip>는 Service의 외부 IP 주소(LoadBalancer Ingress)이고,<port>는 Service 설명의Port값이에요. minikube를 사용한다면minikube service my-service를 입력하면 Hello World 애플리케이션이 브라우저에서 자동으로 열립니다.성공적인 요청에 대한 응답은 hello 메시지입니다:
Hello, world! Version: 2.0.0 Hostname: 0bd46b45f32f
정리하기
Service를 삭제하려면 다음 명령을 입력해요:
kubectl delete services my-service
Hello World 애플리케이션을 실행하는 Deployment·ReplicaSet·Pods를 삭제하려면 다음 명령을 입력해요:
kubectl delete deployment hello-world
더 알아보기 (Learn more)
Service로 애플리케이션 연결하기에 대해 더 알아보기. 프로덕션에서 새 애플리케이션 버전을 안전하게 테스트하는 법을 배우려면 카나리 릴리스 배포하기를 시도해보세요.