madebychung

nginx ingress controller 설치 후 Ingress 리소스 연동하는 방법 본문

K8s

nginx ingress controller 설치 후 Ingress 리소스 연동하는 방법

mdchung 2025. 4. 8. 15:06

nginx ingress controller 설치 후 Ingress 리소스 연동하는 방법

Kubernetes에서 외부 트래픽을 서비스에 연결하려면 Ingress를 써야 합니다. 하지만 Ingress만 정의한다고 끝이 아니죠. 그걸 실제로 처리해주는 게 Ingress Controller입니다.

이번 글에서는 nginx ingress controller 설치 → Ingress 리소스 연동까지 전 과정을 초보자 눈높이에 맞춰 설명합니다.


1. nginx ingress controller 설치하기 (kubectl 방식)

Helm을 사용하지 않고 kubectl apply 방식으로 간단히 설치할 수 있습니다.

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.9.4/deploy/static/provider/cloud/deploy.yaml

설치되면 아래 네임스페이스에 리소스들이 생깁니다:

  • ingress-nginx 네임스페이스
  • Deployment, Pod, Service, ConfigMap 등

✅ 외부 접속 확인

서비스 타입이 LoadBalancer로 되어 있으니 아래 커맨드로 EXTERNAL-IP 확인합니다.

kubectl get svc -n ingress-nginx

온프레미스 환경이라 EXTERNAL-IP가 <pending>으로 뜨면, NodePort로 수정하거나 외부 LoadBalancer와 연동해야 합니다.


2. 테스트용 서비스 배포하기

Ingress 연동 테스트를 위한 간단한 echo 서버를 배포해봅시다.

kubectl create deployment echo-server --image=k8s.gcr.io/echoserver:1.4

서비스로 노출:

kubectl expose deployment echo-server --port=80 --target-port=8080

3. Ingress 리소스 작성 및 적용

도메인 또는 경로 기반 라우팅을 위해 Ingress를 작성합니다.

cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: echo-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: echo.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: echo-server
            port:
              number: 80
EOF

주의: 위에서 사용한 도메인 echo.example.com은 테스트용입니다.
로컬 테스트 시 /etc/hosts에 IP를 등록해줘야 합니다.

sudo echo "<INGRESS_CONTROLLER_IP> echo.example.com" >> /etc/hosts

4. 정상 작동 테스트

브라우저 또는 curl로 테스트합니다.

curl http://echo.example.com

정상 작동하면 echo 서버에서 리턴되는 메타정보가 출력됩니다.


정리

  • Ingress는 규칙, Ingress Controller는 실질적인 처리기
  • nginx ingress controller는 ingressClassName: nginx로 명시
  • 도메인 테스트를 위해 /etc/hosts 설정 필요
  • NodePort 환경에서는 Ingress Controller 서비스의 NodePort를 통해 접근 가능