NGINX 인그레스(Ingress) 컨트롤러로 Minikube에서 인그레스 설정하기
인그레스는 클러스터의 서비스에 대한 외부 액세스를 허용하는 규칙을 정의하는 API 객체이다. 인그레스 컨트롤러는 인그레스에 설정된 규칙을 이행한다.
이 페이지에서는 HTTP URI에 따라 요청을 Service 'web' 또는 'web2' 로 라우팅하는 간단한 인그레스를 설정하는 방법을 보여준다.
시작하기 전에
이 튜토리얼에서는 로컬 Kubernetes 클러스터를 실행하기 위해 minikube를 사용한다고 가정한다.
minikube 설치 방법은 도구 설치 문서를 참고하자.
참고:
이 튜토리얼에서는 AMD64 아키텍처를 필요로 하는 컨테이너를 사용한다. 다른 CPU 아키텍처를 사용하는 컴퓨터에서 minikube를 실행하는 경우, AMD64 아키텍처를 에뮬레이션할 수 있는 드라이버를 사용하는 방법을 고려할 수 있다. 예를 들어, Docker Desktop 드라이버가 이를 지원한다.쿠버네티스 클러스터가 필요하고, kubectl 커맨드-라인 툴이 클러스터와 통신할 수 있도록 설정되어 있어야 한다. 이 튜토리얼은 컨트롤 플레인 호스트가 아닌 노드가 적어도 2개 포함된 클러스터에서 실행하는 것을 추천한다. 만약, 아직 클러스터를 가지고 있지 않다면, minikube를 사용해서 생성하거나 다음 쿠버네티스 플레이그라운드 중 하나를 사용할 수 있다.
쿠버네티스 서버의 버전은 다음과 같거나 더 높아야 함. 버전: 1.19.버전 확인을 위해서, 다음 커맨드를 실행 kubectl version.
Minikube 클러스터 생성하기
아직 클러스터를 로컬에 생성하지 않았다면,
minikube start를 실행하여 클러스터를 생성한다.
인그레스 컨트롤러 활성화
NGINX 인그레스 컨트롤러를 활성화하기 위해 다음 명령을 실행한다.
minikube addons enable ingressNGINX 인그레스 컨트롤러가 실행 중인지 확인한다.
kubectl get pods -n ingress-nginx참고:
파드가 정상적으로 실행되기까지 1분 정도 소요될 수 있다.결과는 다음과 같다.
NAME READY STATUS RESTARTS AGE ingress-nginx-admission-create-g9g49 0/1 Completed 0 11m ingress-nginx-admission-patch-rqp78 0/1 Completed 1 11m ingress-nginx-controller-59b45fb494-26npt 1/1 Running 0 11m
hello, world 앱 배포하기
다음 명령을 사용하여 디플로이먼트(Deployment)를 생성한다.
kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0결과는 다음과 같다.
deployment.apps/web created디플로이먼트가 준비(Ready) 상태인지 확인한다.
kubectl get deployment web결과는 다음과 같다.
NAME READY UP-TO-DATE AVAILABLE AGE web 1/1 1 1 53s디플로이먼트를 노출시킨다.
kubectl expose deployment web --type=NodePort --port=8080결과는 다음과 같다.
service/web exposed서비스(Service)가 생성되고 노드 포트에서 사용할 수 있는지 확인한다.
kubectl get service web결과는 다음과 같다.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE web NodePort 10.104.133.249 <none> 8080:31637/TCP 12mminikube service를 사용하여 노드포트(NodePort)를 통해 서비스에 접속한다. 사용 중인 플랫폼에 해당하는 지침을 따라 진행한다.minikube service web --url결과는 다음과 같다.
http://172.17.0.15:31637이전 단계의 출력에서 얻은 URL을 호출한다.
curl http://172.17.0.15:31637# 별도의 터미널에서 명령어를 실행한다. minikube service web --url결과는 다음과 같다.
http://127.0.0.1:62445 ! Because you are using a Docker driver on darwin, the terminal needs to be open to run it.다른 터미널에서, 이전 단계 출력에서 얻은 URL을 호출한다.
curl http://127.0.0.1:62445
결과는 다음과 같다.Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564이제 Minikube IP 주소와 노드포트를 통해 샘플 앱에 액세스할 수 있다. 다음 단계에서는 인그레스 리소스를 사용하여 앱에 액세스할 수 있다.
인그레스 생성하기
다음 매니페스트는 hello-world.example를 통해
서비스로 트래픽을 보내는 인그레스를 정의한다.
다음 파일을 통해
example-ingress.yaml을 만든다.apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: example-ingress spec: ingressClassName: nginx rules: - host: hello-world.info http: paths: - path: / pathType: Prefix backend: service: name: web port: number: 8080다음 명령어를 실행하여 인그레스 오브젝트를 생성한다.
kubectl apply -f https://k8s.io/examples/service/networking/example-ingress.yaml결과는 다음과 같다.
ingress.networking.k8s.io/example-ingress createdIP 주소가 설정되었는지 확인한다.
kubectl get ingress참고:
이 작업은 몇 분 정도 소요될 수 있다.다음 예시와 같이, ADDRESS 열에서 IPv4 주소를 확인할 수 있다.
NAME CLASS HOSTS ADDRESS PORTS AGE example-ingress <none> hello-world.example 172.17.0.15 80 38sIngress 컨트롤러가 트래픽을 제대로 라우팅하는지 확인한다. 사용 중인 플랫폼에 맞는 지침을 따른다.
참고:
MacOS(Darwin)에서 Docker 드라이버를 사용하는 경우, 네트워크가 제한되어 Node IP에 직접 접근할 수 없다. 인그레스를 동작시키려면 새 터미널을 열고minikube tunnel을 실행해야 한다.
이 과정에서는 sudo 권한이 필요하므로, 요청이 나오면 비밀번호를 입력한다.curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example
minikube tunnel
결과는 다음과 같다.
Tunnel successfully started
NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ...
The service/ingress example-ingress requires privileged ports to be exposed: [80 443]
sudo permission will be asked for it.
Starting tunnel for service example-ingress.
새 터미널을 열고 다음 명령어를 실행한다.
curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example
결과는 다음과 같다.
Hello, world!
Version: 1.0.0
Hostname: web-55b8c6998d-8k564
원한다면 브라우저에서
hello-world.example에 접속할 수도 있다.호스트 컴퓨터의
/etc/hosts파일 맨 아래에 다음 행을 추가한다 (관리자 권한 필요).minikube 에서 알려준 external IP 주소를 확인한다.
minikube ip172.17.0.15 hello-world.example참고:
IP 주소를minikube ip출력 값에 맞게 바꾼다.127.0.0.1 hello-world.example이렇게 하면, 웹 브라우저가
hello-world.exampleURL에 대한 요청을 Minikube로 보낸다.
두 번째 디플로이먼트 생성하기
다음 명령을 사용하여 두 번째 디플로이먼트를 생성한다.
kubectl create deployment web2 --image=gcr.io/google-samples/hello-app:2.0결과는 다음과 같다.
deployment.apps/web2 created디플로이먼트가 준비(Ready) 상태인지 확인한다.
kubectl get deployment web2결과는 다음과 같다.
NAME READY UP-TO-DATE AVAILABLE AGE web2 1/1 1 1 53s두 번째 디플로이먼트를 노출시킨다.
kubectl expose deployment web2 --port=8080 --type=NodePort결과는 다음과 같다.
service/web2 exposed
기존 인그레스 수정하기
기존
example-ingress.yaml매니페스트를 편집하고, 하단에 다음 줄을 추가한다.- path: /v2 pathType: Prefix backend: service: name: web2 port: number: 8080변경 사항을 적용한다.
kubectl apply -f example-ingress.yaml결과는 다음과 같다.
ingress.networking/example-ingress configured
인그레스 테스트하기
Hello World 앱의 첫 번째 버전에 액세스한다.
curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.exampleminikube tunnel결과는 다음과 같다.
Tunnel successfully started NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ... The service/ingress example-ingress requires privileged ports to be exposed: [80 443] sudo permission will be asked for it. Starting tunnel for service example-ingress.새로운 터미널을 열고, 아래 명령을 입력한다.
curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example결과는 다음과 같다.
Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564Hello World 앱의 두 번째 버전에 액세스한다.
curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example/v2minikube tunnel결과는 다음과 같다.
Tunnel successfully started NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ... The service/ingress example-ingress requires privileged ports to be exposed: [80 443] sudo permission will be asked for it. Starting tunnel for service example-ingress.새로운 터미널을 열고, 아래 명령을 입력한다.
curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example/v2결과는 다음과 같다.
Hello, world! Version: 2.0.0 Hostname: web2-75cd47646f-t8cjk참고:
옵션으로/etc/hosts를 수정했다면, 브라우저에서hello-world.example과hello-world.example/v2에도 접속할 수 있다.