Kubernetes/Kubernetes Best Practices

[Scenario Playbook Ep.12] 🚀 Ingress 편 #1 | 기본적인 Ingress 설정

ygtoken 2025. 3. 16. 14:22
728x90

 

쿠버네티스에서 Ingress는 외부 트래픽을 클러스터 내부의 서비스로 라우팅하는 핵심 요소입니다.

이번 글에서는 Ingress 리소스를 활용하여 특정 도메인 기반의 트래픽을 내부 서비스로 연결하는 방법을 다룹니다.

 


📌 글에서 다루는 상황들

 

1️⃣ 기본적인 Ingress 리소스 설정

2️⃣ 여러 서비스로 트래픽을 분배하는 Ingress 규칙 적용

 

각 문제를 실무에서 바로 활용할 수 있도록 Manifest 템플릿과 예상 결과 값을 제공합니다.

 


1️⃣ 기본적인 Ingress 리소스 설정

 

❓ 문제 상황

 

운영팀에서 도메인 기반으로 트래픽을 특정 서비스로 라우팅해야 하는 요구사항이 생겼습니다.

기본적으로 Ingress를 활용하여 외부에서 example.com으로 접근하면 내부의 웹 서비스로 연결되도록 설정해야 합니다.

서비스 이름은 web-service이어야 합니다.

외부에서 http://example.com으로 접속하면 내부 web-service로 연결되어야 합니다.

/ 경로는 web-service로 트래픽을 전달해야 합니다.

 

✅ 어떻게 해결할 수 있을까요?

 


🛠️ 해결 방법

 

1. Ingress 리소스를 생성하여 도메인 기반의 트래픽을 특정 서비스로 라우팅해야 합니다.

host: example.com을 설정하여 특정 도메인에서 요청이 올 경우 트래픽을 전달

2. Ingress Controller(NGINX 또는 다른 컨트롤러)가 정상적으로 배포되어 있어야 합니다.

Ingress는 기본적으로 컨트롤러를 필요로 하므로 사전에 설치 확인 필요

 


✅ 정답 Manifest (기본 Ingress 설정)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web-ingress  # Ingress 리소스 이름
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /  # URL 재작성 설정
spec:
  rules:
  - host: example.com  # 특정 도메인으로 접근할 경우
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web-service  # 트래픽을 전달할 대상 서비스
            port:
              number: 80

 

 


📌 적용 후 예상 결과 값

 

1. Ingress 생성 확인

kubectl get ingress

 

💡 예상 출력 값

NAME          CLASS    HOSTS          ADDRESS         PORTS   AGE
web-ingress   nginx    example.com    203.0.113.50    80      5s

 

2. 도메인을 통해 서비스 접근 테스트

curl -H "Host: example.com" http://203.0.113.50

 

💡 예상 출력 값

<!DOCTYPE html>
<html>
<head><title>Welcome to nginx!</title></head>
<body>
<h1>Welcome to nginx!</h1>
</body>
</html>

 

 


2️⃣ 여러 서비스로 트래픽을 분배하는 Ingress 규칙 적용

 

❓ 문제 상황

 

운영팀에서 하나의 Ingress를 통해 서로 다른 서비스로 트래픽을 전달해야 하는 요구사항이 생겼습니다.

example.com 도메인을 사용하되, 경로별로 서로 다른 서비스로 트래픽을 분배해야 합니다.

/app1 요청은 app1-service로 전달되어야 합니다.

/app2 요청은 app2-service로 전달되어야 합니다.

 

✅ 어떻게 해결할 수 있을까요?

 


🛠️ 해결 방법

 

1. Ingress 리소스에서 paths를 활용하여 서로 다른 서비스로 트래픽을 전달하도록 설정해야 합니다.

path: /app1app1-service로 전달

path: /app2app2-service로 전달

2. Ingress Controller(NGINX 등)가 정상적으로 동작하는지 확인해야 합니다.

 


✅ 정답 Manifest (경로 기반 트래픽 분배 Ingress 설정)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: multi-app-ingress  # Ingress 리소스 이름
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /  # URL 재작성 설정
spec:
  rules:
  - host: example.com  # 동일한 도메인에서 접근
    http:
      paths:
      - path: /app1
        pathType: Prefix
        backend:
          service:
            name: app1-service  # /app1 요청은 app1-service로 전달
            port:
              number: 80
      - path: /app2
        pathType: Prefix
        backend:
          service:
            name: app2-service  # /app2 요청은 app2-service로 전달
            port:
              number: 80

 

 


📌 적용 후 예상 결과 값

 

1. Ingress 생성 확인

kubectl get ingress

 

💡 예상 출력 값

NAME                CLASS    HOSTS          ADDRESS         PORTS   AGE
multi-app-ingress   nginx    example.com    203.0.113.50    80      5s

 

2. 각 경로로 서비스 접근 테스트

curl -H "Host: example.com" http://203.0.113.50/app1

💡 예상 출력 값

<!DOCTYPE html>
<html>
<head><title>Welcome to App1!</title></head>
<body>
<h1>Welcome to App1!</h1>
</body>
</html>
curl -H "Host: example.com" http://203.0.113.50/app2

 

💡 예상 출력 값

<!DOCTYPE html>
<html>
<head><title>Welcome to App2!</title></head>
<body>
<h1>Welcome to App2!</h1>
</body>
</html>
728x90